add acl
This commit is contained in:
+70
@@ -0,0 +1,70 @@
|
||||
package acl
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"mediarr/ident"
|
||||
)
|
||||
|
||||
type Permission int64
|
||||
|
||||
const (
|
||||
PermissionRead Permission = 1 << iota
|
||||
PermissionWrite
|
||||
PermissionCreate
|
||||
PermissionDelete
|
||||
PermissionShare
|
||||
PermissionManage
|
||||
PermissionAdmin
|
||||
)
|
||||
|
||||
const PermissionAll = PermissionRead |
|
||||
PermissionWrite |
|
||||
PermissionCreate |
|
||||
PermissionDelete |
|
||||
PermissionShare |
|
||||
PermissionManage |
|
||||
PermissionAdmin
|
||||
|
||||
func (p Permission) Valid() bool {
|
||||
return p > 0 && p <= PermissionAll
|
||||
}
|
||||
|
||||
func (p Permission) Has(required Permission) bool {
|
||||
if required == 0 {
|
||||
return true
|
||||
}
|
||||
if !required.Valid() {
|
||||
return false
|
||||
}
|
||||
return p&PermissionAdmin != 0 || p&required == required
|
||||
}
|
||||
|
||||
type Entry struct {
|
||||
ID ident.UUID
|
||||
DocumentID ident.UUID
|
||||
SubjectID ident.UUID
|
||||
Permissions Permission
|
||||
Propagate bool
|
||||
ExpiresAt *time.Time
|
||||
GrantedBy ident.UUID
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type GrantParams struct {
|
||||
DocumentID ident.UUID
|
||||
SubjectID ident.UUID
|
||||
Permissions Permission
|
||||
Propagate bool
|
||||
ExpiresAt *time.Time
|
||||
GrantedBy ident.UUID
|
||||
}
|
||||
|
||||
var (
|
||||
ErrInvalidPermissions = errors.New("permissions invalides")
|
||||
ErrDocumentNotFound = errors.New("document introuvable")
|
||||
ErrSubjectNotFound = errors.New("sujet ACL introuvable")
|
||||
ErrEntryNotFound = errors.New("entrée ACL introuvable")
|
||||
ErrDocumentCycle = errors.New("cycle dans l'arborescence des documents")
|
||||
)
|
||||
+123
@@ -0,0 +1,123 @@
|
||||
package acl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"mediarr/database"
|
||||
"mediarr/ident"
|
||||
)
|
||||
|
||||
func (s *Service) Grant(ctx context.Context, params GrantParams) (*Entry, error) {
|
||||
if !params.Permissions.Valid() {
|
||||
return nil, fmt.Errorf("%w: %d", ErrInvalidPermissions, params.Permissions)
|
||||
}
|
||||
|
||||
if _, err := s.db.Queries.GetDocument(ctx, params.DocumentID); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, fmt.Errorf("%w: %s", ErrDocumentNotFound, params.DocumentID)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if _, err := s.loadSubject(ctx, params.SubjectID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
id, err := ident.NewV7()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
|
||||
entry, err := s.db.WriteQueries.UpsertACLEntry(ctx, database.UpsertACLEntryParams{
|
||||
ID: id,
|
||||
DocumentID: params.DocumentID,
|
||||
SubjectID: params.SubjectID,
|
||||
Permissions: int64(params.Permissions),
|
||||
Propagate: boolToInt(params.Propagate),
|
||||
ExpiresAt: timeToNull(params.ExpiresAt),
|
||||
GrantedBy: params.GrantedBy,
|
||||
CreatedAt: now.UnixMilli(),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return entryFromDB(entry), nil
|
||||
}
|
||||
|
||||
func (s *Service) Revoke(ctx context.Context, documentID, subjectID ident.UUID) error {
|
||||
_, err := s.db.WriteQueries.RevokeACLEntry(ctx, database.RevokeACLEntryParams{
|
||||
RevokedAt: sql.NullInt64{
|
||||
Int64: time.Now().UnixMilli(),
|
||||
Valid: true,
|
||||
},
|
||||
DocumentID: documentID,
|
||||
SubjectID: subjectID,
|
||||
})
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ErrEntryNotFound
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) List(ctx context.Context, documentID ident.UUID) ([]Entry, error) {
|
||||
rows, err := s.db.Queries.ListActiveACLEntriesForDocument(ctx, database.ListActiveACLEntriesForDocumentParams{
|
||||
DocumentID: documentID,
|
||||
Now: time.Now().UnixMilli(),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
entries := make([]Entry, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
entries = append(entries, *entryFromDB(row))
|
||||
}
|
||||
|
||||
return entries, nil
|
||||
}
|
||||
|
||||
func entryFromDB(entry database.AclEntry) *Entry {
|
||||
return &Entry{
|
||||
ID: entry.ID,
|
||||
DocumentID: entry.DocumentID,
|
||||
SubjectID: entry.SubjectID,
|
||||
Permissions: Permission(entry.Permissions),
|
||||
Propagate: entry.Propagate != 0,
|
||||
ExpiresAt: nullToTime(entry.ExpiresAt),
|
||||
GrantedBy: entry.GrantedBy,
|
||||
CreatedAt: time.UnixMilli(entry.CreatedAt),
|
||||
}
|
||||
}
|
||||
|
||||
func boolToInt(value bool) int64 {
|
||||
if value {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func timeToNull(value *time.Time) sql.NullInt64 {
|
||||
if value == nil {
|
||||
return sql.NullInt64{}
|
||||
}
|
||||
return sql.NullInt64{Int64: value.UnixMilli(), Valid: true}
|
||||
}
|
||||
|
||||
func nullToTime(value sql.NullInt64) *time.Time {
|
||||
if !value.Valid {
|
||||
return nil
|
||||
}
|
||||
result := time.UnixMilli(value.Int64)
|
||||
return &result
|
||||
}
|
||||
@@ -0,0 +1,279 @@
|
||||
package acl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"mediarr/ident"
|
||||
)
|
||||
|
||||
func TestGrantAndList(t *testing.T) {
|
||||
service := newTestService(t)
|
||||
ctx := context.Background()
|
||||
|
||||
owner := mustUser(t, service, "owner")
|
||||
alice := mustUser(t, service, "alice")
|
||||
documentID := mustDocument(t, service, owner.ID, nil, "Photos", "", true)
|
||||
|
||||
expiresAt := time.Now().Add(time.Hour).Truncate(time.Millisecond)
|
||||
|
||||
entry, err := service.Grant(ctx, GrantParams{
|
||||
DocumentID: documentID,
|
||||
SubjectID: alice.ID,
|
||||
Permissions: PermissionRead | PermissionWrite,
|
||||
Propagate: true,
|
||||
ExpiresAt: &expiresAt,
|
||||
GrantedBy: owner.ID,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Grant : %v", err)
|
||||
}
|
||||
if entry.SubjectID != alice.ID {
|
||||
t.Fatalf("subject = %s, attendu %s", entry.SubjectID, alice.ID)
|
||||
}
|
||||
if entry.Permissions != PermissionRead|PermissionWrite {
|
||||
t.Fatalf("permissions = %d, attendu %d", entry.Permissions, PermissionRead|PermissionWrite)
|
||||
}
|
||||
if !entry.Propagate {
|
||||
t.Fatal("propagate = false, attendu true")
|
||||
}
|
||||
if entry.ExpiresAt == nil || !entry.ExpiresAt.Equal(expiresAt) {
|
||||
t.Fatalf("expires_at = %v, attendu %v", entry.ExpiresAt, expiresAt)
|
||||
}
|
||||
|
||||
entries, err := service.List(ctx, documentID)
|
||||
if err != nil {
|
||||
t.Fatalf("List : %v", err)
|
||||
}
|
||||
if len(entries) != 1 {
|
||||
t.Fatalf("entrées = %d, attendu 1", len(entries))
|
||||
}
|
||||
if entries[0].ID != entry.ID {
|
||||
t.Fatalf("id = %s, attendu %s", entries[0].ID, entry.ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGrantUpsertsExistingEntry(t *testing.T) {
|
||||
service := newTestService(t)
|
||||
ctx := context.Background()
|
||||
|
||||
owner := mustUser(t, service, "owner")
|
||||
alice := mustUser(t, service, "alice")
|
||||
documentID := mustDocument(t, service, owner.ID, nil, "Photos", "", true)
|
||||
|
||||
if _, err := service.Grant(ctx, GrantParams{
|
||||
DocumentID: documentID,
|
||||
SubjectID: alice.ID,
|
||||
Permissions: PermissionRead,
|
||||
GrantedBy: owner.ID,
|
||||
}); err != nil {
|
||||
t.Fatalf("premier Grant : %v", err)
|
||||
}
|
||||
|
||||
if _, err := service.Grant(ctx, GrantParams{
|
||||
DocumentID: documentID,
|
||||
SubjectID: alice.ID,
|
||||
Permissions: PermissionWrite,
|
||||
GrantedBy: owner.ID,
|
||||
}); err != nil {
|
||||
t.Fatalf("second Grant : %v", err)
|
||||
}
|
||||
|
||||
entries, err := service.List(ctx, documentID)
|
||||
if err != nil {
|
||||
t.Fatalf("List : %v", err)
|
||||
}
|
||||
if len(entries) != 1 {
|
||||
t.Fatalf("entrées = %d, attendu 1", len(entries))
|
||||
}
|
||||
if entries[0].Permissions != PermissionWrite {
|
||||
t.Fatalf("permissions = %d, attendu %d", entries[0].Permissions, PermissionWrite)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGrantRejectsInvalidPermissions(t *testing.T) {
|
||||
service := newTestService(t)
|
||||
ctx := context.Background()
|
||||
|
||||
owner := mustUser(t, service, "owner")
|
||||
alice := mustUser(t, service, "alice")
|
||||
documentID := mustDocument(t, service, owner.ID, nil, "Photos", "", true)
|
||||
|
||||
for _, permissions := range []Permission{0, PermissionAll + 1} {
|
||||
_, err := service.Grant(ctx, GrantParams{
|
||||
DocumentID: documentID,
|
||||
SubjectID: alice.ID,
|
||||
Permissions: permissions,
|
||||
GrantedBy: owner.ID,
|
||||
})
|
||||
if !errors.Is(err, ErrInvalidPermissions) {
|
||||
t.Fatalf("permissions %d : erreur = %v, attendu ErrInvalidPermissions", permissions, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGrantRejectsMissingDocument(t *testing.T) {
|
||||
service := newTestService(t)
|
||||
ctx := context.Background()
|
||||
|
||||
owner := mustUser(t, service, "owner")
|
||||
alice := mustUser(t, service, "alice")
|
||||
missing, err := ident.NewV7()
|
||||
if err != nil {
|
||||
t.Fatalf("NewV7 : %v", err)
|
||||
}
|
||||
|
||||
_, err = service.Grant(ctx, GrantParams{
|
||||
DocumentID: missing,
|
||||
SubjectID: alice.ID,
|
||||
Permissions: PermissionRead,
|
||||
GrantedBy: owner.ID,
|
||||
})
|
||||
if !errors.Is(err, ErrDocumentNotFound) {
|
||||
t.Fatalf("erreur = %v, attendu ErrDocumentNotFound", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGrantRejectsMissingSubject(t *testing.T) {
|
||||
service := newTestService(t)
|
||||
ctx := context.Background()
|
||||
|
||||
owner := mustUser(t, service, "owner")
|
||||
documentID := mustDocument(t, service, owner.ID, nil, "Photos", "", true)
|
||||
missing, err := ident.NewV7()
|
||||
if err != nil {
|
||||
t.Fatalf("NewV7 : %v", err)
|
||||
}
|
||||
|
||||
_, err = service.Grant(ctx, GrantParams{
|
||||
DocumentID: documentID,
|
||||
SubjectID: missing,
|
||||
Permissions: PermissionRead,
|
||||
GrantedBy: owner.ID,
|
||||
})
|
||||
if !errors.Is(err, ErrSubjectNotFound) {
|
||||
t.Fatalf("erreur = %v, attendu ErrSubjectNotFound", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGrantRejectsInactiveSubject(t *testing.T) {
|
||||
service := newTestService(t)
|
||||
ctx := context.Background()
|
||||
|
||||
owner := mustUser(t, service, "owner")
|
||||
alice := mustUser(t, service, "alice")
|
||||
documentID := mustDocument(t, service, owner.ID, nil, "Photos", "", true)
|
||||
|
||||
disablePrincipal(t, service, alice.ID)
|
||||
|
||||
_, err := service.Grant(ctx, GrantParams{
|
||||
DocumentID: documentID,
|
||||
SubjectID: alice.ID,
|
||||
Permissions: PermissionRead,
|
||||
GrantedBy: owner.ID,
|
||||
})
|
||||
if !errors.Is(err, ErrSubjectNotFound) {
|
||||
t.Fatalf("erreur = %v, attendu ErrSubjectNotFound", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRevoke(t *testing.T) {
|
||||
service := newTestService(t)
|
||||
ctx := context.Background()
|
||||
|
||||
owner := mustUser(t, service, "owner")
|
||||
alice := mustUser(t, service, "alice")
|
||||
documentID := mustDocument(t, service, owner.ID, nil, "Photos", "", true)
|
||||
|
||||
if _, err := service.Grant(ctx, GrantParams{
|
||||
DocumentID: documentID,
|
||||
SubjectID: alice.ID,
|
||||
Permissions: PermissionRead,
|
||||
GrantedBy: owner.ID,
|
||||
}); err != nil {
|
||||
t.Fatalf("Grant : %v", err)
|
||||
}
|
||||
|
||||
if err := service.Revoke(ctx, documentID, alice.ID); err != nil {
|
||||
t.Fatalf("Revoke : %v", err)
|
||||
}
|
||||
|
||||
entries, err := service.List(ctx, documentID)
|
||||
if err != nil {
|
||||
t.Fatalf("List : %v", err)
|
||||
}
|
||||
if len(entries) != 0 {
|
||||
t.Fatalf("entrées = %d, attendu 0", len(entries))
|
||||
}
|
||||
|
||||
if err := service.Revoke(ctx, documentID, alice.ID); !errors.Is(err, ErrEntryNotFound) {
|
||||
t.Fatalf("erreur = %v, attendu ErrEntryNotFound", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRevokeThenGrantAgain(t *testing.T) {
|
||||
service := newTestService(t)
|
||||
ctx := context.Background()
|
||||
|
||||
owner := mustUser(t, service, "owner")
|
||||
alice := mustUser(t, service, "alice")
|
||||
documentID := mustDocument(t, service, owner.ID, nil, "Photos", "", true)
|
||||
|
||||
if _, err := service.Grant(ctx, GrantParams{
|
||||
DocumentID: documentID,
|
||||
SubjectID: alice.ID,
|
||||
Permissions: PermissionRead,
|
||||
GrantedBy: owner.ID,
|
||||
}); err != nil {
|
||||
t.Fatalf("Grant : %v", err)
|
||||
}
|
||||
if err := service.Revoke(ctx, documentID, alice.ID); err != nil {
|
||||
t.Fatalf("Revoke : %v", err)
|
||||
}
|
||||
if _, err := service.Grant(ctx, GrantParams{
|
||||
DocumentID: documentID,
|
||||
SubjectID: alice.ID,
|
||||
Permissions: PermissionWrite,
|
||||
GrantedBy: owner.ID,
|
||||
}); err != nil {
|
||||
t.Fatalf("Grant après révocation : %v", err)
|
||||
}
|
||||
|
||||
permissions, err := service.Effective(ctx, alice.ID, documentID)
|
||||
if err != nil {
|
||||
t.Fatalf("Effective : %v", err)
|
||||
}
|
||||
if permissions != PermissionWrite {
|
||||
t.Fatalf("permissions = %d, attendu %d", permissions, PermissionWrite)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListIgnoresExpiredEntries(t *testing.T) {
|
||||
service := newTestService(t)
|
||||
ctx := context.Background()
|
||||
|
||||
owner := mustUser(t, service, "owner")
|
||||
alice := mustUser(t, service, "alice")
|
||||
documentID := mustDocument(t, service, owner.ID, nil, "Photos", "", true)
|
||||
|
||||
expiredAt := time.Now().Add(-time.Hour)
|
||||
if _, err := service.Grant(ctx, GrantParams{
|
||||
DocumentID: documentID,
|
||||
SubjectID: alice.ID,
|
||||
Permissions: PermissionRead,
|
||||
ExpiresAt: &expiredAt,
|
||||
GrantedBy: owner.ID,
|
||||
}); err != nil {
|
||||
t.Fatalf("Grant : %v", err)
|
||||
}
|
||||
|
||||
entries, err := service.List(ctx, documentID)
|
||||
if err != nil {
|
||||
t.Fatalf("List : %v", err)
|
||||
}
|
||||
if len(entries) != 0 {
|
||||
t.Fatalf("entrées = %d, attendu 0", len(entries))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
package acl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"mediarr/database"
|
||||
"mediarr/document"
|
||||
"mediarr/ident"
|
||||
"mediarr/identity"
|
||||
)
|
||||
|
||||
var testPolicyID = ident.MustParse("00000000-0000-7000-8000-000000000101")
|
||||
|
||||
func newTestService(t *testing.T) *Service {
|
||||
t.Helper()
|
||||
|
||||
t.Chdir(t.TempDir())
|
||||
|
||||
err, db := database.NewDatabase(database.NewDatabaseParams{})
|
||||
if err != nil {
|
||||
t.Fatalf("NewDatabase : %v", err)
|
||||
}
|
||||
|
||||
t.Cleanup(func() {
|
||||
if err := db.Close(); err != nil {
|
||||
t.Errorf("Close : %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
return NewService(*db)
|
||||
}
|
||||
|
||||
func mustPrincipal(t *testing.T, service *Service, kind, name string) *identity.Principal {
|
||||
t.Helper()
|
||||
|
||||
principal, err := service.identity.CreatePrincipal(context.Background(), identity.CreatePrincipalParams{
|
||||
Kind: kind,
|
||||
Name: name,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("CreatePrincipal(%q) : %v", name, err)
|
||||
}
|
||||
|
||||
return principal
|
||||
}
|
||||
|
||||
func mustUser(t *testing.T, service *Service, name string) *identity.Principal {
|
||||
return mustPrincipal(t, service, identity.KindUser, name)
|
||||
}
|
||||
|
||||
func mustGroup(t *testing.T, service *Service, name string) *identity.Principal {
|
||||
return mustPrincipal(t, service, identity.KindGroup, name)
|
||||
}
|
||||
|
||||
func addMember(t *testing.T, service *Service, groupID, memberID ident.UUID) {
|
||||
t.Helper()
|
||||
|
||||
if err := service.identity.AddMember(context.Background(), groupID, memberID, identity.RoleMember); err != nil {
|
||||
t.Fatalf("AddMember : %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func disablePrincipal(t *testing.T, service *Service, principalID ident.UUID) {
|
||||
t.Helper()
|
||||
|
||||
_, err := service.db.WriteQueries.UpdatePrincipalStatus(context.Background(), database.UpdatePrincipalStatusParams{
|
||||
Status: identity.StatusDisabled,
|
||||
ID: principalID,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("UpdatePrincipalStatus : %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func mustDocument(t *testing.T, service *Service, ownerID ident.UUID, parentID *ident.UUID, name, visibility string, aclInherit bool) ident.UUID {
|
||||
t.Helper()
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
id, err := ident.NewV7()
|
||||
if err != nil {
|
||||
t.Fatalf("NewV7 : %v", err)
|
||||
}
|
||||
|
||||
if visibility == "" {
|
||||
visibility = document.VisibilityPrivate
|
||||
}
|
||||
|
||||
kind := "library"
|
||||
path := hex.EncodeToString(id[:]) + "/"
|
||||
depth := int64(0)
|
||||
|
||||
var parentValue any
|
||||
var policy any = testPolicyID
|
||||
var visibilityValue any = visibility
|
||||
|
||||
if parentID != nil {
|
||||
parent, err := service.db.Queries.GetDocument(ctx, *parentID)
|
||||
if err != nil {
|
||||
t.Fatalf("GetDocument(parent) : %v", err)
|
||||
}
|
||||
|
||||
kind = "folder"
|
||||
path = parent.Path + hex.EncodeToString(id[:]) + "/"
|
||||
depth = parent.Depth + 1
|
||||
parentValue = *parentID
|
||||
policy = nil
|
||||
visibilityValue = nil
|
||||
}
|
||||
|
||||
now := time.Now().UnixMilli()
|
||||
|
||||
_, err = service.db.WriteDB.ExecContext(ctx, `
|
||||
INSERT INTO document (
|
||||
id, kind, owner_id, created_by, parent_id, name, name_key, path, depth,
|
||||
policy_id, visibility, acl_inherit, created_at, updated_at
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
id,
|
||||
kind,
|
||||
ownerID,
|
||||
ownerID,
|
||||
parentValue,
|
||||
name,
|
||||
strings.ToLower(name),
|
||||
path,
|
||||
depth,
|
||||
policy,
|
||||
visibilityValue,
|
||||
boolToInt(aclInherit),
|
||||
now,
|
||||
now,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("insertion du document %q : %v", name, err)
|
||||
}
|
||||
|
||||
return id
|
||||
}
|
||||
@@ -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 ""
|
||||
}
|
||||
@@ -0,0 +1,424 @@
|
||||
package acl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"mediarr/document"
|
||||
"mediarr/ident"
|
||||
"mediarr/identity"
|
||||
)
|
||||
|
||||
func TestPermissionValidAndHas(t *testing.T) {
|
||||
if !PermissionAll.Valid() {
|
||||
t.Fatal("PermissionAll devrait être valide")
|
||||
}
|
||||
if Permission(0).Valid() {
|
||||
t.Fatal("Permission(0) devrait être invalide")
|
||||
}
|
||||
if (PermissionAll + 1).Valid() {
|
||||
t.Fatal("PermissionAll+1 devrait être invalide")
|
||||
}
|
||||
|
||||
if !PermissionAdmin.Has(PermissionRead) {
|
||||
t.Fatal("PermissionAdmin devrait impliquer PermissionRead")
|
||||
}
|
||||
if !PermissionAll.Has(PermissionManage) {
|
||||
t.Fatal("PermissionAll devrait contenir PermissionManage")
|
||||
}
|
||||
if PermissionRead.Has(PermissionWrite) {
|
||||
t.Fatal("PermissionRead ne devrait pas impliquer PermissionWrite")
|
||||
}
|
||||
if PermissionAdmin.Has(PermissionAll + 1) {
|
||||
t.Fatal("une permission requise invalide ne devrait pas être satisfaite")
|
||||
}
|
||||
if !Permission(0).Has(0) {
|
||||
t.Fatal("aucune permission requise devrait toujours être satisfaite")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOwnerHasAllPermissions(t *testing.T) {
|
||||
service := newTestService(t)
|
||||
ctx := context.Background()
|
||||
|
||||
owner := mustUser(t, service, "owner")
|
||||
documentID := mustDocument(t, service, owner.ID, nil, "Photos", "", true)
|
||||
|
||||
permissions, err := service.Effective(ctx, owner.ID, documentID)
|
||||
if err != nil {
|
||||
t.Fatalf("Effective : %v", err)
|
||||
}
|
||||
if permissions != PermissionAll {
|
||||
t.Fatalf("permissions = %d, attendu %d", permissions, PermissionAll)
|
||||
}
|
||||
|
||||
allowed, err := service.Can(ctx, owner.ID, documentID, PermissionAdmin)
|
||||
if err != nil {
|
||||
t.Fatalf("Can : %v", err)
|
||||
}
|
||||
if !allowed {
|
||||
t.Fatal("le propriétaire devrait avoir PermissionAdmin")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEffectiveDirectAndGroup(t *testing.T) {
|
||||
service := newTestService(t)
|
||||
ctx := context.Background()
|
||||
|
||||
owner := mustUser(t, service, "owner")
|
||||
alice := mustUser(t, service, "alice")
|
||||
group := mustGroup(t, service, "family")
|
||||
documentID := mustDocument(t, service, owner.ID, nil, "Photos", "", true)
|
||||
|
||||
addMember(t, service, group.ID, alice.ID)
|
||||
|
||||
if _, err := service.Grant(ctx, GrantParams{
|
||||
DocumentID: documentID,
|
||||
SubjectID: group.ID,
|
||||
Permissions: PermissionRead,
|
||||
GrantedBy: owner.ID,
|
||||
}); err != nil {
|
||||
t.Fatalf("Grant(groupe) : %v", err)
|
||||
}
|
||||
if _, err := service.Grant(ctx, GrantParams{
|
||||
DocumentID: documentID,
|
||||
SubjectID: alice.ID,
|
||||
Permissions: PermissionWrite,
|
||||
GrantedBy: owner.ID,
|
||||
}); err != nil {
|
||||
t.Fatalf("Grant(alice) : %v", err)
|
||||
}
|
||||
|
||||
permissions, err := service.Effective(ctx, alice.ID, documentID)
|
||||
if err != nil {
|
||||
t.Fatalf("Effective : %v", err)
|
||||
}
|
||||
if permissions != PermissionRead|PermissionWrite {
|
||||
t.Fatalf("permissions = %d, attendu %d", permissions, PermissionRead|PermissionWrite)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEffectiveDisabledGroup(t *testing.T) {
|
||||
service := newTestService(t)
|
||||
ctx := context.Background()
|
||||
|
||||
owner := mustUser(t, service, "owner")
|
||||
alice := mustUser(t, service, "alice")
|
||||
group := mustGroup(t, service, "family")
|
||||
documentID := mustDocument(t, service, owner.ID, nil, "Photos", "", true)
|
||||
|
||||
addMember(t, service, group.ID, alice.ID)
|
||||
|
||||
if _, err := service.Grant(ctx, GrantParams{
|
||||
DocumentID: documentID,
|
||||
SubjectID: group.ID,
|
||||
Permissions: PermissionRead,
|
||||
GrantedBy: owner.ID,
|
||||
}); err != nil {
|
||||
t.Fatalf("Grant : %v", err)
|
||||
}
|
||||
|
||||
disablePrincipal(t, service, group.ID)
|
||||
|
||||
permissions, err := service.Effective(ctx, alice.ID, documentID)
|
||||
if err != nil {
|
||||
t.Fatalf("Effective : %v", err)
|
||||
}
|
||||
if permissions != 0 {
|
||||
t.Fatalf("permissions = %d, attendu 0", permissions)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEffectiveAdminImpliesAll(t *testing.T) {
|
||||
service := newTestService(t)
|
||||
ctx := context.Background()
|
||||
|
||||
owner := mustUser(t, service, "owner")
|
||||
alice := mustUser(t, service, "alice")
|
||||
documentID := mustDocument(t, service, owner.ID, nil, "Photos", "", true)
|
||||
|
||||
if _, err := service.Grant(ctx, GrantParams{
|
||||
DocumentID: documentID,
|
||||
SubjectID: alice.ID,
|
||||
Permissions: PermissionAdmin,
|
||||
GrantedBy: owner.ID,
|
||||
}); err != nil {
|
||||
t.Fatalf("Grant : %v", err)
|
||||
}
|
||||
|
||||
allowed, err := service.Can(ctx, alice.ID, documentID, PermissionRead|PermissionDelete)
|
||||
if err != nil {
|
||||
t.Fatalf("Can : %v", err)
|
||||
}
|
||||
if !allowed {
|
||||
t.Fatal("PermissionAdmin devrait impliquer toutes les permissions")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEffectiveInheritance(t *testing.T) {
|
||||
service := newTestService(t)
|
||||
ctx := context.Background()
|
||||
|
||||
owner := mustUser(t, service, "owner")
|
||||
alice := mustUser(t, service, "alice")
|
||||
|
||||
root := mustDocument(t, service, owner.ID, nil, "Root", "", true)
|
||||
folder := mustDocument(t, service, owner.ID, &root, "Folder", "", true)
|
||||
child := mustDocument(t, service, owner.ID, &folder, "Child", "", true)
|
||||
|
||||
if _, err := service.Grant(ctx, GrantParams{
|
||||
DocumentID: root,
|
||||
SubjectID: alice.ID,
|
||||
Permissions: PermissionRead,
|
||||
Propagate: true,
|
||||
GrantedBy: owner.ID,
|
||||
}); err != nil {
|
||||
t.Fatalf("Grant(root) : %v", err)
|
||||
}
|
||||
if _, err := service.Grant(ctx, GrantParams{
|
||||
DocumentID: folder,
|
||||
SubjectID: alice.ID,
|
||||
Permissions: PermissionWrite,
|
||||
Propagate: true,
|
||||
GrantedBy: owner.ID,
|
||||
}); err != nil {
|
||||
t.Fatalf("Grant(folder) : %v", err)
|
||||
}
|
||||
if _, err := service.Grant(ctx, GrantParams{
|
||||
DocumentID: child,
|
||||
SubjectID: alice.ID,
|
||||
Permissions: PermissionDelete,
|
||||
GrantedBy: owner.ID,
|
||||
}); err != nil {
|
||||
t.Fatalf("Grant(child) : %v", err)
|
||||
}
|
||||
|
||||
permissions, err := service.Effective(ctx, alice.ID, child)
|
||||
if err != nil {
|
||||
t.Fatalf("Effective : %v", err)
|
||||
}
|
||||
expected := PermissionRead | PermissionWrite | PermissionDelete
|
||||
if permissions != expected {
|
||||
t.Fatalf("permissions = %d, attendu %d", permissions, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEffectivePropagationDisabled(t *testing.T) {
|
||||
service := newTestService(t)
|
||||
ctx := context.Background()
|
||||
|
||||
owner := mustUser(t, service, "owner")
|
||||
alice := mustUser(t, service, "alice")
|
||||
|
||||
root := mustDocument(t, service, owner.ID, nil, "Root", "", true)
|
||||
child := mustDocument(t, service, owner.ID, &root, "Child", "", true)
|
||||
|
||||
if _, err := service.Grant(ctx, GrantParams{
|
||||
DocumentID: root,
|
||||
SubjectID: alice.ID,
|
||||
Permissions: PermissionRead,
|
||||
Propagate: false,
|
||||
GrantedBy: owner.ID,
|
||||
}); err != nil {
|
||||
t.Fatalf("Grant : %v", err)
|
||||
}
|
||||
|
||||
permissions, err := service.Effective(ctx, alice.ID, child)
|
||||
if err != nil {
|
||||
t.Fatalf("Effective : %v", err)
|
||||
}
|
||||
if permissions != 0 {
|
||||
t.Fatalf("permissions = %d, attendu 0", permissions)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEffectiveAclInheritBreak(t *testing.T) {
|
||||
service := newTestService(t)
|
||||
ctx := context.Background()
|
||||
|
||||
owner := mustUser(t, service, "owner")
|
||||
alice := mustUser(t, service, "alice")
|
||||
|
||||
root := mustDocument(t, service, owner.ID, nil, "Root", "", true)
|
||||
middle := mustDocument(t, service, owner.ID, &root, "Middle", "", false)
|
||||
leaf := mustDocument(t, service, owner.ID, &middle, "Leaf", "", true)
|
||||
|
||||
if _, err := service.Grant(ctx, GrantParams{
|
||||
DocumentID: root,
|
||||
SubjectID: alice.ID,
|
||||
Permissions: PermissionRead,
|
||||
Propagate: true,
|
||||
GrantedBy: owner.ID,
|
||||
}); err != nil {
|
||||
t.Fatalf("Grant(root) : %v", err)
|
||||
}
|
||||
|
||||
permissions, err := service.Effective(ctx, alice.ID, leaf)
|
||||
if err != nil {
|
||||
t.Fatalf("Effective : %v", err)
|
||||
}
|
||||
if permissions != 0 {
|
||||
t.Fatalf("permissions = %d, attendu 0", permissions)
|
||||
}
|
||||
|
||||
if _, err := service.Grant(ctx, GrantParams{
|
||||
DocumentID: leaf,
|
||||
SubjectID: alice.ID,
|
||||
Permissions: PermissionWrite,
|
||||
GrantedBy: owner.ID,
|
||||
}); err != nil {
|
||||
t.Fatalf("Grant(leaf) : %v", err)
|
||||
}
|
||||
|
||||
permissions, err = service.Effective(ctx, alice.ID, leaf)
|
||||
if err != nil {
|
||||
t.Fatalf("Effective après grant direct : %v", err)
|
||||
}
|
||||
if permissions != PermissionWrite {
|
||||
t.Fatalf("permissions = %d, attendu %d", permissions, PermissionWrite)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEffectiveExpiredAndRevoked(t *testing.T) {
|
||||
service := newTestService(t)
|
||||
ctx := context.Background()
|
||||
|
||||
owner := mustUser(t, service, "owner")
|
||||
alice := mustUser(t, service, "alice")
|
||||
documentID := mustDocument(t, service, owner.ID, nil, "Photos", "", true)
|
||||
|
||||
expiredAt := time.Now().Add(-time.Hour)
|
||||
if _, err := service.Grant(ctx, GrantParams{
|
||||
DocumentID: documentID,
|
||||
SubjectID: alice.ID,
|
||||
Permissions: PermissionRead,
|
||||
ExpiresAt: &expiredAt,
|
||||
GrantedBy: owner.ID,
|
||||
}); err != nil {
|
||||
t.Fatalf("Grant expiré : %v", err)
|
||||
}
|
||||
|
||||
permissions, err := service.Effective(ctx, alice.ID, documentID)
|
||||
if err != nil {
|
||||
t.Fatalf("Effective : %v", err)
|
||||
}
|
||||
if permissions != 0 {
|
||||
t.Fatalf("permissions = %d, attendu 0", permissions)
|
||||
}
|
||||
|
||||
future := time.Now().Add(time.Hour)
|
||||
if _, err := service.Grant(ctx, GrantParams{
|
||||
DocumentID: documentID,
|
||||
SubjectID: alice.ID,
|
||||
Permissions: PermissionWrite,
|
||||
ExpiresAt: &future,
|
||||
GrantedBy: owner.ID,
|
||||
}); err != nil {
|
||||
t.Fatalf("Grant futur : %v", err)
|
||||
}
|
||||
|
||||
permissions, err = service.Effective(ctx, alice.ID, documentID)
|
||||
if err != nil {
|
||||
t.Fatalf("Effective : %v", err)
|
||||
}
|
||||
if permissions != PermissionWrite {
|
||||
t.Fatalf("permissions = %d, attendu %d", permissions, PermissionWrite)
|
||||
}
|
||||
|
||||
if err := service.Revoke(ctx, documentID, alice.ID); err != nil {
|
||||
t.Fatalf("Revoke : %v", err)
|
||||
}
|
||||
|
||||
permissions, err = service.Effective(ctx, alice.ID, documentID)
|
||||
if err != nil {
|
||||
t.Fatalf("Effective après révocation : %v", err)
|
||||
}
|
||||
if permissions != 0 {
|
||||
t.Fatalf("permissions = %d, attendu 0", permissions)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEffectiveVisibility(t *testing.T) {
|
||||
service := newTestService(t)
|
||||
ctx := context.Background()
|
||||
|
||||
owner := mustUser(t, service, "owner")
|
||||
alice := mustUser(t, service, "alice")
|
||||
guest := mustPrincipal(t, service, identity.KindGuest, "jean")
|
||||
system := mustPrincipal(t, service, identity.KindSystem, "system-test")
|
||||
|
||||
clusterDoc := mustDocument(t, service, owner.ID, nil, "Cluster", document.VisibilityCluster, true)
|
||||
privateDoc := mustDocument(t, service, owner.ID, nil, "Private", document.VisibilityPrivate, true)
|
||||
publicDoc := mustDocument(t, service, owner.ID, nil, "Public", document.VisibilityPublic, true)
|
||||
federatedDoc := mustDocument(t, service, owner.ID, nil, "Federated", document.VisibilityFederated, true)
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
principalID ident.UUID
|
||||
documentID ident.UUID
|
||||
expected Permission
|
||||
}{
|
||||
{"cluster/user", alice.ID, clusterDoc, PermissionRead},
|
||||
{"cluster/guest", guest.ID, clusterDoc, PermissionRead},
|
||||
{"cluster/system", system.ID, clusterDoc, 0},
|
||||
{"private/user", alice.ID, privateDoc, 0},
|
||||
{"public/system", system.ID, publicDoc, PermissionRead},
|
||||
{"federated/user", alice.ID, federatedDoc, 0},
|
||||
}
|
||||
|
||||
for _, testCase := range cases {
|
||||
permissions, err := service.Effective(ctx, testCase.principalID, testCase.documentID)
|
||||
if err != nil {
|
||||
t.Fatalf("%s : Effective : %v", testCase.name, err)
|
||||
}
|
||||
if permissions != testCase.expected {
|
||||
t.Fatalf("%s : permissions = %d, attendu %d", testCase.name, permissions, testCase.expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEffectiveInactivePrincipal(t *testing.T) {
|
||||
service := newTestService(t)
|
||||
ctx := context.Background()
|
||||
|
||||
owner := mustUser(t, service, "owner")
|
||||
alice := mustUser(t, service, "alice")
|
||||
documentID := mustDocument(t, service, owner.ID, nil, "Photos", "", true)
|
||||
|
||||
if _, err := service.Grant(ctx, GrantParams{
|
||||
DocumentID: documentID,
|
||||
SubjectID: alice.ID,
|
||||
Permissions: PermissionRead,
|
||||
GrantedBy: owner.ID,
|
||||
}); err != nil {
|
||||
t.Fatalf("Grant : %v", err)
|
||||
}
|
||||
|
||||
disablePrincipal(t, service, alice.ID)
|
||||
|
||||
permissions, err := service.Effective(ctx, alice.ID, documentID)
|
||||
if err != nil {
|
||||
t.Fatalf("Effective : %v", err)
|
||||
}
|
||||
if permissions != 0 {
|
||||
t.Fatalf("permissions = %d, attendu 0", permissions)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEffectiveMissingDocument(t *testing.T) {
|
||||
service := newTestService(t)
|
||||
ctx := context.Background()
|
||||
|
||||
alice := mustUser(t, service, "alice")
|
||||
missing, err := ident.NewV7()
|
||||
if err != nil {
|
||||
t.Fatalf("NewV7 : %v", err)
|
||||
}
|
||||
|
||||
_, err = service.Effective(ctx, alice.ID, missing)
|
||||
if !errors.Is(err, ErrDocumentNotFound) {
|
||||
t.Fatalf("erreur = %v, attendu ErrDocumentNotFound", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package acl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"mediarr/database"
|
||||
"mediarr/ident"
|
||||
"mediarr/identity"
|
||||
)
|
||||
|
||||
const maxDocumentDepth = 256
|
||||
|
||||
type Service struct {
|
||||
db database.DatabaseClient
|
||||
identity *identity.Service
|
||||
}
|
||||
|
||||
func NewService(db database.DatabaseClient) *Service {
|
||||
return &Service{
|
||||
db: db,
|
||||
identity: identity.NewService(db),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) documentChain(ctx context.Context, documentID ident.UUID) ([]database.Document, error) {
|
||||
chain := make([]database.Document, 0, 8)
|
||||
seen := make(map[ident.UUID]struct{})
|
||||
current := documentID
|
||||
|
||||
for {
|
||||
if _, exists := seen[current]; exists {
|
||||
return nil, ErrDocumentCycle
|
||||
}
|
||||
seen[current] = struct{}{}
|
||||
|
||||
document, err := s.db.Queries.GetDocument(ctx, current)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, fmt.Errorf("%w: %s", ErrDocumentNotFound, current)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
chain = append(chain, document)
|
||||
if document.ParentID == nil {
|
||||
return chain, nil
|
||||
}
|
||||
if len(chain) >= maxDocumentDepth {
|
||||
return nil, ErrDocumentCycle
|
||||
}
|
||||
|
||||
current = *document.ParentID
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) loadSubject(ctx context.Context, subjectID ident.UUID) (*identity.Principal, error) {
|
||||
subject, err := s.identity.GetPrincipal(ctx, subjectID)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, fmt.Errorf("%w: %s", ErrSubjectNotFound, subjectID)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if subject.Status != identity.StatusActive {
|
||||
return nil, fmt.Errorf("%w: %s", ErrSubjectNotFound, subjectID)
|
||||
}
|
||||
|
||||
return subject, nil
|
||||
}
|
||||
+196
-2
@@ -398,6 +398,45 @@ func (q *Queries) GetClusterPrincipal(ctx context.Context) (Principal, error) {
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getDocument = `-- name: GetDocument :one
|
||||
|
||||
SELECT id, kind, owner_id, created_by, parent_id, name, name_key, path, depth, position, head_version_id, metadata, policy_id, visibility, acl_inherit, rev, created_at, updated_at, deleted_at, deleted_by, trash_batch
|
||||
FROM document
|
||||
WHERE id = ?1
|
||||
`
|
||||
|
||||
// ============================================================================
|
||||
// document
|
||||
// ============================================================================
|
||||
func (q *Queries) GetDocument(ctx context.Context, id ident.UUID) (Document, error) {
|
||||
row := q.db.QueryRowContext(ctx, getDocument, id)
|
||||
var i Document
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Kind,
|
||||
&i.OwnerID,
|
||||
&i.CreatedBy,
|
||||
&i.ParentID,
|
||||
&i.Name,
|
||||
&i.NameKey,
|
||||
&i.Path,
|
||||
&i.Depth,
|
||||
&i.Position,
|
||||
&i.HeadVersionID,
|
||||
&i.Metadata,
|
||||
&i.PolicyID,
|
||||
&i.Visibility,
|
||||
&i.AclInherit,
|
||||
&i.Rev,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.DeletedAt,
|
||||
&i.DeletedBy,
|
||||
&i.TrashBatch,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getMembership = `-- name: GetMembership :one
|
||||
SELECT group_id, member_id, role, created_at
|
||||
FROM membership
|
||||
@@ -572,6 +611,53 @@ func (q *Queries) IncrementFailedAttempts(ctx context.Context, principalID ident
|
||||
return i, err
|
||||
}
|
||||
|
||||
const listActiveACLEntriesForDocument = `-- name: ListActiveACLEntriesForDocument :many
|
||||
SELECT id, document_id, subject_id, permissions, propagate, expires_at, granted_by, created_at, revoked_at
|
||||
FROM acl_entry
|
||||
WHERE document_id = ?1
|
||||
AND revoked_at IS NULL
|
||||
AND (expires_at IS NULL OR expires_at > CAST(?2 AS INTEGER))
|
||||
ORDER BY created_at
|
||||
`
|
||||
|
||||
type ListActiveACLEntriesForDocumentParams struct {
|
||||
DocumentID ident.UUID
|
||||
Now int64
|
||||
}
|
||||
|
||||
func (q *Queries) ListActiveACLEntriesForDocument(ctx context.Context, arg ListActiveACLEntriesForDocumentParams) ([]AclEntry, error) {
|
||||
rows, err := q.db.QueryContext(ctx, listActiveACLEntriesForDocument, arg.DocumentID, arg.Now)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []AclEntry
|
||||
for rows.Next() {
|
||||
var i AclEntry
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.DocumentID,
|
||||
&i.SubjectID,
|
||||
&i.Permissions,
|
||||
&i.Propagate,
|
||||
&i.ExpiresAt,
|
||||
&i.GrantedBy,
|
||||
&i.CreatedAt,
|
||||
&i.RevokedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listActivePrincipals = `-- name: ListActivePrincipals :many
|
||||
SELECT id, kind, name, public_key, status, successor_id, created_at, disabled_at
|
||||
FROM principal
|
||||
@@ -735,7 +821,7 @@ func (q *Queries) ListMemberGroups(ctx context.Context, memberID ident.UUID) ([]
|
||||
}
|
||||
|
||||
const listMemberGroupsWithRole = `-- name: ListMemberGroupsWithRole :many
|
||||
SELECT p.id, p.name, m.role
|
||||
SELECT p.id, p.name, p.status, m.role
|
||||
FROM membership m
|
||||
JOIN principal p ON p.id = m.group_id
|
||||
WHERE m.member_id = ?1
|
||||
@@ -745,6 +831,7 @@ ORDER BY p.name
|
||||
type ListMemberGroupsWithRoleRow struct {
|
||||
ID ident.UUID
|
||||
Name string
|
||||
Status string
|
||||
Role string
|
||||
}
|
||||
|
||||
@@ -757,7 +844,12 @@ func (q *Queries) ListMemberGroupsWithRole(ctx context.Context, memberID ident.U
|
||||
var items []ListMemberGroupsWithRoleRow
|
||||
for rows.Next() {
|
||||
var i ListMemberGroupsWithRoleRow
|
||||
if err := rows.Scan(&i.ID, &i.Name, &i.Role); err != nil {
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
&i.Status,
|
||||
&i.Role,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
@@ -1093,6 +1185,38 @@ func (q *Queries) ResetFailedAttempts(ctx context.Context, principalID ident.UUI
|
||||
return i, err
|
||||
}
|
||||
|
||||
const revokeACLEntry = `-- name: RevokeACLEntry :one
|
||||
UPDATE acl_entry
|
||||
SET revoked_at = ?1
|
||||
WHERE document_id = ?2
|
||||
AND subject_id = ?3
|
||||
AND revoked_at IS NULL
|
||||
RETURNING id, document_id, subject_id, permissions, propagate, expires_at, granted_by, created_at, revoked_at
|
||||
`
|
||||
|
||||
type RevokeACLEntryParams struct {
|
||||
RevokedAt sql.NullInt64
|
||||
DocumentID ident.UUID
|
||||
SubjectID ident.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) RevokeACLEntry(ctx context.Context, arg RevokeACLEntryParams) (AclEntry, error) {
|
||||
row := q.db.QueryRowContext(ctx, revokeACLEntry, arg.RevokedAt, arg.DocumentID, arg.SubjectID)
|
||||
var i AclEntry
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.DocumentID,
|
||||
&i.SubjectID,
|
||||
&i.Permissions,
|
||||
&i.Propagate,
|
||||
&i.ExpiresAt,
|
||||
&i.GrantedBy,
|
||||
&i.CreatedAt,
|
||||
&i.RevokedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const revokeSession = `-- name: RevokeSession :one
|
||||
UPDATE session
|
||||
SET revoked_at = ?1
|
||||
@@ -1425,3 +1549,73 @@ func (q *Queries) UpdatePrincipalSuccessor(ctx context.Context, arg UpdatePrinci
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const upsertACLEntry = `-- name: UpsertACLEntry :one
|
||||
|
||||
INSERT INTO acl_entry (
|
||||
id,
|
||||
document_id,
|
||||
subject_id,
|
||||
permissions,
|
||||
propagate,
|
||||
expires_at,
|
||||
granted_by,
|
||||
created_at
|
||||
) VALUES (
|
||||
?1,
|
||||
?2,
|
||||
?3,
|
||||
?4,
|
||||
?5,
|
||||
?6,
|
||||
?7,
|
||||
?8
|
||||
)
|
||||
ON CONFLICT(document_id, subject_id) DO UPDATE SET
|
||||
permissions = excluded.permissions,
|
||||
propagate = excluded.propagate,
|
||||
expires_at = excluded.expires_at,
|
||||
granted_by = excluded.granted_by,
|
||||
revoked_at = NULL
|
||||
RETURNING id, document_id, subject_id, permissions, propagate, expires_at, granted_by, created_at, revoked_at
|
||||
`
|
||||
|
||||
type UpsertACLEntryParams struct {
|
||||
ID ident.UUID
|
||||
DocumentID ident.UUID
|
||||
SubjectID ident.UUID
|
||||
Permissions int64
|
||||
Propagate int64
|
||||
ExpiresAt sql.NullInt64
|
||||
GrantedBy ident.UUID
|
||||
CreatedAt int64
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// acl_entry
|
||||
// ============================================================================
|
||||
func (q *Queries) UpsertACLEntry(ctx context.Context, arg UpsertACLEntryParams) (AclEntry, error) {
|
||||
row := q.db.QueryRowContext(ctx, upsertACLEntry,
|
||||
arg.ID,
|
||||
arg.DocumentID,
|
||||
arg.SubjectID,
|
||||
arg.Permissions,
|
||||
arg.Propagate,
|
||||
arg.ExpiresAt,
|
||||
arg.GrantedBy,
|
||||
arg.CreatedAt,
|
||||
)
|
||||
var i AclEntry
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.DocumentID,
|
||||
&i.SubjectID,
|
||||
&i.Permissions,
|
||||
&i.Propagate,
|
||||
&i.ExpiresAt,
|
||||
&i.GrantedBy,
|
||||
&i.CreatedAt,
|
||||
&i.RevokedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
@@ -155,7 +155,7 @@ WHERE group_id = sqlc.arg(group_id)
|
||||
AND member_id = sqlc.arg(member_id);
|
||||
|
||||
-- name: ListMemberGroupsWithRole :many
|
||||
SELECT p.id, p.name, m.role
|
||||
SELECT p.id, p.name, p.status, m.role
|
||||
FROM membership m
|
||||
JOIN principal p ON p.id = m.group_id
|
||||
WHERE m.member_id = sqlc.arg(member_id)
|
||||
@@ -351,3 +351,60 @@ ORDER BY created_at DESC;
|
||||
DELETE FROM session
|
||||
WHERE expires_at <= sqlc.arg(now)
|
||||
OR idle_expires_at <= sqlc.arg(now);
|
||||
|
||||
-- ============================================================================
|
||||
-- document
|
||||
-- ============================================================================
|
||||
|
||||
-- name: GetDocument :one
|
||||
SELECT *
|
||||
FROM document
|
||||
WHERE id = sqlc.arg(id);
|
||||
|
||||
-- ============================================================================
|
||||
-- acl_entry
|
||||
-- ============================================================================
|
||||
|
||||
-- name: UpsertACLEntry :one
|
||||
INSERT INTO acl_entry (
|
||||
id,
|
||||
document_id,
|
||||
subject_id,
|
||||
permissions,
|
||||
propagate,
|
||||
expires_at,
|
||||
granted_by,
|
||||
created_at
|
||||
) VALUES (
|
||||
sqlc.arg(id),
|
||||
sqlc.arg(document_id),
|
||||
sqlc.arg(subject_id),
|
||||
sqlc.arg(permissions),
|
||||
sqlc.arg(propagate),
|
||||
sqlc.narg(expires_at),
|
||||
sqlc.arg(granted_by),
|
||||
sqlc.arg(created_at)
|
||||
)
|
||||
ON CONFLICT(document_id, subject_id) DO UPDATE SET
|
||||
permissions = excluded.permissions,
|
||||
propagate = excluded.propagate,
|
||||
expires_at = excluded.expires_at,
|
||||
granted_by = excluded.granted_by,
|
||||
revoked_at = NULL
|
||||
RETURNING *;
|
||||
|
||||
-- name: RevokeACLEntry :one
|
||||
UPDATE acl_entry
|
||||
SET revoked_at = sqlc.arg(revoked_at)
|
||||
WHERE document_id = sqlc.arg(document_id)
|
||||
AND subject_id = sqlc.arg(subject_id)
|
||||
AND revoked_at IS NULL
|
||||
RETURNING *;
|
||||
|
||||
-- name: ListActiveACLEntriesForDocument :many
|
||||
SELECT *
|
||||
FROM acl_entry
|
||||
WHERE document_id = sqlc.arg(document_id)
|
||||
AND revoked_at IS NULL
|
||||
AND (expires_at IS NULL OR expires_at > CAST(sqlc.arg(now) AS INTEGER))
|
||||
ORDER BY created_at;
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
package document
|
||||
|
||||
const (
|
||||
VisibilityPrivate = "PRIVATE"
|
||||
VisibilityCluster = "CLUSTER"
|
||||
VisibilityFederated = "FEDERATED"
|
||||
VisibilityPublic = "PUBLIC"
|
||||
)
|
||||
|
||||
type Document struct {
|
||||
Kind string
|
||||
Hidden string
|
||||
|
||||
@@ -97,6 +97,7 @@ func (s *Service) ListGroups(ctx context.Context, memberID ident.UUID) ([]Group,
|
||||
groups = append(groups, Group{
|
||||
ID: row.ID,
|
||||
Name: row.Name,
|
||||
Status: row.Status,
|
||||
Role: row.Role,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -37,6 +37,7 @@ type Principal struct {
|
||||
type Group struct {
|
||||
ID ident.UUID
|
||||
Name string
|
||||
Status string
|
||||
Role string
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package node
|
||||
|
||||
import (
|
||||
"mediarr/acl"
|
||||
"mediarr/auth"
|
||||
"mediarr/cluster"
|
||||
"mediarr/database"
|
||||
@@ -59,3 +60,7 @@ func (n *Node) Identity() *identity.Service {
|
||||
func (n *Node) Auth() *auth.Service {
|
||||
return auth.NewService(n.db)
|
||||
}
|
||||
|
||||
func (n *Node) ACL() *acl.Service {
|
||||
return acl.NewService(n.db)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user