diff --git a/pkg/auth/anonymous.go b/pkg/auth/anonymous.go index 62aa8a1ac28c45f09a065906a7735e44c1add05e..e000842a68b2a0a64b5ed59bc7f5bcb5b7bf6514 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 cf63410f0c2efb4c3d2150ce3956a75c5e1cbfab..1e22b4b92c27ff35f21d4e866394363b39c96a98 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 8522ec94ff4ac7e04e6eabfdca561f698645db9d..0000000000000000000000000000000000000000 --- 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 f34693bf93f9b63c5d5eca84398b096af86e52ef..a4500c140ee48d80b97000fd3f703da8be7c782d 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 7f7248e5208f1691d7e094eaf640f361383faab6..8d32fb4d066026d0ccab1a0c596aa54bfb18d6ba 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 0000000000000000000000000000000000000000..055b97921ff78450bb229a14efeca265903e42c4 --- /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")) +)