@@ -21,38 +21,13 @@ import (
2121 "slices"
2222 "strings"
2323
24+ "github.com/unikorn-cloud/core/pkg/authorization/rbac"
2425 "github.com/unikorn-cloud/core/pkg/authorization/roles"
2526 unikornv1 "github.com/unikorn-cloud/identity/pkg/apis/unikorn/v1alpha1"
2627
2728 "sigs.k8s.io/controller-runtime/pkg/client"
2829)
2930
30- // GroupPermissions are privilege grants for a project.
31- type GroupPermissions struct {
32- // ID is the unique, immutable project identifier.
33- ID string `json:"id"`
34- // Roles are the privileges a user has for the group.
35- Roles []roles.Role `json:"roles"`
36- }
37-
38- // OrganizationPermissions are privilege grants for an organization.
39- type OrganizationPermissions struct {
40- // IsAdmin allows the user to play with all resources in an organization.
41- IsAdmin bool `json:"isAdmin,omitempty"`
42- // Name is the name of the organization.
43- Name string `json:"name"`
44- // Groups are any groups the user belongs to in an organization.
45- Groups []GroupPermissions `json:"groups,omitempty"`
46- }
47-
48- // Permissions are privilege grants for the entire system.
49- type Permissions struct {
50- // IsSuperAdmin HAS SUPER COW POWERS!!!
51- IsSuperAdmin bool `json:"isSuperAdmin,omitempty"`
52- // Organizations are any organizations the user has access to.
53- Organizations []OrganizationPermissions `json:"organizations,omitempty"`
54- }
55-
5631// RBAC contains all the scoping rules for services across the platform.
5732type RBAC struct {
5833 client client.Client
@@ -80,16 +55,20 @@ func (r *RBAC) GetOrganizatons(ctx context.Context) (*unikornv1.OrganizationList
8055
8156// UserPermissions builds up a hierarchy of permissions for a user, this is used
8257// both internally and given out to resource servers via token introspection.
83- func (r * RBAC ) UserPermissions (ctx context.Context , email string ) (* Permissions , error ) {
84- permissions := & Permissions {}
58+ //
59+ //nolint:cyclop
60+ func (r * RBAC ) UserPermissions (ctx context.Context , email string ) (* rbac.Permissions , error ) {
61+ permissions := & rbac.Permissions {}
8562
8663 organizations , err := r .GetOrganizatons (ctx )
8764 if err != nil {
8865 return nil , err
8966 }
9067
9168 for _ , organization := range organizations .Items {
92- organizationPermissions := OrganizationPermissions {}
69+ var isAdmin bool
70+
71+ var groups []rbac.GroupPermissions
9372
9473 for _ , group := range organization .Spec .Groups {
9574 // TODO: implicit groups.
@@ -104,18 +83,33 @@ func (r *RBAC) UserPermissions(ctx context.Context, email string) (*Permissions,
10483
10584 // Hoist admin powers.
10685 if slices .Contains (group .Roles , roles .Admin ) {
107- organizationPermissions . IsAdmin = true
86+ isAdmin = true
10887 }
10988
110- organizationPermissions .Groups = append (organizationPermissions .Groups , GroupPermissions {
89+ // Remove any special roles.
90+ minifiedRoles := slices .DeleteFunc (group .Roles , func (role roles.Role ) bool {
91+ return role == roles .SuperAdmin || role == roles .Admin
92+ })
93+
94+ if len (minifiedRoles ) == 0 {
95+ continue
96+ }
97+
98+ groups = append (groups , rbac.GroupPermissions {
11199 ID : group .ID ,
112- Roles : group . Roles ,
100+ Roles : minifiedRoles ,
113101 })
114102 }
115103
116- if organizationPermissions . IsAdmin || len (organizationPermissions . Groups ) > 0 {
117- permissions . Organizations = append ( permissions . Organizations , organizationPermissions )
104+ if ! isAdmin && len (groups ) == 0 {
105+ continue
118106 }
107+
108+ permissions .Organizations = append (permissions .Organizations , rbac.OrganizationPermissions {
109+ Name : organization .Name ,
110+ IsAdmin : isAdmin ,
111+ Groups : groups ,
112+ })
119113 }
120114
121115 return permissions , nil
0 commit comments