add acl
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
package acl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"mediarr/database"
|
||||
"mediarr/document"
|
||||
"mediarr/ident"
|
||||
"mediarr/identity"
|
||||
)
|
||||
|
||||
func (s *Service) Effective(ctx context.Context, principalID, documentID ident.UUID) (Permission, error) {
|
||||
principal, err := s.identity.GetPrincipal(ctx, principalID)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return 0, ErrSubjectNotFound
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
if principal.Status != identity.StatusActive {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
chain, err := s.documentChain(ctx, documentID)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
target := chain[0]
|
||||
if target.OwnerID == principalID {
|
||||
return PermissionAll, nil
|
||||
}
|
||||
|
||||
subjects := map[ident.UUID]struct{}{principalID: {}}
|
||||
groups, err := s.identity.ListGroups(ctx, principalID)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
for _, group := range groups {
|
||||
if group.Status == identity.StatusActive {
|
||||
subjects[group.ID] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
var effective Permission
|
||||
now := time.Now().UnixMilli()
|
||||
inherit := target.AclInherit != 0
|
||||
|
||||
for depth, current := range chain {
|
||||
if depth > 0 && !inherit {
|
||||
break
|
||||
}
|
||||
|
||||
entries, err := s.db.Queries.ListActiveACLEntriesForDocument(ctx, database.ListActiveACLEntriesForDocumentParams{
|
||||
DocumentID: current.ID,
|
||||
Now: now,
|
||||
})
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
for _, entry := range entries {
|
||||
if depth > 0 && entry.Propagate == 0 {
|
||||
continue
|
||||
}
|
||||
if _, matches := subjects[entry.SubjectID]; matches {
|
||||
effective |= Permission(entry.Permissions)
|
||||
}
|
||||
}
|
||||
|
||||
if depth == 0 {
|
||||
inherit = current.AclInherit != 0
|
||||
} else {
|
||||
inherit = inherit && current.AclInherit != 0
|
||||
}
|
||||
}
|
||||
|
||||
switch effectiveVisibility(chain) {
|
||||
case document.VisibilityCluster:
|
||||
if principal.Kind == identity.KindUser || principal.Kind == identity.KindGuest {
|
||||
effective |= PermissionRead
|
||||
}
|
||||
case document.VisibilityPublic:
|
||||
effective |= PermissionRead
|
||||
}
|
||||
|
||||
return effective, nil
|
||||
}
|
||||
|
||||
func (s *Service) Can(ctx context.Context, principalID, documentID ident.UUID, required Permission) (bool, error) {
|
||||
if required == 0 {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
permissions, err := s.Effective(ctx, principalID, documentID)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return permissions.Has(required), nil
|
||||
}
|
||||
|
||||
func effectiveVisibility(chain []database.Document) string {
|
||||
for _, document := range chain {
|
||||
if document.Visibility.Valid {
|
||||
return document.Visibility.String
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
Reference in New Issue
Block a user