acl
This commit is contained in:
+10
-30
@@ -5,42 +5,22 @@ import (
|
||||
"time"
|
||||
|
||||
"mediarr/ident"
|
||||
"mediarr/permission"
|
||||
)
|
||||
|
||||
type Permission int64
|
||||
type Permission = permission.Permission
|
||||
|
||||
const (
|
||||
PermissionRead Permission = 1 << iota
|
||||
PermissionWrite
|
||||
PermissionCreate
|
||||
PermissionDelete
|
||||
PermissionShare
|
||||
PermissionManage
|
||||
PermissionAdmin
|
||||
PermissionRead = permission.Read
|
||||
PermissionWrite = permission.Write
|
||||
PermissionCreate = permission.Create
|
||||
PermissionDelete = permission.Delete
|
||||
PermissionShare = permission.Share
|
||||
PermissionManage = permission.Manage
|
||||
PermissionAdmin = permission.Admin
|
||||
PermissionAll = permission.All
|
||||
)
|
||||
|
||||
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
|
||||
|
||||
@@ -422,3 +422,27 @@ func TestEffectiveMissingDocument(t *testing.T) {
|
||||
t.Fatalf("erreur = %v, attendu ErrDocumentNotFound", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsActive(t *testing.T) {
|
||||
service := newTestService(t)
|
||||
ctx := context.Background()
|
||||
alice := mustUser(t, service, "alice")
|
||||
|
||||
active, err := service.IsActive(ctx, alice.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("IsActive : %v", err)
|
||||
}
|
||||
if !active {
|
||||
t.Fatal("alice devrait être active")
|
||||
}
|
||||
|
||||
disablePrincipal(t, service, alice.ID)
|
||||
|
||||
active, err = service.IsActive(ctx, alice.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("IsActive après désactivation : %v", err)
|
||||
}
|
||||
if active {
|
||||
t.Fatal("alice ne devrait plus être active")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,3 +70,14 @@ func (s *Service) loadSubject(ctx context.Context, subjectID ident.UUID) (*ident
|
||||
|
||||
return subject, nil
|
||||
}
|
||||
|
||||
func (s *Service) IsActive(ctx context.Context, principalID ident.UUID) (bool, error) {
|
||||
if _, err := s.loadSubject(ctx, principalID); err != nil {
|
||||
if errors.Is(err, ErrSubjectNotFound) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
+17
-2
@@ -9,9 +9,17 @@ import (
|
||||
|
||||
"mediarr/database"
|
||||
"mediarr/ident"
|
||||
"mediarr/permission"
|
||||
)
|
||||
|
||||
func (s *Service) CreateLibrary(ctx context.Context, params CreateLibraryParams) (*Document, error) {
|
||||
func (s *Service) CreateLibrary(ctx context.Context, actorID ident.UUID, params CreateLibraryParams) (*Document, error) {
|
||||
if err := s.requireActive(ctx, actorID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if params.OwnerID != actorID || params.CreatedBy != actorID {
|
||||
return nil, fmt.Errorf("%w: owner_id et created_by doivent être l'acteur", ErrPermissionDenied)
|
||||
}
|
||||
|
||||
name, nameKey, err := normalizeName(params.Name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -64,7 +72,11 @@ func (s *Service) CreateLibrary(ctx context.Context, params CreateLibraryParams)
|
||||
return documentFromDB(created), nil
|
||||
}
|
||||
|
||||
func (s *Service) CreateFolder(ctx context.Context, params CreateFolderParams) (*Document, error) {
|
||||
func (s *Service) CreateFolder(ctx context.Context, actorID ident.UUID, params CreateFolderParams) (*Document, error) {
|
||||
if params.OwnerID != actorID || params.CreatedBy != actorID {
|
||||
return nil, fmt.Errorf("%w: owner_id et created_by doivent être l'acteur", ErrPermissionDenied)
|
||||
}
|
||||
|
||||
name, nameKey, err := normalizeName(params.Name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -83,6 +95,9 @@ func (s *Service) CreateFolder(ctx context.Context, params CreateFolderParams) (
|
||||
if !isContainerKind(parent.Kind) {
|
||||
return nil, fmt.Errorf("%w: %s", ErrInvalidParent, parent.Kind)
|
||||
}
|
||||
if err := s.can(ctx, actorID, params.ParentID, permission.Create); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
id, err := ident.NewV7()
|
||||
if err != nil {
|
||||
|
||||
+12
-12
@@ -15,7 +15,7 @@ func TestCreateLibrary(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
owner := mustUser(t, service, "owner")
|
||||
|
||||
library, err := service.CreateLibrary(ctx, CreateLibraryParams{
|
||||
library, err := service.CreateLibrary(ctx, owner.ID, CreateLibraryParams{
|
||||
OwnerID: owner.ID,
|
||||
CreatedBy: owner.ID,
|
||||
Name: "Photos",
|
||||
@@ -66,7 +66,7 @@ func TestCreateLibraryExplicitOptions(t *testing.T) {
|
||||
owner := mustUser(t, service, "owner")
|
||||
aclInherit := false
|
||||
|
||||
library, err := service.CreateLibrary(ctx, CreateLibraryParams{
|
||||
library, err := service.CreateLibrary(ctx, owner.ID, CreateLibraryParams{
|
||||
OwnerID: owner.ID,
|
||||
CreatedBy: owner.ID,
|
||||
Name: "Films",
|
||||
@@ -97,7 +97,7 @@ func TestCreateLibraryRejectsInvalidName(t *testing.T) {
|
||||
|
||||
cases := []string{"", " ", "a/b", strings.Repeat("a", 256)}
|
||||
for _, name := range cases {
|
||||
_, err := service.CreateLibrary(ctx, CreateLibraryParams{
|
||||
_, err := service.CreateLibrary(ctx, owner.ID, CreateLibraryParams{
|
||||
OwnerID: owner.ID,
|
||||
CreatedBy: owner.ID,
|
||||
Name: name,
|
||||
@@ -114,7 +114,7 @@ func TestCreateLibraryRejectsInvalidVisibility(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
owner := mustUser(t, service, "owner")
|
||||
|
||||
_, err := service.CreateLibrary(ctx, CreateLibraryParams{
|
||||
_, err := service.CreateLibrary(ctx, owner.ID, CreateLibraryParams{
|
||||
OwnerID: owner.ID,
|
||||
CreatedBy: owner.ID,
|
||||
Name: "Photos",
|
||||
@@ -135,7 +135,7 @@ func TestCreateLibraryRejectsMissingPolicy(t *testing.T) {
|
||||
t.Fatalf("NewV7 : %v", err)
|
||||
}
|
||||
|
||||
_, err = service.CreateLibrary(ctx, CreateLibraryParams{
|
||||
_, err = service.CreateLibrary(ctx, owner.ID, CreateLibraryParams{
|
||||
OwnerID: owner.ID,
|
||||
CreatedBy: owner.ID,
|
||||
Name: "Photos",
|
||||
@@ -153,7 +153,7 @@ func TestCreateLibraryRejectsDuplicateName(t *testing.T) {
|
||||
|
||||
mustLibrary(t, service, owner, "Photos")
|
||||
|
||||
_, err := service.CreateLibrary(ctx, CreateLibraryParams{
|
||||
_, err := service.CreateLibrary(ctx, owner.ID, CreateLibraryParams{
|
||||
OwnerID: owner.ID,
|
||||
CreatedBy: owner.ID,
|
||||
Name: "photos",
|
||||
@@ -170,7 +170,7 @@ func TestCreateFolder(t *testing.T) {
|
||||
owner := mustUser(t, service, "owner")
|
||||
|
||||
library := mustLibrary(t, service, owner, "Photos")
|
||||
folder, err := service.CreateFolder(ctx, CreateFolderParams{
|
||||
folder, err := service.CreateFolder(ctx, owner.ID, CreateFolderParams{
|
||||
OwnerID: owner.ID,
|
||||
CreatedBy: owner.ID,
|
||||
ParentID: library.ID,
|
||||
@@ -212,7 +212,7 @@ func TestCreateFolderRejectsMissingParent(t *testing.T) {
|
||||
t.Fatalf("NewV7 : %v", err)
|
||||
}
|
||||
|
||||
_, err = service.CreateFolder(ctx, CreateFolderParams{
|
||||
_, err = service.CreateFolder(ctx, owner.ID, CreateFolderParams{
|
||||
OwnerID: owner.ID,
|
||||
CreatedBy: owner.ID,
|
||||
ParentID: missing,
|
||||
@@ -229,11 +229,11 @@ func TestCreateFolderRejectsDeletedParent(t *testing.T) {
|
||||
owner := mustUser(t, service, "owner")
|
||||
library := mustLibrary(t, service, owner, "Photos")
|
||||
|
||||
if _, err := service.Trash(ctx, library.ID, owner.ID); err != nil {
|
||||
if _, err := service.Trash(ctx, owner.ID, library.ID); err != nil {
|
||||
t.Fatalf("Trash : %v", err)
|
||||
}
|
||||
|
||||
_, err := service.CreateFolder(ctx, CreateFolderParams{
|
||||
_, err := service.CreateFolder(ctx, owner.ID, CreateFolderParams{
|
||||
OwnerID: owner.ID,
|
||||
CreatedBy: owner.ID,
|
||||
ParentID: library.ID,
|
||||
@@ -251,7 +251,7 @@ func TestCreateFolderRejectsInvalidParent(t *testing.T) {
|
||||
library := mustLibrary(t, service, owner, "Photos")
|
||||
movie := mustKindDocument(t, service, owner, library, "movie", "Film")
|
||||
|
||||
_, err := service.CreateFolder(ctx, CreateFolderParams{
|
||||
_, err := service.CreateFolder(ctx, owner.ID, CreateFolderParams{
|
||||
OwnerID: owner.ID,
|
||||
CreatedBy: owner.ID,
|
||||
ParentID: movie.ID,
|
||||
@@ -270,7 +270,7 @@ func TestCreateFolderRejectsDuplicateSibling(t *testing.T) {
|
||||
|
||||
mustFolder(t, service, owner, library.ID, "Vacances")
|
||||
|
||||
_, err := service.CreateFolder(ctx, CreateFolderParams{
|
||||
_, err := service.CreateFolder(ctx, owner.ID, CreateFolderParams{
|
||||
OwnerID: owner.ID,
|
||||
CreatedBy: owner.ID,
|
||||
ParentID: library.ID,
|
||||
|
||||
@@ -74,4 +74,5 @@ var (
|
||||
ErrInvalidVisibility = errors.New("visibilité invalide")
|
||||
ErrPolicyNotFound = errors.New("politique de stockage introuvable")
|
||||
ErrInvalidTrashBatch = errors.New("lot de corbeille invalide")
|
||||
ErrPermissionDenied = errors.New("permission refusée")
|
||||
)
|
||||
|
||||
@@ -0,0 +1,247 @@
|
||||
package document
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"mediarr/ident"
|
||||
"mediarr/permission"
|
||||
)
|
||||
|
||||
func TestEnforcementCreateLibrary(t *testing.T) {
|
||||
authorizer := newAllowAllAuthorizer()
|
||||
service := newTestServiceWithAuthorizer(t, authorizer)
|
||||
ctx := context.Background()
|
||||
owner := mustUser(t, service, "owner")
|
||||
other := mustUser(t, service, "other")
|
||||
|
||||
authorizer.allowAll = false
|
||||
authorizer.active = map[ident.UUID]bool{owner.ID: false}
|
||||
|
||||
_, err := service.CreateLibrary(ctx, owner.ID, CreateLibraryParams{
|
||||
OwnerID: owner.ID,
|
||||
CreatedBy: owner.ID,
|
||||
Name: "Photos",
|
||||
PolicyID: testPolicyID,
|
||||
})
|
||||
if !errors.Is(err, ErrPermissionDenied) {
|
||||
t.Fatalf("acteur inactif : erreur = %v, attendu ErrPermissionDenied", err)
|
||||
}
|
||||
|
||||
authorizer.active[owner.ID] = true
|
||||
|
||||
_, err = service.CreateLibrary(ctx, owner.ID, CreateLibraryParams{
|
||||
OwnerID: other.ID,
|
||||
CreatedBy: owner.ID,
|
||||
Name: "Photos",
|
||||
PolicyID: testPolicyID,
|
||||
})
|
||||
if !errors.Is(err, ErrPermissionDenied) {
|
||||
t.Fatalf("owner usurpé : erreur = %v, attendu ErrPermissionDenied", err)
|
||||
}
|
||||
|
||||
_, err = service.CreateLibrary(ctx, owner.ID, CreateLibraryParams{
|
||||
OwnerID: owner.ID,
|
||||
CreatedBy: other.ID,
|
||||
Name: "Photos",
|
||||
PolicyID: testPolicyID,
|
||||
})
|
||||
if !errors.Is(err, ErrPermissionDenied) {
|
||||
t.Fatalf("created_by usurpé : erreur = %v, attendu ErrPermissionDenied", err)
|
||||
}
|
||||
|
||||
if _, err := service.CreateLibrary(ctx, owner.ID, CreateLibraryParams{
|
||||
OwnerID: owner.ID,
|
||||
CreatedBy: owner.ID,
|
||||
Name: "Photos",
|
||||
PolicyID: testPolicyID,
|
||||
}); err != nil {
|
||||
t.Fatalf("CreateLibrary : %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnforcementCreateFolder(t *testing.T) {
|
||||
authorizer := newAllowAllAuthorizer()
|
||||
service := newTestServiceWithAuthorizer(t, authorizer)
|
||||
ctx := context.Background()
|
||||
owner := mustUser(t, service, "owner")
|
||||
other := mustUser(t, service, "other")
|
||||
library := mustLibrary(t, service, owner, "Photos")
|
||||
|
||||
authorizer.allowAll = false
|
||||
authorizer.active = map[ident.UUID]bool{owner.ID: true, other.ID: true}
|
||||
|
||||
_, err := service.CreateFolder(ctx, owner.ID, CreateFolderParams{
|
||||
OwnerID: owner.ID,
|
||||
CreatedBy: owner.ID,
|
||||
ParentID: library.ID,
|
||||
Name: "Vacances",
|
||||
})
|
||||
if !errors.Is(err, ErrPermissionDenied) {
|
||||
t.Fatalf("sans Create : erreur = %v, attendu ErrPermissionDenied", err)
|
||||
}
|
||||
|
||||
authorizer.grant(owner.ID, library.ID, permission.Create)
|
||||
|
||||
if _, err := service.CreateFolder(ctx, owner.ID, CreateFolderParams{
|
||||
OwnerID: owner.ID,
|
||||
CreatedBy: owner.ID,
|
||||
ParentID: library.ID,
|
||||
Name: "Vacances",
|
||||
}); err != nil {
|
||||
t.Fatalf("CreateFolder : %v", err)
|
||||
}
|
||||
|
||||
_, err = service.CreateFolder(ctx, owner.ID, CreateFolderParams{
|
||||
OwnerID: other.ID,
|
||||
CreatedBy: owner.ID,
|
||||
ParentID: library.ID,
|
||||
Name: "Autre",
|
||||
})
|
||||
if !errors.Is(err, ErrPermissionDenied) {
|
||||
t.Fatalf("owner usurpé : erreur = %v, attendu ErrPermissionDenied", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnforcementGetAndList(t *testing.T) {
|
||||
authorizer := newAllowAllAuthorizer()
|
||||
service := newTestServiceWithAuthorizer(t, authorizer)
|
||||
ctx := context.Background()
|
||||
owner := mustUser(t, service, "owner")
|
||||
reader := mustUser(t, service, "reader")
|
||||
outsider := mustUser(t, service, "outsider")
|
||||
|
||||
photos := mustLibrary(t, service, owner, "Photos")
|
||||
mustLibrary(t, service, owner, "Films")
|
||||
allowedFolder := mustFolder(t, service, owner, photos.ID, "Allowed")
|
||||
hiddenFolder := mustFolder(t, service, owner, photos.ID, "Hidden")
|
||||
|
||||
authorizer.allowAll = false
|
||||
authorizer.active = map[ident.UUID]bool{
|
||||
owner.ID: true,
|
||||
reader.ID: true,
|
||||
outsider.ID: true,
|
||||
}
|
||||
authorizer.grant(reader.ID, photos.ID, permission.Read)
|
||||
authorizer.grant(reader.ID, allowedFolder.ID, permission.Read)
|
||||
|
||||
if _, err := service.Get(ctx, outsider.ID, photos.ID); !errors.Is(err, ErrPermissionDenied) {
|
||||
t.Fatalf("Get sans Read : erreur = %v, attendu ErrPermissionDenied", err)
|
||||
}
|
||||
if _, err := service.Get(ctx, reader.ID, photos.ID); err != nil {
|
||||
t.Fatalf("Get : %v", err)
|
||||
}
|
||||
if _, err := service.Get(ctx, reader.ID, hiddenFolder.ID); !errors.Is(err, ErrPermissionDenied) {
|
||||
t.Fatalf("Get enfant caché : erreur = %v, attendu ErrPermissionDenied", err)
|
||||
}
|
||||
|
||||
libraries, err := service.ListLibraries(ctx, reader.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("ListLibraries : %v", err)
|
||||
}
|
||||
if len(libraries) != 1 || libraries[0].ID != photos.ID {
|
||||
t.Fatalf("bibliothèques = %+v", libraries)
|
||||
}
|
||||
|
||||
children, err := service.ListChildren(ctx, reader.ID, photos.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("ListChildren : %v", err)
|
||||
}
|
||||
if len(children) != 1 || children[0].ID != allowedFolder.ID {
|
||||
t.Fatalf("enfants = %+v", children)
|
||||
}
|
||||
|
||||
if _, err := service.ListChildren(ctx, outsider.ID, photos.ID); !errors.Is(err, ErrPermissionDenied) {
|
||||
t.Fatalf("ListChildren sans Read : erreur = %v, attendu ErrPermissionDenied", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnforcementRename(t *testing.T) {
|
||||
authorizer := newAllowAllAuthorizer()
|
||||
service := newTestServiceWithAuthorizer(t, authorizer)
|
||||
ctx := context.Background()
|
||||
owner := mustUser(t, service, "owner")
|
||||
writer := mustUser(t, service, "writer")
|
||||
library := mustLibrary(t, service, owner, "Photos")
|
||||
folder := mustFolder(t, service, owner, library.ID, "Action")
|
||||
|
||||
authorizer.allowAll = false
|
||||
authorizer.active = map[ident.UUID]bool{writer.ID: true}
|
||||
|
||||
if _, err := service.Rename(ctx, writer.ID, folder.ID, "Aventure"); !errors.Is(err, ErrPermissionDenied) {
|
||||
t.Fatalf("sans Write : erreur = %v, attendu ErrPermissionDenied", err)
|
||||
}
|
||||
|
||||
authorizer.grant(writer.ID, folder.ID, permission.Manage)
|
||||
if _, err := service.Rename(ctx, writer.ID, folder.ID, "Aventure"); !errors.Is(err, ErrPermissionDenied) {
|
||||
t.Fatalf("avec Manage seul : erreur = %v, attendu ErrPermissionDenied", err)
|
||||
}
|
||||
|
||||
authorizer.grant(writer.ID, folder.ID, permission.Write)
|
||||
if _, err := service.Rename(ctx, writer.ID, folder.ID, "Aventure"); err != nil {
|
||||
t.Fatalf("Rename : %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnforcementMove(t *testing.T) {
|
||||
authorizer := newAllowAllAuthorizer()
|
||||
service := newTestServiceWithAuthorizer(t, authorizer)
|
||||
ctx := context.Background()
|
||||
owner := mustUser(t, service, "owner")
|
||||
mover := mustUser(t, service, "mover")
|
||||
libraryA := mustLibrary(t, service, owner, "Photos")
|
||||
libraryB := mustLibrary(t, service, owner, "Films")
|
||||
folder := mustFolder(t, service, owner, libraryA.ID, "Action")
|
||||
|
||||
authorizer.allowAll = false
|
||||
authorizer.active = map[ident.UUID]bool{mover.ID: true}
|
||||
|
||||
if _, err := service.Move(ctx, mover.ID, folder.ID, libraryB.ID); !errors.Is(err, ErrPermissionDenied) {
|
||||
t.Fatalf("sans Manage : erreur = %v, attendu ErrPermissionDenied", err)
|
||||
}
|
||||
|
||||
authorizer.grant(mover.ID, folder.ID, permission.Manage)
|
||||
if _, err := service.Move(ctx, mover.ID, folder.ID, libraryB.ID); !errors.Is(err, ErrPermissionDenied) {
|
||||
t.Fatalf("sans Create sur la cible : erreur = %v, attendu ErrPermissionDenied", err)
|
||||
}
|
||||
|
||||
authorizer.grant(mover.ID, libraryB.ID, permission.Create)
|
||||
if _, err := service.Move(ctx, mover.ID, folder.ID, libraryB.ID); err != nil {
|
||||
t.Fatalf("Move : %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnforcementTrashRestore(t *testing.T) {
|
||||
authorizer := newAllowAllAuthorizer()
|
||||
service := newTestServiceWithAuthorizer(t, authorizer)
|
||||
ctx := context.Background()
|
||||
owner := mustUser(t, service, "owner")
|
||||
deleter := mustUser(t, service, "deleter")
|
||||
library := mustLibrary(t, service, owner, "Photos")
|
||||
folder := mustFolder(t, service, owner, library.ID, "Action")
|
||||
|
||||
authorizer.allowAll = false
|
||||
authorizer.active = map[ident.UUID]bool{deleter.ID: true}
|
||||
|
||||
if _, err := service.Trash(ctx, deleter.ID, folder.ID); !errors.Is(err, ErrPermissionDenied) {
|
||||
t.Fatalf("Trash sans Delete : erreur = %v, attendu ErrPermissionDenied", err)
|
||||
}
|
||||
|
||||
authorizer.grant(deleter.ID, folder.ID, permission.Delete)
|
||||
trashed, err := service.Trash(ctx, deleter.ID, folder.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("Trash : %v", err)
|
||||
}
|
||||
if trashed.DeletedBy == nil || *trashed.DeletedBy != deleter.ID {
|
||||
t.Fatalf("deleted_by = %v, attendu %s", trashed.DeletedBy, deleter.ID)
|
||||
}
|
||||
|
||||
if _, err := service.Get(ctx, deleter.ID, folder.ID); !errors.Is(err, ErrPermissionDenied) {
|
||||
t.Fatalf("Get sans Read : erreur = %v, attendu ErrPermissionDenied", err)
|
||||
}
|
||||
|
||||
if _, err := service.Restore(ctx, deleter.ID, folder.ID); err != nil {
|
||||
t.Fatalf("Restore : %v", err)
|
||||
}
|
||||
}
|
||||
@@ -9,13 +9,62 @@ import (
|
||||
"mediarr/database"
|
||||
"mediarr/ident"
|
||||
"mediarr/identity"
|
||||
"mediarr/permission"
|
||||
)
|
||||
|
||||
var testPolicyID = ident.MustParse("00000000-0000-7000-8000-000000000101")
|
||||
|
||||
type testAuthorizer struct {
|
||||
allowAll bool
|
||||
active map[ident.UUID]bool
|
||||
grants map[ident.UUID]map[ident.UUID]permission.Permission
|
||||
}
|
||||
|
||||
func newAllowAllAuthorizer() *testAuthorizer {
|
||||
return &testAuthorizer{allowAll: true}
|
||||
}
|
||||
|
||||
func (a *testAuthorizer) IsActive(_ context.Context, principalID ident.UUID) (bool, error) {
|
||||
if a.active == nil {
|
||||
return true, nil
|
||||
}
|
||||
if active, exists := a.active[principalID]; exists {
|
||||
return active, nil
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (a *testAuthorizer) Can(_ context.Context, principalID, documentID ident.UUID, required permission.Permission) (bool, error) {
|
||||
if a.allowAll {
|
||||
return true, nil
|
||||
}
|
||||
if a.active != nil {
|
||||
if active, exists := a.active[principalID]; exists && !active {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
return a.grants[documentID][principalID].Has(required), nil
|
||||
}
|
||||
|
||||
func (a *testAuthorizer) grant(principalID, documentID ident.UUID, permissions permission.Permission) {
|
||||
if a.grants == nil {
|
||||
a.grants = make(map[ident.UUID]map[ident.UUID]permission.Permission)
|
||||
}
|
||||
if a.grants[documentID] == nil {
|
||||
a.grants[documentID] = make(map[ident.UUID]permission.Permission)
|
||||
}
|
||||
a.grants[documentID][principalID] |= permissions
|
||||
}
|
||||
|
||||
func newTestService(t *testing.T) *Service {
|
||||
t.Helper()
|
||||
|
||||
return newTestServiceWithAuthorizer(t, newAllowAllAuthorizer())
|
||||
}
|
||||
|
||||
func newTestServiceWithAuthorizer(t *testing.T, authorizer Authorizer) *Service {
|
||||
t.Helper()
|
||||
|
||||
t.Chdir(t.TempDir())
|
||||
|
||||
err, db := database.NewDatabase(database.NewDatabaseParams{})
|
||||
@@ -29,7 +78,7 @@ func newTestService(t *testing.T) *Service {
|
||||
}
|
||||
})
|
||||
|
||||
return NewService(*db)
|
||||
return NewService(*db, authorizer)
|
||||
}
|
||||
|
||||
func mustPrincipal(t *testing.T, service *Service, kind, name string) *identity.Principal {
|
||||
@@ -53,7 +102,7 @@ func mustUser(t *testing.T, service *Service, name string) *identity.Principal {
|
||||
func mustLibrary(t *testing.T, service *Service, owner *identity.Principal, name string) *Document {
|
||||
t.Helper()
|
||||
|
||||
library, err := service.CreateLibrary(context.Background(), CreateLibraryParams{
|
||||
library, err := service.CreateLibrary(context.Background(), owner.ID, CreateLibraryParams{
|
||||
OwnerID: owner.ID,
|
||||
CreatedBy: owner.ID,
|
||||
Name: name,
|
||||
@@ -69,7 +118,7 @@ func mustLibrary(t *testing.T, service *Service, owner *identity.Principal, name
|
||||
func mustFolder(t *testing.T, service *Service, owner *identity.Principal, parentID ident.UUID, name string) *Document {
|
||||
t.Helper()
|
||||
|
||||
folder, err := service.CreateFolder(context.Background(), CreateFolderParams{
|
||||
folder, err := service.CreateFolder(context.Background(), owner.ID, CreateFolderParams{
|
||||
OwnerID: owner.ID,
|
||||
CreatedBy: owner.ID,
|
||||
ParentID: parentID,
|
||||
|
||||
+23
-7
@@ -11,12 +11,16 @@ import (
|
||||
|
||||
"mediarr/database"
|
||||
"mediarr/ident"
|
||||
"mediarr/permission"
|
||||
)
|
||||
|
||||
func (s *Service) Rename(ctx context.Context, documentID ident.UUID, name string) (*Document, error) {
|
||||
func (s *Service) Rename(ctx context.Context, actorID, documentID ident.UUID, name string) (*Document, error) {
|
||||
if _, err := s.getActiveDocument(ctx, documentID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.can(ctx, actorID, documentID, permission.Write); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
normalized, nameKey, err := normalizeName(name)
|
||||
if err != nil {
|
||||
@@ -42,11 +46,14 @@ func (s *Service) Rename(ctx context.Context, documentID ident.UUID, name string
|
||||
return documentFromDB(updated), nil
|
||||
}
|
||||
|
||||
func (s *Service) Move(ctx context.Context, documentID, newParentID ident.UUID) (*Document, error) {
|
||||
func (s *Service) Move(ctx context.Context, actorID, documentID, newParentID ident.UUID) (*Document, error) {
|
||||
document, err := s.getActiveDocument(ctx, documentID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.can(ctx, actorID, documentID, permission.Manage); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if document.ParentID == nil {
|
||||
return nil, ErrCannotMoveRoot
|
||||
}
|
||||
@@ -73,6 +80,9 @@ func (s *Service) Move(ctx context.Context, documentID, newParentID ident.UUID)
|
||||
if strings.HasPrefix(parent.Path, document.Path) {
|
||||
return nil, ErrInvalidMove
|
||||
}
|
||||
if err := s.can(ctx, actorID, newParentID, permission.Create); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
newPath := parent.Path + hex.EncodeToString(document.ID[:]) + "/"
|
||||
newDepth := parent.Depth + 1
|
||||
@@ -126,11 +136,14 @@ func (s *Service) Move(ctx context.Context, documentID, newParentID ident.UUID)
|
||||
return documentFromDB(moved), nil
|
||||
}
|
||||
|
||||
func (s *Service) Trash(ctx context.Context, documentID, deletedBy ident.UUID) (*Document, error) {
|
||||
func (s *Service) Trash(ctx context.Context, actorID, documentID ident.UUID) (*Document, error) {
|
||||
document, err := s.getActiveDocument(ctx, documentID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.can(ctx, actorID, documentID, permission.Delete); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
batch, err := ident.NewV7()
|
||||
if err != nil {
|
||||
@@ -144,7 +157,7 @@ func (s *Service) Trash(ctx context.Context, documentID, deletedBy ident.UUID) (
|
||||
Int64: now,
|
||||
Valid: true,
|
||||
},
|
||||
DeletedBy: &deletedBy,
|
||||
DeletedBy: &actorID,
|
||||
TrashBatch: batch.Bytes(),
|
||||
UpdatedAt: now,
|
||||
Prefix: document.Path,
|
||||
@@ -156,14 +169,17 @@ func (s *Service) Trash(ctx context.Context, documentID, deletedBy ident.UUID) (
|
||||
return nil, fmt.Errorf("%w: %s", ErrDocumentNotFound, documentID)
|
||||
}
|
||||
|
||||
return s.Get(ctx, documentID)
|
||||
return s.getDocumentModel(ctx, documentID)
|
||||
}
|
||||
|
||||
func (s *Service) Restore(ctx context.Context, documentID ident.UUID) (*Document, error) {
|
||||
func (s *Service) Restore(ctx context.Context, actorID, documentID ident.UUID) (*Document, error) {
|
||||
document, err := s.getDocument(ctx, documentID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.can(ctx, actorID, documentID, permission.Delete); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !document.DeletedAt.Valid {
|
||||
return documentFromDB(*document), nil
|
||||
}
|
||||
@@ -199,5 +215,5 @@ func (s *Service) Restore(ctx context.Context, documentID ident.UUID) (*Document
|
||||
return nil, fmt.Errorf("%w: %s", ErrDocumentNotFound, documentID)
|
||||
}
|
||||
|
||||
return s.Get(ctx, documentID)
|
||||
return s.getDocumentModel(ctx, documentID)
|
||||
}
|
||||
|
||||
+38
-38
@@ -15,7 +15,7 @@ func TestRename(t *testing.T) {
|
||||
library := mustLibrary(t, service, owner, "Photos")
|
||||
folder := mustFolder(t, service, owner, library.ID, "Action")
|
||||
|
||||
renamed, err := service.Rename(ctx, folder.ID, "Aventure")
|
||||
renamed, err := service.Rename(ctx, owner.ID, folder.ID, "Aventure")
|
||||
if err != nil {
|
||||
t.Fatalf("Rename : %v", err)
|
||||
}
|
||||
@@ -26,19 +26,19 @@ func TestRename(t *testing.T) {
|
||||
t.Fatalf("rev = %d, attendu %d", renamed.Rev, folder.Rev+1)
|
||||
}
|
||||
|
||||
if _, err := service.Rename(ctx, folder.ID, "a/b"); !errors.Is(err, ErrInvalidName) {
|
||||
if _, err := service.Rename(ctx, owner.ID, folder.ID, "a/b"); !errors.Is(err, ErrInvalidName) {
|
||||
t.Fatalf("erreur = %v, attendu ErrInvalidName", err)
|
||||
}
|
||||
|
||||
other := mustFolder(t, service, owner, library.ID, "Drame")
|
||||
if _, err := service.Rename(ctx, other.ID, "aventure"); !errors.Is(err, ErrNameTaken) {
|
||||
if _, err := service.Rename(ctx, owner.ID, other.ID, "aventure"); !errors.Is(err, ErrNameTaken) {
|
||||
t.Fatalf("erreur = %v, attendu ErrNameTaken", err)
|
||||
}
|
||||
|
||||
if _, err := service.Trash(ctx, other.ID, owner.ID); err != nil {
|
||||
if _, err := service.Trash(ctx, owner.ID, other.ID); err != nil {
|
||||
t.Fatalf("Trash : %v", err)
|
||||
}
|
||||
if _, err := service.Rename(ctx, other.ID, "Autre"); !errors.Is(err, ErrDocumentDeleted) {
|
||||
if _, err := service.Rename(ctx, owner.ID, other.ID, "Autre"); !errors.Is(err, ErrDocumentDeleted) {
|
||||
t.Fatalf("erreur = %v, attendu ErrDocumentDeleted", err)
|
||||
}
|
||||
}
|
||||
@@ -53,7 +53,7 @@ func TestMove(t *testing.T) {
|
||||
folder := mustFolder(t, service, owner, libraryA.ID, "Action")
|
||||
child := mustFolder(t, service, owner, folder.ID, "Extras")
|
||||
|
||||
moved, err := service.Move(ctx, folder.ID, libraryB.ID)
|
||||
moved, err := service.Move(ctx, owner.ID, folder.ID, libraryB.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("Move : %v", err)
|
||||
}
|
||||
@@ -70,7 +70,7 @@ func TestMove(t *testing.T) {
|
||||
t.Fatalf("rev = %d, attendu %d", moved.Rev, folder.Rev+1)
|
||||
}
|
||||
|
||||
movedChild, err := service.Get(ctx, child.ID)
|
||||
movedChild, err := service.Get(ctx, owner.ID, child.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("Get(child) : %v", err)
|
||||
}
|
||||
@@ -87,7 +87,7 @@ func TestMove(t *testing.T) {
|
||||
t.Fatalf("rev enfant = %d, attendu %d", movedChild.Rev, child.Rev+1)
|
||||
}
|
||||
|
||||
children, err := service.ListChildren(ctx, libraryB.ID)
|
||||
children, err := service.ListChildren(ctx, owner.ID, libraryB.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("ListChildren : %v", err)
|
||||
}
|
||||
@@ -103,7 +103,7 @@ func TestMoveRootRejected(t *testing.T) {
|
||||
library := mustLibrary(t, service, owner, "Photos")
|
||||
other := mustLibrary(t, service, owner, "Films")
|
||||
|
||||
_, err := service.Move(ctx, library.ID, other.ID)
|
||||
_, err := service.Move(ctx, owner.ID, library.ID, other.ID)
|
||||
if !errors.Is(err, ErrCannotMoveRoot) {
|
||||
t.Fatalf("erreur = %v, attendu ErrCannotMoveRoot", err)
|
||||
}
|
||||
@@ -117,7 +117,7 @@ func TestMoveIntoDescendantRejected(t *testing.T) {
|
||||
folder := mustFolder(t, service, owner, library.ID, "Action")
|
||||
child := mustFolder(t, service, owner, folder.ID, "Extras")
|
||||
|
||||
_, err := service.Move(ctx, folder.ID, child.ID)
|
||||
_, err := service.Move(ctx, owner.ID, folder.ID, child.ID)
|
||||
if !errors.Is(err, ErrInvalidMove) {
|
||||
t.Fatalf("erreur = %v, attendu ErrInvalidMove", err)
|
||||
}
|
||||
@@ -130,7 +130,7 @@ func TestMoveIntoItselfRejected(t *testing.T) {
|
||||
library := mustLibrary(t, service, owner, "Photos")
|
||||
folder := mustFolder(t, service, owner, library.ID, "Action")
|
||||
|
||||
_, err := service.Move(ctx, folder.ID, folder.ID)
|
||||
_, err := service.Move(ctx, owner.ID, folder.ID, folder.ID)
|
||||
if !errors.Is(err, ErrInvalidMove) {
|
||||
t.Fatalf("erreur = %v, attendu ErrInvalidMove", err)
|
||||
}
|
||||
@@ -144,7 +144,7 @@ func TestMoveToInvalidParentRejected(t *testing.T) {
|
||||
folder := mustFolder(t, service, owner, library.ID, "Action")
|
||||
movie := mustKindDocument(t, service, owner, library, "movie", "Film")
|
||||
|
||||
_, err := service.Move(ctx, folder.ID, movie.ID)
|
||||
_, err := service.Move(ctx, owner.ID, folder.ID, movie.ID)
|
||||
if !errors.Is(err, ErrInvalidParent) {
|
||||
t.Fatalf("erreur = %v, attendu ErrInvalidParent", err)
|
||||
}
|
||||
@@ -157,7 +157,7 @@ func TestMoveToSameParentIsNoop(t *testing.T) {
|
||||
library := mustLibrary(t, service, owner, "Photos")
|
||||
folder := mustFolder(t, service, owner, library.ID, "Action")
|
||||
|
||||
moved, err := service.Move(ctx, folder.ID, library.ID)
|
||||
moved, err := service.Move(ctx, owner.ID, folder.ID, library.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("Move : %v", err)
|
||||
}
|
||||
@@ -175,7 +175,7 @@ func TestMoveDuplicateNameRejected(t *testing.T) {
|
||||
folder := mustFolder(t, service, owner, libraryA.ID, "Action")
|
||||
mustFolder(t, service, owner, libraryB.ID, "action")
|
||||
|
||||
_, err := service.Move(ctx, folder.ID, libraryB.ID)
|
||||
_, err := service.Move(ctx, owner.ID, folder.ID, libraryB.ID)
|
||||
if !errors.Is(err, ErrNameTaken) {
|
||||
t.Fatalf("erreur = %v, attendu ErrNameTaken", err)
|
||||
}
|
||||
@@ -189,11 +189,11 @@ func TestMoveRejectsDeletedDocument(t *testing.T) {
|
||||
libraryB := mustLibrary(t, service, owner, "Films")
|
||||
folder := mustFolder(t, service, owner, libraryA.ID, "Action")
|
||||
|
||||
if _, err := service.Trash(ctx, folder.ID, owner.ID); err != nil {
|
||||
if _, err := service.Trash(ctx, owner.ID, folder.ID); err != nil {
|
||||
t.Fatalf("Trash : %v", err)
|
||||
}
|
||||
|
||||
_, err := service.Move(ctx, folder.ID, libraryB.ID)
|
||||
_, err := service.Move(ctx, owner.ID, folder.ID, libraryB.ID)
|
||||
if !errors.Is(err, ErrDocumentDeleted) {
|
||||
t.Fatalf("erreur = %v, attendu ErrDocumentDeleted", err)
|
||||
}
|
||||
@@ -207,11 +207,11 @@ func TestMoveRejectsDeletedParent(t *testing.T) {
|
||||
libraryB := mustLibrary(t, service, owner, "Films")
|
||||
folder := mustFolder(t, service, owner, libraryA.ID, "Action")
|
||||
|
||||
if _, err := service.Trash(ctx, libraryB.ID, owner.ID); err != nil {
|
||||
if _, err := service.Trash(ctx, owner.ID, libraryB.ID); err != nil {
|
||||
t.Fatalf("Trash : %v", err)
|
||||
}
|
||||
|
||||
_, err := service.Move(ctx, folder.ID, libraryB.ID)
|
||||
_, err := service.Move(ctx, owner.ID, folder.ID, libraryB.ID)
|
||||
if !errors.Is(err, ErrParentDeleted) {
|
||||
t.Fatalf("erreur = %v, attendu ErrParentDeleted", err)
|
||||
}
|
||||
@@ -225,7 +225,7 @@ func TestTrashAndRestore(t *testing.T) {
|
||||
folder := mustFolder(t, service, owner, library.ID, "Action")
|
||||
child := mustFolder(t, service, owner, folder.ID, "Extras")
|
||||
|
||||
trashed, err := service.Trash(ctx, folder.ID, owner.ID)
|
||||
trashed, err := service.Trash(ctx, owner.ID, folder.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("Trash : %v", err)
|
||||
}
|
||||
@@ -239,7 +239,7 @@ func TestTrashAndRestore(t *testing.T) {
|
||||
t.Fatal("trash_batch non renseigné")
|
||||
}
|
||||
|
||||
trashedChild, err := service.Get(ctx, child.ID)
|
||||
trashedChild, err := service.Get(ctx, owner.ID, child.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("Get(child) : %v", err)
|
||||
}
|
||||
@@ -250,7 +250,7 @@ func TestTrashAndRestore(t *testing.T) {
|
||||
t.Fatal("l’enfant n’appartient pas au même lot de corbeille")
|
||||
}
|
||||
|
||||
children, err := service.ListChildren(ctx, library.ID)
|
||||
children, err := service.ListChildren(ctx, owner.ID, library.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("ListChildren : %v", err)
|
||||
}
|
||||
@@ -258,7 +258,7 @@ func TestTrashAndRestore(t *testing.T) {
|
||||
t.Fatalf("enfants = %d, attendu 0", len(children))
|
||||
}
|
||||
|
||||
restored, err := service.Restore(ctx, folder.ID)
|
||||
restored, err := service.Restore(ctx, owner.ID, folder.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("Restore : %v", err)
|
||||
}
|
||||
@@ -266,7 +266,7 @@ func TestTrashAndRestore(t *testing.T) {
|
||||
t.Fatalf("document restauré avec des champs de corbeille : %+v", restored)
|
||||
}
|
||||
|
||||
restoredChild, err := service.Get(ctx, child.ID)
|
||||
restoredChild, err := service.Get(ctx, owner.ID, child.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("Get(child restauré) : %v", err)
|
||||
}
|
||||
@@ -274,7 +274,7 @@ func TestTrashAndRestore(t *testing.T) {
|
||||
t.Fatal("enfant non restauré")
|
||||
}
|
||||
|
||||
children, err = service.ListChildren(ctx, library.ID)
|
||||
children, err = service.ListChildren(ctx, owner.ID, library.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("ListChildren après restauration : %v", err)
|
||||
}
|
||||
@@ -289,7 +289,7 @@ func TestRestoreNonDeletedIsNoop(t *testing.T) {
|
||||
owner := mustUser(t, service, "owner")
|
||||
library := mustLibrary(t, service, owner, "Photos")
|
||||
|
||||
restored, err := service.Restore(ctx, library.ID)
|
||||
restored, err := service.Restore(ctx, owner.ID, library.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("Restore : %v", err)
|
||||
}
|
||||
@@ -304,11 +304,11 @@ func TestTrashAlreadyDeleted(t *testing.T) {
|
||||
owner := mustUser(t, service, "owner")
|
||||
library := mustLibrary(t, service, owner, "Photos")
|
||||
|
||||
if _, err := service.Trash(ctx, library.ID, owner.ID); err != nil {
|
||||
if _, err := service.Trash(ctx, owner.ID, library.ID); err != nil {
|
||||
t.Fatalf("Trash : %v", err)
|
||||
}
|
||||
|
||||
if _, err := service.Trash(ctx, library.ID, owner.ID); !errors.Is(err, ErrDocumentDeleted) {
|
||||
if _, err := service.Trash(ctx, owner.ID, library.ID); !errors.Is(err, ErrDocumentDeleted) {
|
||||
t.Fatalf("erreur = %v, attendu ErrDocumentDeleted", err)
|
||||
}
|
||||
}
|
||||
@@ -320,13 +320,13 @@ func TestRestoreConflict(t *testing.T) {
|
||||
library := mustLibrary(t, service, owner, "Photos")
|
||||
folder := mustFolder(t, service, owner, library.ID, "Action")
|
||||
|
||||
if _, err := service.Trash(ctx, folder.ID, owner.ID); err != nil {
|
||||
if _, err := service.Trash(ctx, owner.ID, folder.ID); err != nil {
|
||||
t.Fatalf("Trash : %v", err)
|
||||
}
|
||||
|
||||
mustFolder(t, service, owner, library.ID, "Action")
|
||||
|
||||
if _, err := service.Restore(ctx, folder.ID); !errors.Is(err, ErrNameTaken) {
|
||||
if _, err := service.Restore(ctx, owner.ID, folder.ID); !errors.Is(err, ErrNameTaken) {
|
||||
t.Fatalf("erreur = %v, attendu ErrNameTaken", err)
|
||||
}
|
||||
}
|
||||
@@ -339,14 +339,14 @@ func TestRestoreParentDeletedInDifferentBatch(t *testing.T) {
|
||||
folder := mustFolder(t, service, owner, library.ID, "Action")
|
||||
child := mustFolder(t, service, owner, folder.ID, "Extras")
|
||||
|
||||
if _, err := service.Trash(ctx, child.ID, owner.ID); err != nil {
|
||||
if _, err := service.Trash(ctx, owner.ID, child.ID); err != nil {
|
||||
t.Fatalf("Trash(child) : %v", err)
|
||||
}
|
||||
if _, err := service.Trash(ctx, folder.ID, owner.ID); err != nil {
|
||||
if _, err := service.Trash(ctx, owner.ID, folder.ID); err != nil {
|
||||
t.Fatalf("Trash(folder) : %v", err)
|
||||
}
|
||||
|
||||
if _, err := service.Restore(ctx, child.ID); !errors.Is(err, ErrParentDeleted) {
|
||||
if _, err := service.Restore(ctx, owner.ID, child.ID); !errors.Is(err, ErrParentDeleted) {
|
||||
t.Fatalf("erreur = %v, attendu ErrParentDeleted", err)
|
||||
}
|
||||
}
|
||||
@@ -359,14 +359,14 @@ func TestRestoreRejectsDeletedAncestor(t *testing.T) {
|
||||
folder := mustFolder(t, service, owner, library.ID, "Action")
|
||||
child := mustFolder(t, service, owner, folder.ID, "Extras")
|
||||
|
||||
if _, err := service.Trash(ctx, folder.ID, owner.ID); err != nil {
|
||||
if _, err := service.Trash(ctx, owner.ID, folder.ID); err != nil {
|
||||
t.Fatalf("Trash(folder) : %v", err)
|
||||
}
|
||||
if _, err := service.Trash(ctx, library.ID, owner.ID); err != nil {
|
||||
if _, err := service.Trash(ctx, owner.ID, library.ID); err != nil {
|
||||
t.Fatalf("Trash(library) : %v", err)
|
||||
}
|
||||
|
||||
if _, err := service.Restore(ctx, child.ID); !errors.Is(err, ErrParentDeleted) {
|
||||
if _, err := service.Restore(ctx, owner.ID, child.ID); !errors.Is(err, ErrParentDeleted) {
|
||||
t.Fatalf("erreur = %v, attendu ErrParentDeleted", err)
|
||||
}
|
||||
}
|
||||
@@ -380,16 +380,16 @@ func TestMoveUpdatesTrashedDescendants(t *testing.T) {
|
||||
folder := mustFolder(t, service, owner, libraryA.ID, "Action")
|
||||
child := mustFolder(t, service, owner, folder.ID, "Extras")
|
||||
|
||||
if _, err := service.Trash(ctx, child.ID, owner.ID); err != nil {
|
||||
if _, err := service.Trash(ctx, owner.ID, child.ID); err != nil {
|
||||
t.Fatalf("Trash(child) : %v", err)
|
||||
}
|
||||
|
||||
moved, err := service.Move(ctx, folder.ID, libraryB.ID)
|
||||
moved, err := service.Move(ctx, owner.ID, folder.ID, libraryB.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("Move : %v", err)
|
||||
}
|
||||
|
||||
restored, err := service.Restore(ctx, child.ID)
|
||||
restored, err := service.Restore(ctx, owner.ID, child.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("Restore(child) : %v", err)
|
||||
}
|
||||
|
||||
+22
-3
@@ -4,21 +4,28 @@ import (
|
||||
"context"
|
||||
|
||||
"mediarr/ident"
|
||||
"mediarr/permission"
|
||||
)
|
||||
|
||||
func (s *Service) Get(ctx context.Context, id ident.UUID) (*Document, error) {
|
||||
func (s *Service) Get(ctx context.Context, actorID, id ident.UUID) (*Document, error) {
|
||||
document, err := s.getDocument(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.can(ctx, actorID, id, permission.Read); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return documentFromDB(*document), nil
|
||||
}
|
||||
|
||||
func (s *Service) ListChildren(ctx context.Context, parentID ident.UUID) ([]Document, error) {
|
||||
func (s *Service) ListChildren(ctx context.Context, actorID, parentID ident.UUID) ([]Document, error) {
|
||||
if _, err := s.getActiveDocument(ctx, parentID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.can(ctx, actorID, parentID, permission.Read); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rows, err := s.db.Queries.ListDocumentChildren(ctx, &parentID)
|
||||
if err != nil {
|
||||
@@ -27,13 +34,19 @@ func (s *Service) ListChildren(ctx context.Context, parentID ident.UUID) ([]Docu
|
||||
|
||||
documents := make([]Document, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
allowed, err := s.allowed(ctx, actorID, row.ID, permission.Read)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if allowed {
|
||||
documents = append(documents, *documentFromDB(row))
|
||||
}
|
||||
}
|
||||
|
||||
return documents, nil
|
||||
}
|
||||
|
||||
func (s *Service) ListLibraries(ctx context.Context) ([]Document, error) {
|
||||
func (s *Service) ListLibraries(ctx context.Context, actorID ident.UUID) ([]Document, error) {
|
||||
rows, err := s.db.Queries.ListLibraries(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -41,8 +54,14 @@ func (s *Service) ListLibraries(ctx context.Context) ([]Document, error) {
|
||||
|
||||
documents := make([]Document, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
allowed, err := s.allowed(ctx, actorID, row.ID, permission.Read)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if allowed {
|
||||
documents = append(documents, *documentFromDB(row))
|
||||
}
|
||||
}
|
||||
|
||||
return documents, nil
|
||||
}
|
||||
|
||||
+12
-8
@@ -16,7 +16,7 @@ func TestListLibraries(t *testing.T) {
|
||||
mustLibrary(t, service, owner, "Photos")
|
||||
mustLibrary(t, service, owner, "Films")
|
||||
|
||||
libraries, err := service.ListLibraries(ctx)
|
||||
libraries, err := service.ListLibraries(ctx, owner.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("ListLibraries : %v", err)
|
||||
}
|
||||
@@ -37,7 +37,7 @@ func TestListChildren(t *testing.T) {
|
||||
mustFolder(t, service, owner, library.ID, "B")
|
||||
a := mustFolder(t, service, owner, library.ID, "A")
|
||||
|
||||
children, err := service.ListChildren(ctx, library.ID)
|
||||
children, err := service.ListChildren(ctx, owner.ID, library.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("ListChildren : %v", err)
|
||||
}
|
||||
@@ -45,11 +45,11 @@ func TestListChildren(t *testing.T) {
|
||||
t.Fatalf("enfants = %+v", children)
|
||||
}
|
||||
|
||||
if _, err := service.Trash(ctx, a.ID, owner.ID); err != nil {
|
||||
if _, err := service.Trash(ctx, owner.ID, a.ID); err != nil {
|
||||
t.Fatalf("Trash : %v", err)
|
||||
}
|
||||
|
||||
children, err = service.ListChildren(ctx, library.ID)
|
||||
children, err = service.ListChildren(ctx, owner.ID, library.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("ListChildren après trash : %v", err)
|
||||
}
|
||||
@@ -64,11 +64,11 @@ func TestGetDeletedDocument(t *testing.T) {
|
||||
owner := mustUser(t, service, "owner")
|
||||
library := mustLibrary(t, service, owner, "Photos")
|
||||
|
||||
if _, err := service.Trash(ctx, library.ID, owner.ID); err != nil {
|
||||
if _, err := service.Trash(ctx, owner.ID, library.ID); err != nil {
|
||||
t.Fatalf("Trash : %v", err)
|
||||
}
|
||||
|
||||
deleted, err := service.Get(ctx, library.ID)
|
||||
deleted, err := service.Get(ctx, owner.ID, library.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("Get : %v", err)
|
||||
}
|
||||
@@ -85,12 +85,14 @@ func TestGetDeletedDocument(t *testing.T) {
|
||||
|
||||
func TestGetMissingDocument(t *testing.T) {
|
||||
service := newTestService(t)
|
||||
ctx := context.Background()
|
||||
owner := mustUser(t, service, "owner")
|
||||
missing, err := ident.NewV7()
|
||||
if err != nil {
|
||||
t.Fatalf("NewV7 : %v", err)
|
||||
}
|
||||
|
||||
_, err = service.Get(context.Background(), missing)
|
||||
_, err = service.Get(ctx, owner.ID, missing)
|
||||
if !errors.Is(err, ErrDocumentNotFound) {
|
||||
t.Fatalf("erreur = %v, attendu ErrDocumentNotFound", err)
|
||||
}
|
||||
@@ -98,12 +100,14 @@ func TestGetMissingDocument(t *testing.T) {
|
||||
|
||||
func TestListChildrenMissingParent(t *testing.T) {
|
||||
service := newTestService(t)
|
||||
ctx := context.Background()
|
||||
owner := mustUser(t, service, "owner")
|
||||
missing, err := ident.NewV7()
|
||||
if err != nil {
|
||||
t.Fatalf("NewV7 : %v", err)
|
||||
}
|
||||
|
||||
_, err = service.ListChildren(context.Background(), missing)
|
||||
_, err = service.ListChildren(ctx, owner.ID, missing)
|
||||
if !errors.Is(err, ErrDocumentNotFound) {
|
||||
t.Fatalf("erreur = %v, attendu ErrDocumentNotFound", err)
|
||||
}
|
||||
|
||||
+49
-2
@@ -10,14 +10,24 @@ import (
|
||||
|
||||
"mediarr/database"
|
||||
"mediarr/ident"
|
||||
"mediarr/permission"
|
||||
)
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
db database.DatabaseClient
|
||||
authorizer Authorizer
|
||||
}
|
||||
|
||||
func NewService(db database.DatabaseClient) *Service {
|
||||
return &Service{db: db}
|
||||
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) {
|
||||
|
||||
@@ -3,5 +3,5 @@ package node
|
||||
import "mediarr/document"
|
||||
|
||||
func (n *Node) Documents() *document.Service {
|
||||
return document.NewService(n.db)
|
||||
return document.NewService(n.db, n.ACL())
|
||||
}
|
||||
|
||||
+102
-2
@@ -2,8 +2,10 @@ package node
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"mediarr/acl"
|
||||
"mediarr/document"
|
||||
"mediarr/ident"
|
||||
"mediarr/identity"
|
||||
@@ -37,7 +39,7 @@ func TestNodeDocuments(t *testing.T) {
|
||||
|
||||
policyID := ident.MustParse("00000000-0000-7000-8000-000000000101")
|
||||
|
||||
library, err := node.Documents().CreateLibrary(ctx, document.CreateLibraryParams{
|
||||
library, err := node.Documents().CreateLibrary(ctx, owner.ID, document.CreateLibraryParams{
|
||||
OwnerID: owner.ID,
|
||||
CreatedBy: owner.ID,
|
||||
Name: "Photos",
|
||||
@@ -50,7 +52,7 @@ func TestNodeDocuments(t *testing.T) {
|
||||
t.Fatalf("kind = %q, attendu %q", library.Kind, document.KindLibrary)
|
||||
}
|
||||
|
||||
folder, err := node.Documents().CreateFolder(ctx, document.CreateFolderParams{
|
||||
folder, err := node.Documents().CreateFolder(ctx, owner.ID, document.CreateFolderParams{
|
||||
OwnerID: owner.ID,
|
||||
CreatedBy: owner.ID,
|
||||
ParentID: library.ID,
|
||||
@@ -63,3 +65,101 @@ func TestNodeDocuments(t *testing.T) {
|
||||
t.Fatalf("parent = %v, attendu %s", folder.ParentID, library.ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNodeDocumentsEnforcesACL(t *testing.T) {
|
||||
t.Chdir(t.TempDir())
|
||||
|
||||
err, node := NewNode(NewNodeParams{DatabaseName: "documents_acl"})
|
||||
if err != nil {
|
||||
t.Fatalf("NewNode : %v", err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
if err := node.Close(); err != nil {
|
||||
t.Errorf("Close : %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
if err := node.Up(); err != nil {
|
||||
t.Fatalf("Up : %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
owner, err := node.Identity().CreatePrincipal(ctx, identity.CreatePrincipalParams{
|
||||
Kind: identity.KindUser,
|
||||
Name: "owner",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("CreatePrincipal(owner) : %v", err)
|
||||
}
|
||||
other, err := node.Identity().CreatePrincipal(ctx, identity.CreatePrincipalParams{
|
||||
Kind: identity.KindUser,
|
||||
Name: "other",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("CreatePrincipal(other) : %v", err)
|
||||
}
|
||||
|
||||
policyID := ident.MustParse("00000000-0000-7000-8000-000000000101")
|
||||
|
||||
library, err := node.Documents().CreateLibrary(ctx, owner.ID, document.CreateLibraryParams{
|
||||
OwnerID: owner.ID,
|
||||
CreatedBy: owner.ID,
|
||||
Name: "Photos",
|
||||
PolicyID: policyID,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("CreateLibrary : %v", err)
|
||||
}
|
||||
|
||||
if _, err := node.Documents().Get(ctx, other.ID, library.ID); !errors.Is(err, document.ErrPermissionDenied) {
|
||||
t.Fatalf("Get sans ACL : erreur = %v, attendu ErrPermissionDenied", err)
|
||||
}
|
||||
|
||||
if _, err := node.ACL().Grant(ctx, acl.GrantParams{
|
||||
DocumentID: library.ID,
|
||||
SubjectID: other.ID,
|
||||
Permissions: acl.PermissionRead,
|
||||
GrantedBy: owner.ID,
|
||||
}); err != nil {
|
||||
t.Fatalf("Grant(Read) : %v", err)
|
||||
}
|
||||
|
||||
if _, err := node.Documents().Get(ctx, other.ID, library.ID); err != nil {
|
||||
t.Fatalf("Get avec Read : %v", err)
|
||||
}
|
||||
|
||||
libraries, err := node.Documents().ListLibraries(ctx, other.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("ListLibraries : %v", err)
|
||||
}
|
||||
if len(libraries) != 1 || libraries[0].ID != library.ID {
|
||||
t.Fatalf("bibliothèques = %+v", libraries)
|
||||
}
|
||||
|
||||
if _, err := node.Documents().CreateFolder(ctx, other.ID, document.CreateFolderParams{
|
||||
OwnerID: other.ID,
|
||||
CreatedBy: other.ID,
|
||||
ParentID: library.ID,
|
||||
Name: "Vacances",
|
||||
}); !errors.Is(err, document.ErrPermissionDenied) {
|
||||
t.Fatalf("CreateFolder sans Create : erreur = %v, attendu ErrPermissionDenied", err)
|
||||
}
|
||||
|
||||
if _, err := node.ACL().Grant(ctx, acl.GrantParams{
|
||||
DocumentID: library.ID,
|
||||
SubjectID: other.ID,
|
||||
Permissions: acl.PermissionCreate,
|
||||
GrantedBy: owner.ID,
|
||||
}); err != nil {
|
||||
t.Fatalf("Grant(Create) : %v", err)
|
||||
}
|
||||
|
||||
if _, err := node.Documents().CreateFolder(ctx, other.ID, document.CreateFolderParams{
|
||||
OwnerID: other.ID,
|
||||
CreatedBy: other.ID,
|
||||
ParentID: library.ID,
|
||||
Name: "Vacances",
|
||||
}); err != nil {
|
||||
t.Fatalf("CreateFolder avec Create : %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package permission
|
||||
|
||||
type Permission int64
|
||||
|
||||
const (
|
||||
Read Permission = 1 << iota
|
||||
Write
|
||||
Create
|
||||
Delete
|
||||
Share
|
||||
Manage
|
||||
Admin
|
||||
)
|
||||
|
||||
const All = Read |
|
||||
Write |
|
||||
Create |
|
||||
Delete |
|
||||
Share |
|
||||
Manage |
|
||||
Admin
|
||||
|
||||
func (p Permission) Valid() bool {
|
||||
return p > 0 && p <= All
|
||||
}
|
||||
|
||||
func (p Permission) Has(required Permission) bool {
|
||||
if required == 0 {
|
||||
return true
|
||||
}
|
||||
if !required.Valid() {
|
||||
return false
|
||||
}
|
||||
return p&Admin != 0 || p&required == required
|
||||
}
|
||||
Reference in New Issue
Block a user