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