From d734f1ebe35e93bb17617842134399a26037d0ff Mon Sep 17 00:00:00 2001 From: Alena Petraki <alena.petraki@gmail.com> Date: Thu, 13 Apr 2023 18:11:23 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D1=8A=D0=B5=D0=BA=D1=82=20=D1=80?= =?UTF-8?q?=D0=BE=D0=BB=D0=B8=20=D0=B2=D1=8B=D0=BD=D0=B5=D1=81=D0=B5=D0=BD?= =?UTF-8?q?=20=D0=B2=20=D0=BE=D1=82=D0=B4=D0=B5=D0=BB=D1=8C=D0=BD=D1=8B?= =?UTF-8?q?=D0=B9=20=D1=84=D0=B0=D0=B9=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/roles/role.go | 65 ++++++++++++++++++++++++++++++++++++++++++++ pkg/roles/service.go | 60 ---------------------------------------- 2 files changed, 65 insertions(+), 60 deletions(-) create mode 100644 pkg/roles/role.go diff --git a/pkg/roles/role.go b/pkg/roles/role.go new file mode 100644 index 00000000..4c284ee5 --- /dev/null +++ b/pkg/roles/role.go @@ -0,0 +1,65 @@ +package roles + +import ( + "context" + + "git.perx.ru/perxis/perxis-go/pkg/data" + "git.perx.ru/perxis/perxis-go/pkg/environments" + "git.perx.ru/perxis/perxis-go/pkg/permission" +) + +const ( + AnonymousRole = "anonymous" + AuthorizedRole = "authorized" + ViewRole = "view" +) + +type Role struct { + // Внутренний идентификатор роли + ID string `json:"id" bson:"_id"` + + // Рдентификатор пространства + SpaceID string `json:"spaceId" bson:"-"` + + // Описание роли, назначение + Description string `json:"description" bson:"description"` + + // РЎРїРёСЃРѕРє доступных окружений (ID или Alias) + Environments []string `json:"environments" bson:"environments"` + + // РЎРїРёСЃРѕРє правил доступа Рє коллекциям + Rules permission.Rules `json:"rules" bson:"rules"` + + // Разрешить доступ API управления + AllowManagement bool `json:"allow_management" bson:"allow_management"` +} + +func (r Role) CanAccessEnvironment(ctx context.Context, service environments.Environments, spaceID, envID string) bool { + if spaceID == "" || envID == "" { + return false + } + + // Если СЏРІРЅРѕ РЅРµ указаны доступные окружения - доступ РїРѕ умолчанию Рє окружению master + if len(r.Environments) == 0 { + r.Environments = []string{environments.DefaultEnvironment} + } + + if data.Contains(envID, r.Environments) { + return true + } + + e, err := service.Get(ctx, spaceID, envID) + if err != nil || e == nil { + return false + } + + aliases := append(e.Aliases, e.ID) + + for _, ce := range r.Environments { + if data.Contains(ce, aliases) { + return true + } + } + + return false +} diff --git a/pkg/roles/service.go b/pkg/roles/service.go index 4606e3dd..b003008b 100644 --- a/pkg/roles/service.go +++ b/pkg/roles/service.go @@ -2,38 +2,8 @@ package roles import ( "context" - - "git.perx.ru/perxis/perxis-go/pkg/data" - "git.perx.ru/perxis/perxis-go/pkg/environments" - "git.perx.ru/perxis/perxis-go/pkg/permission" -) - -const ( - AnonymousRole = "anonymous" - AuthorizedRole = "authorized" - ViewRole = "view" ) -type Role struct { - // Внутренний идентификатор роли - ID string `json:"id" bson:"_id"` - - // Рдентификатор пространства - SpaceID string `json:"spaceId" bson:"-"` - - // Описание роли, назначение - Description string `json:"description" bson:"description"` - - // РЎРїРёСЃРѕРє доступных окружений (ID или Alias) - Environments []string `json:"environments" bson:"environments"` - - // РЎРїРёСЃРѕРє правил доступа Рє коллекциям - Rules permission.Rules `json:"rules" bson:"rules"` - - // Разрешить доступ API управления - AllowManagement bool `json:"allow_management" bson:"allow_management"` -} - // @microgen grpc // @protobuf git.perx.ru/perxis/perxis-go/proto/roles // @grpc-addr content.roles.Roles @@ -44,33 +14,3 @@ type Roles interface { Update(ctx context.Context, role *Role) (err error) Delete(ctx context.Context, spaceId, roleId string) (err error) } - -func (r Role) CanAccessEnvironment(ctx context.Context, service environments.Environments, spaceID, envID string) bool { - if spaceID == "" || envID == "" { - return false - } - - // Если СЏРІРЅРѕ РЅРµ указаны доступные окружения - доступ РїРѕ умолчанию Рє окружению master - if len(r.Environments) == 0 { - r.Environments = []string{environments.DefaultEnvironment} - } - - if data.Contains(envID, r.Environments) { - return true - } - - e, err := service.Get(ctx, spaceID, envID) - if err != nil || e == nil { - return false - } - - aliases := append(e.Aliases, e.ID) - - for _, ce := range r.Environments { - if data.Contains(ce, aliases) { - return true - } - } - - return false -} -- GitLab