From 3353fec7cf0501a0932b6528fa8ca9795db6fac6 Mon Sep 17 00:00:00 2001 From: Alena Petraki <alena.petraki@gmail.com> Date: Tue, 18 Apr 2023 11:35:13 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/auth/anonymous.go | 11 ++++++----- pkg/auth/client.go | 15 ++++++++------- pkg/auth/errors.go | 10 ---------- pkg/auth/user.go | 17 +++++++++-------- pkg/cache/cache.go | 10 ++++------ pkg/service/errors.go | 9 +++++++++ 6 files changed, 36 insertions(+), 36 deletions(-) delete mode 100644 pkg/auth/errors.go create mode 100644 pkg/service/errors.go diff --git a/pkg/auth/anonymous.go b/pkg/auth/anonymous.go index 62aa8a1a..e000842a 100644 --- a/pkg/auth/anonymous.go +++ b/pkg/auth/anonymous.go @@ -9,6 +9,7 @@ import ( "git.perx.ru/perxis/perxis-go/pkg/members" "git.perx.ru/perxis/perxis-go/pkg/permission" "git.perx.ru/perxis/perxis-go/pkg/roles" + "git.perx.ru/perxis/perxis-go/pkg/service" "git.perx.ru/perxis/perxis-go/pkg/spaces" ) @@ -23,7 +24,7 @@ func (Anonymous) GetID(ctx context.Context) string { return "anonymous" } func (Anonymous) IsValid(ctx context.Context) bool { return false } func (Anonymous) IsSystem(ctx context.Context) bool { return false } func (Anonymous) IsManagementAllowed(ctx context.Context, spaceID string) error { - return ErrAccessDenied + return service.ErrAccessDenied } func (a Anonymous) Space(spaceID string) SpaceAccessor { @@ -84,7 +85,7 @@ func (Anonymous) Format(f fmt.State, verb rune) { func (a Anonymous) HasAccess(ctx context.Context, spaceID, orgID string) error { if !a.IsValid(ctx) { - return ErrAccessDenied + return service.ErrAccessDenied } if a.IsSystem(ctx) { @@ -106,7 +107,7 @@ func (a Anonymous) HasAccess(ctx context.Context, spaceID, orgID string) error { return nil } - return ErrAccessDenied + return service.ErrAccessDenied } func (a *Anonymous) hasRole(ctx context.Context, spaceID string) (bool, error) { @@ -118,9 +119,9 @@ func (a *Anonymous) hasRole(ctx context.Context, spaceID string) (bool, error) { return true, nil } - if errors.Is(err, ErrNotFound) { + if errors.Is(err, service.ErrNotFound) { if sp := a.getSpace(ctx, spaceID); sp == nil { - return false, ErrNotFound + return false, service.ErrNotFound } } return false, nil diff --git a/pkg/auth/client.go b/pkg/auth/client.go index cf63410f..1e22b4b9 100644 --- a/pkg/auth/client.go +++ b/pkg/auth/client.go @@ -11,6 +11,7 @@ import ( "git.perx.ru/perxis/perxis-go/pkg/members" "git.perx.ru/perxis/perxis-go/pkg/permission" "git.perx.ru/perxis/perxis-go/pkg/roles" + "git.perx.ru/perxis/perxis-go/pkg/service" "git.perx.ru/perxis/perxis-go/pkg/spaces" ) @@ -76,14 +77,14 @@ func (ClientPrincipal) IsSystem(ctx context.Context) bool { func (c *ClientPrincipal) IsManagementAllowed(ctx context.Context, spaceID string) error { if !c.IsValid(ctx) { - return ErrAccessDenied + return service.ErrAccessDenied } if role := c.Role(ctx, spaceID); role != nil && role.AllowManagement { return nil } - return ErrAccessDenied + return service.ErrAccessDenied } func (c *ClientPrincipal) Member(ctx context.Context) members.Role { @@ -211,7 +212,7 @@ func (c *ClientPrincipal) Rules(ctx context.Context, spaceID, envID string) perm func (c *ClientPrincipal) HasAccess(ctx context.Context, spaceID, orgID string) error { if !c.IsValid(ctx) { - return ErrAccessDenied + return service.ErrAccessDenied } if c.IsSystem(ctx) { @@ -220,7 +221,7 @@ func (c *ClientPrincipal) HasAccess(ctx context.Context, spaceID, orgID string) if spaceID != "" { if c.spaceID == "" { - return ErrAccessDenied + return service.ErrAccessDenied } client, _ := c.Client(ctx) @@ -233,7 +234,7 @@ func (c *ClientPrincipal) HasAccess(ctx context.Context, spaceID, orgID string) return nil } - return ErrAccessDenied + return service.ErrAccessDenied } func (c *ClientPrincipal) hasRole(ctx context.Context, spaceID string) (bool, error) { @@ -242,9 +243,9 @@ func (c *ClientPrincipal) hasRole(ctx context.Context, spaceID string) (bool, er } client, err := c.Client(ctx) - if err != nil && errors.Is(err, ErrNotFound) { + if err != nil && errors.Is(err, service.ErrNotFound) { if sp := c.getSpace(ctx, spaceID); sp == nil { - return false, ErrNotFound + return false, service.ErrNotFound } } if client != nil && client.SpaceID == spaceID { diff --git a/pkg/auth/errors.go b/pkg/auth/errors.go deleted file mode 100644 index 8522ec94..00000000 --- a/pkg/auth/errors.go +++ /dev/null @@ -1,10 +0,0 @@ -package auth - -import ( - "git.perx.ru/perxis/perxis-go/pkg/errors" -) - -var ( - ErrAccessDenied = errors.PermissionDenied(errors.New("access denied")) - ErrNotFound = errors.NotFound(errors.New("not found")) -) diff --git a/pkg/auth/user.go b/pkg/auth/user.go index f34693bf..a4500c14 100644 --- a/pkg/auth/user.go +++ b/pkg/auth/user.go @@ -10,6 +10,7 @@ import ( "git.perx.ru/perxis/perxis-go/pkg/members" "git.perx.ru/perxis/perxis-go/pkg/permission" "git.perx.ru/perxis/perxis-go/pkg/roles" + "git.perx.ru/perxis/perxis-go/pkg/service" "git.perx.ru/perxis/perxis-go/pkg/spaces" "git.perx.ru/perxis/perxis-go/pkg/users" ) @@ -87,7 +88,7 @@ func (u *UserPrincipal) IsSystem(ctx context.Context) bool { func (u *UserPrincipal) IsManagementAllowed(ctx context.Context, spaceID string) error { if !u.IsValid(ctx) { - return ErrAccessDenied + return service.ErrAccessDenied } if u.IsSystem(ctx) { @@ -102,7 +103,7 @@ func (u *UserPrincipal) IsManagementAllowed(ctx context.Context, spaceID string) return nil } - return ErrAccessDenied + return service.ErrAccessDenied } func (u *UserPrincipal) User(ctx context.Context) *users.User { @@ -182,7 +183,7 @@ func (u *UserPrincipal) HasSpaceAccess(ctx context.Context, spaceID string) bool // - Пространство позволяет доступ для не участников (есть роли AnonymousRole/AuthorizedRole/ViewRole) func (u *UserPrincipal) HasAccess(ctx context.Context, spaceID, orgID string) error { if !u.IsValid(ctx) { - return ErrAccessDenied + return service.ErrAccessDenied } if u.IsSystem(ctx) { @@ -210,7 +211,7 @@ func (u *UserPrincipal) HasAccess(ctx context.Context, spaceID, orgID string) er } } - return ErrAccessDenied + return service.ErrAccessDenied } func (u *UserPrincipal) hasRole(ctx context.Context, spaceID string) (bool, error) { @@ -230,9 +231,9 @@ func (u *UserPrincipal) hasRole(ctx context.Context, spaceID string) (bool, erro if rErr == nil { return true, nil } - if errors.Is(cErr, ErrNotFound) || errors.Is(rErr, ErrNotFound) { + if errors.Is(cErr, service.ErrNotFound) || errors.Is(rErr, service.ErrNotFound) { if sp := u.getSpace(ctx, spaceID); sp == nil { - return false, ErrNotFound + return false, service.ErrNotFound } } @@ -249,9 +250,9 @@ func (u *UserPrincipal) hasRole(ctx context.Context, spaceID string) (bool, erro return true, nil } - if errors.Is(cErr, ErrNotFound) || errors.Is(rErr, ErrNotFound) { + if errors.Is(cErr, service.ErrNotFound) || errors.Is(rErr, service.ErrNotFound) { if sp := u.getSpace(ctx, spaceID); sp == nil { - return false, ErrNotFound + return false, service.ErrNotFound } } diff --git a/pkg/cache/cache.go b/pkg/cache/cache.go index 7f7248e5..8d32fb4d 100644 --- a/pkg/cache/cache.go +++ b/pkg/cache/cache.go @@ -1,10 +1,10 @@ package cache import ( - "errors" "fmt" "time" + "git.perx.ru/perxis/perxis-go/pkg/service" lru "github.com/hashicorp/golang-lru" "go.uber.org/zap" ) @@ -14,8 +14,6 @@ const ( defaultTTL = 30 * time.Second ) -var ErrNotFound = errors.New("not found") - type Cache struct { cache *lru.Cache ttl time.Duration @@ -69,13 +67,13 @@ func (c *Cache) Get(key interface{}) (value interface{}, err error) { if v.expiredAt.Before(time.Now()) { c.Remove(key) c.logger.Debug("Expired", zap.String("key", fmt.Sprintf("%v", key)), zap.String("ptr", fmt.Sprintf("%p", v.value))) - return nil, ErrNotFound + return nil, service.ErrNotFound } c.logger.Debug("Hit", zap.String("key", fmt.Sprintf("%v", key)), zap.String("ptr", fmt.Sprintf("%p", v.value))) return v.value, nil } c.logger.Debug("Miss", zap.String("key", fmt.Sprintf("%v", key))) - return nil, ErrNotFound + return nil, service.ErrNotFound } func (c *Cache) Remove(key interface{}) (err error) { @@ -83,7 +81,7 @@ func (c *Cache) Remove(key interface{}) (err error) { c.logger.Debug("Remove", zap.String("key", fmt.Sprintf("%v", key))) if !present { - err = ErrNotFound + err = service.ErrNotFound } return diff --git a/pkg/service/errors.go b/pkg/service/errors.go new file mode 100644 index 00000000..055b9792 --- /dev/null +++ b/pkg/service/errors.go @@ -0,0 +1,9 @@ +package service + +import "git.perx.ru/perxis/perxis-go/pkg/errors" + +var ( + ErrAccessDenied = errors.PermissionDenied(errors.New("access denied")) + ErrNotFound = errors.NotFound(errors.New("not found")) + ErrAlreadyExists = errors.AlreadyExists(errors.New("already exists")) +) -- GitLab