acl
This commit is contained in:
+51
-4
@@ -10,14 +10,24 @@ import (
|
||||
|
||||
"mediarr/database"
|
||||
"mediarr/ident"
|
||||
"mediarr/permission"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
db database.DatabaseClient
|
||||
type Authorizer interface {
|
||||
Can(ctx context.Context, principalID, documentID ident.UUID, required permission.Permission) (bool, error)
|
||||
IsActive(ctx context.Context, principalID ident.UUID) (bool, error)
|
||||
}
|
||||
|
||||
func NewService(db database.DatabaseClient) *Service {
|
||||
return &Service{db: db}
|
||||
type Service struct {
|
||||
db database.DatabaseClient
|
||||
authorizer Authorizer
|
||||
}
|
||||
|
||||
func NewService(db database.DatabaseClient, authorizer Authorizer) *Service {
|
||||
return &Service{
|
||||
db: db,
|
||||
authorizer: authorizer,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) withTx(ctx context.Context, fn func(*database.Queries) error) error {
|
||||
@@ -60,6 +70,43 @@ func (s *Service) getActiveDocument(ctx context.Context, id ident.UUID) (*databa
|
||||
return document, nil
|
||||
}
|
||||
|
||||
func (s *Service) getDocumentModel(ctx context.Context, id ident.UUID) (*Document, error) {
|
||||
document, err := s.getDocument(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return documentFromDB(*document), nil
|
||||
}
|
||||
|
||||
func (s *Service) requireActive(ctx context.Context, principalID ident.UUID) error {
|
||||
active, err := s.authorizer.IsActive(ctx, principalID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !active {
|
||||
return fmt.Errorf("%w: principal inactif %s", ErrPermissionDenied, principalID)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) allowed(ctx context.Context, principalID, documentID ident.UUID, required permission.Permission) (bool, error) {
|
||||
return s.authorizer.Can(ctx, principalID, documentID, required)
|
||||
}
|
||||
|
||||
func (s *Service) can(ctx context.Context, principalID, documentID ident.UUID, required permission.Permission) error {
|
||||
allowed, err := s.allowed(ctx, principalID, documentID, required)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !allowed {
|
||||
return fmt.Errorf("%w: %s sur %s", ErrPermissionDenied, principalID, documentID)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func normalizeName(name string) (string, string, error) {
|
||||
normalized := strings.TrimSpace(name)
|
||||
if normalized == "" || len(normalized) > 255 || strings.Contains(normalized, "/") || strings.ContainsRune(normalized, 0) {
|
||||
|
||||
Reference in New Issue
Block a user