-
Notifications
You must be signed in to change notification settings - Fork 0
/
role_permissions_expander.go
41 lines (33 loc) · 1.44 KB
/
role_permissions_expander.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package xroles
import (
"github.com/totemcaf/gollections/slices"
)
// RolesRepository provides roles and their corresponding permissions
type RolesRepository interface {
// FindByRole returns the permissions for the given role and true if the role exists.
// If the role does not exist, it returns an empty slice and false.
FindByRole(role string) ([]string, bool)
}
// ExpandRolPermissions returns the given permissions and roles expanded to its corresponding permissions.
// allPermissions by role are provided by RolesRepository
// Roles are expanded recursively.
type ExpandRolPermissions = func(permissionsOrRoles []string) []string
type rolePermissionsExpander struct {
RolesRepository
}
// NewRolePermissionsExpander returns a function that returns the given permissions and
// roles expanded to its corresponding permissions.
// allPermissions by role are provided by RolesRepository
func NewRolePermissionsExpander(rolesRepository RolesRepository) ExpandRolPermissions {
expander := &rolePermissionsExpander{RolesRepository: rolesRepository}
return expander.expandRoles
}
func (f *rolePermissionsExpander) expandRoles(permissions []string) []string {
return slices.JoinDistinct(slices.Map(permissions, f.expandRole)...)
}
func (f *rolePermissionsExpander) expandRole(permissionOrRole string) []string {
if permissions, found := f.RolesRepository.FindByRole(permissionOrRole); found {
return permissions
}
return []string{permissionOrRole}
}