Select Git revision
channel_core_test.go
service.go 2.13 KiB
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
type Roles interface {
Create(ctx context.Context, role *Role) (created *Role, err error)
Get(ctx context.Context, spaceId, roleId string) (role *Role, err error)
List(ctx context.Context, spaceId string) (roles []*Role, err error)
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
}