Expose default permissions for system RBAC roles in role detail API.
Seed SUPER_ADMIN from ADMIN plus team invitation permissions, and return coded defaults when role_permissions rows are missing. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
df4f4d8ade
commit
85fd535bb1
|
|
@ -414,6 +414,7 @@ var defaultStudentLearnerPermissions = []string{
|
|||
|
||||
// DefaultRolePermissions maps each system role to the permission keys it should
|
||||
// have by default. This preserves the previous middleware behavior:
|
||||
// - SUPER_ADMIN: ADMIN defaults plus team email invitation permissions
|
||||
// - ADMIN: everything that was previously OnlyAdminAndAbove + SuperAdminOnly + all authenticated routes
|
||||
// - STUDENT/OPEN_LEARNER/INSTRUCTOR/SUPPORT: only self-service endpoints (profile, courses, progress, etc.)
|
||||
var DefaultRolePermissions = map[string][]string{
|
||||
|
|
@ -698,3 +699,11 @@ var DefaultRolePermissions = map[string][]string{
|
|||
"auth.logout",
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
adminKeys := DefaultRolePermissions["ADMIN"]
|
||||
superKeys := make([]string, len(adminKeys), len(adminKeys)+2)
|
||||
copy(superKeys, adminKeys)
|
||||
superKeys = append(superKeys, "team.members.invite", "team.invitations.resend")
|
||||
DefaultRolePermissions["SUPER_ADMIN"] = superKeys
|
||||
}
|
||||
|
|
|
|||
|
|
@ -166,5 +166,38 @@ func (s *Service) SetRolePermissions(ctx context.Context, roleID int64, permissi
|
|||
}
|
||||
|
||||
func (s *Service) GetRolePermissions(ctx context.Context, roleID int64) ([]domain.Permission, error) {
|
||||
return s.store.GetRolePermissions(ctx, roleID)
|
||||
perms, err := s.store.GetRolePermissions(ctx, roleID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(perms) > 0 {
|
||||
return perms, nil
|
||||
}
|
||||
role, err := s.store.GetRoleByID(ctx, roleID)
|
||||
if err != nil || !role.IsSystem {
|
||||
return perms, nil
|
||||
}
|
||||
keys, ok := DefaultRolePermissions[role.Name]
|
||||
if !ok || len(keys) == 0 {
|
||||
return perms, nil
|
||||
}
|
||||
return s.permissionsFromDefaultKeys(ctx, keys)
|
||||
}
|
||||
|
||||
func (s *Service) permissionsFromDefaultKeys(ctx context.Context, keys []string) ([]domain.Permission, error) {
|
||||
out := make([]domain.Permission, 0, len(keys))
|
||||
seen := make(map[int64]struct{}, len(keys))
|
||||
for _, key := range keys {
|
||||
p, err := s.store.GetPermissionByKey(ctx, key)
|
||||
if err != nil {
|
||||
s.logger.Warn("default role permission key missing", "key", key)
|
||||
continue
|
||||
}
|
||||
if _, dup := seen[p.ID]; dup {
|
||||
continue
|
||||
}
|
||||
seen[p.ID] = struct{}{}
|
||||
out = append(out, p)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user