Skip to content
Snippets Groups Projects
Commit d734f1eb authored by Alena Petraki's avatar Alena Petraki
Browse files

Объект роли вынесен в отдельный файл

parent 626af5bb
Branches
Tags
No related merge requests found
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
}
...@@ -2,38 +2,8 @@ package roles ...@@ -2,38 +2,8 @@ package roles
import ( import (
"context" "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 // @microgen grpc
// @protobuf git.perx.ru/perxis/perxis-go/proto/roles // @protobuf git.perx.ru/perxis/perxis-go/proto/roles
// @grpc-addr content.roles.Roles // @grpc-addr content.roles.Roles
...@@ -44,33 +14,3 @@ type Roles interface { ...@@ -44,33 +14,3 @@ type Roles interface {
Update(ctx context.Context, role *Role) (err error) Update(ctx context.Context, role *Role) (err error)
Delete(ctx context.Context, spaceId, roleId string) (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
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment