acl
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user