settings: Add ListRoleAssignmentByRole

This adds the implementation for ListRolesAssignments by role-id to the
metadata backend. Because of the current layout of the account folders
and assignment files this is currently still very inefficient.

Related Issue: #8939
This commit is contained in:
Ralf Haferkamp
2024-06-04 11:00:09 +02:00
committed by Ralf Haferkamp
parent 90f7cc23f4
commit d7f10f38a0
6 changed files with 308 additions and 4 deletions
@@ -62,6 +62,59 @@ func (s *Store) ListRoleAssignments(accountUUID string) ([]*settingsmsg.UserRole
return ass, nil
}
// ListRoleAssignmentsByRole returns all role assignmentes matching the give roleID
func (s *Store) ListRoleAssignmentsByRole(roleID string) ([]*settingsmsg.UserRoleAssignment, error) {
s.Init()
ctx := context.TODO()
accountIDs, err := s.mdc.ReadDir(ctx, accountsFolderLocation)
switch err.(type) {
case nil:
// continue
case errtypes.NotFound:
return make([]*settingsmsg.UserRoleAssignment, 0), nil
default:
return nil, err
}
assignments := make([]*settingsmsg.UserRoleAssignment, 0, len(accountIDs))
// This is very inefficient, with the current layout we need to iterated through all
// account folders and read each assignment file in there to check if that contains
// the give role ID.
for _, account := range accountIDs {
assignmentIDs, err := s.mdc.ReadDir(ctx, accountPath(account))
switch err.(type) {
case nil:
// continue
case errtypes.NotFound:
return make([]*settingsmsg.UserRoleAssignment, 0), nil
default:
return nil, err
}
for _, assignmentID := range assignmentIDs {
b, err := s.mdc.SimpleDownload(ctx, assignmentPath(account, assignmentID))
switch err.(type) {
case nil:
// continue
case errtypes.NotFound:
continue
default:
return nil, err
}
a := &settingsmsg.UserRoleAssignment{}
err = json.Unmarshal(b, a)
if err != nil {
return nil, err
}
if a.GetRoleId() == roleID {
assignments = append(assignments, a)
}
}
}
return assignments, nil
}
// WriteRoleAssignment appends the given role assignment to the existing assignments of the respective account.
func (s *Store) WriteRoleAssignment(accountUUID, roleID string) (*settingsmsg.UserRoleAssignment, error) {
s.Init()
@@ -14,8 +14,12 @@ import (
)
var (
einstein = "a4d07560-a670-4be9-8d60-9b547751a208"
//marie = "3c054db3-eec1-4ca4-b985-bc56dcf560cb"
einstein = "00000000-0000-0000-0000-000000000001"
marie = "00000000-0000-0000-0000-000000000002"
moss = "00000000-0000-0000-0000-000000000003"
role1 = "11111111-1111-1111-1111-111111111111"
role2 = "22222222-2222-2222-2222-222222222222"
s = &Store{
Logger: logger,
@@ -149,6 +153,79 @@ func TestAssignmentUniqueness(t *testing.T) {
}
}
func TestListRoleAssignmentByRole(t *testing.T) {
type assignment struct {
userID string
roleID string
}
var scenarios = map[string]struct {
assignments []assignment
queryRole string
numResults int
}{
"just 2 assignments": {
assignments: []assignment{
{
userID: einstein,
roleID: role1,
}, {
userID: marie,
roleID: role1,
},
},
queryRole: role1,
numResults: 2,
},
"no assignments match": {
assignments: []assignment{
{
userID: einstein,
roleID: role1,
}, {
userID: marie,
roleID: role1,
},
},
queryRole: role2,
numResults: 0,
},
"only one assignment matches": {
assignments: []assignment{
{
userID: einstein,
roleID: role1,
}, {
userID: marie,
roleID: role1,
}, {
userID: moss,
roleID: role2,
},
},
queryRole: role2,
numResults: 1,
},
}
for name, scenario := range scenarios {
scenario := scenario
t.Run(name, func(t *testing.T) {
for _, a := range scenario.assignments {
ass, err := s.WriteRoleAssignment(a.userID, a.roleID)
require.NoError(t, err)
require.Equal(t, ass.RoleId, a.roleID)
}
list, err := s.ListRoleAssignmentsByRole(scenario.queryRole)
require.NoError(t, err)
require.Equal(t, scenario.numResults, len(list))
for _, ass := range list {
require.Equal(t, ass.RoleId, scenario.queryRole)
}
})
}
}
func TestDeleteAssignment(t *testing.T) {
var scenarios = []struct {
name string