Skip to content

Commit 85d6f69

Browse files
committed
Return the Role struct instead of only the name
1 parent fb61fd7 commit 85d6f69

File tree

5 files changed

+34
-28
lines changed

5 files changed

+34
-28
lines changed

coderd/rbac/builtin.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,11 @@ func IsOrgRole(roleName string) (string, bool) {
156156
//
157157
// This should be a list in a database, but until then we build
158158
// the list from the builtins.
159-
func OrganizationRoles(organizationID uuid.UUID) []string {
160-
var roles []string
159+
func OrganizationRoles(organizationID uuid.UUID) []Role {
160+
var roles []Role
161161
for _, roleF := range builtInRoles {
162-
role := roleF(organizationID.String()).Name
163-
_, scope, err := roleSplit(role)
162+
role := roleF(organizationID.String())
163+
_, scope, err := roleSplit(role.Name)
164164
if err != nil {
165165
// This should never happen
166166
continue
@@ -177,8 +177,8 @@ func OrganizationRoles(organizationID uuid.UUID) []string {
177177
//
178178
// This should be a list in a database, but until then we build
179179
// the list from the builtins.
180-
func SiteRoles() []string {
181-
var roles []string
180+
func SiteRoles() []Role {
181+
var roles []Role
182182
for _, roleF := range builtInRoles {
183183
role := roleF("random")
184184
_, scope, err := roleSplit(role.Name)
@@ -187,7 +187,7 @@ func SiteRoles() []string {
187187
continue
188188
}
189189
if scope == "" {
190-
roles = append(roles, role.Name)
190+
roles = append(roles, role)
191191
}
192192
}
193193
return roles

coderd/rbac/builtin_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,31 @@ func TestIsOrgRole(t *testing.T) {
6565
func TestListRoles(t *testing.T) {
6666
t.Parallel()
6767

68+
siteRoles := rbac.SiteRoles()
69+
siteRoleNames := make([]string, 0, len(siteRoles))
70+
for _, role := range siteRoles {
71+
siteRoleNames = append(siteRoleNames, role.Name)
72+
}
73+
6874
// If this test is ever failing, just update the list to the roles
6975
// expected from the builtin set.
7076
require.ElementsMatch(t, []string{
7177
"admin",
7278
"member",
7379
"auditor",
7480
},
75-
rbac.SiteRoles())
81+
siteRoleNames)
7682

7783
orgID := uuid.New()
84+
orgRoles := rbac.OrganizationRoles(orgID)
85+
orgRoleNames := make([]string, 0, len(orgRoles))
86+
for _, role := range orgRoles {
87+
orgRoleNames = append(orgRoleNames, role.Name)
88+
}
89+
7890
require.ElementsMatch(t, []string{
7991
fmt.Sprintf("organization-admin:%s", orgID.String()),
8092
fmt.Sprintf("organization-member:%s", orgID.String()),
8193
},
82-
rbac.OrganizationRoles(orgID))
94+
orgRoleNames)
8395
}

coderd/roles.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,15 @@ import (
1414
func (*api) assignableSiteRoles(rw http.ResponseWriter, _ *http.Request) {
1515
// TODO: @emyrk in the future, allow granular subsets of roles to be returned based on the
1616
// role of the user.
17-
roleNames := rbac.SiteRoles()
18-
roles := codersdk.RolesFromName(roleNames)
19-
httpapi.Write(rw, http.StatusOK, roles)
17+
roles := rbac.SiteRoles()
18+
httpapi.Write(rw, http.StatusOK, codersdk.ConvertRoles(roles))
2019
}
2120

2221
// assignableSiteRoles returns all site wide roles that can be assigned.
2322
func (*api) assignableOrgRoles(rw http.ResponseWriter, r *http.Request) {
2423
// TODO: @emyrk in the future, allow granular subsets of roles to be returned based on the
2524
// role of the user.
2625
organization := httpmw.OrganizationParam(r)
27-
roleNames := rbac.OrganizationRoles(organization.ID)
28-
roles := codersdk.RolesFromName(roleNames)
29-
httpapi.Write(rw, http.StatusOK, roles)
26+
roles := rbac.OrganizationRoles(organization.ID)
27+
httpapi.Write(rw, http.StatusOK, codersdk.ConvertRoles(roles))
3028
}

coderd/roles_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func TestListRoles(t *testing.T) {
8484
APICall: func() ([]codersdk.Role, error) {
8585
return orgAdmin.ListOrganizationRoles(ctx, admin.OrganizationID)
8686
},
87-
ExpectedRoles: codersdk.RolesFromName(rbac.OrganizationRoles(admin.OrganizationID)),
87+
ExpectedRoles: codersdk.ConvertRoles(rbac.OrganizationRoles(admin.OrganizationID)),
8888
},
8989
{
9090
Name: "OrgAdminListOtherOrg",
@@ -99,14 +99,14 @@ func TestListRoles(t *testing.T) {
9999
APICall: func() ([]codersdk.Role, error) {
100100
return client.ListSiteRoles(ctx)
101101
},
102-
ExpectedRoles: codersdk.RolesFromName(rbac.SiteRoles()),
102+
ExpectedRoles: codersdk.ConvertRoles(rbac.SiteRoles()),
103103
},
104104
{
105105
Name: "AdminListOrg",
106106
APICall: func() ([]codersdk.Role, error) {
107107
return client.ListOrganizationRoles(ctx, admin.OrganizationID)
108108
},
109-
ExpectedRoles: codersdk.RolesFromName(rbac.OrganizationRoles(admin.OrganizationID)),
109+
ExpectedRoles: codersdk.ConvertRoles(rbac.OrganizationRoles(admin.OrganizationID)),
110110
},
111111
}
112112

codersdk/roles.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,13 @@ func (c *Client) ListOrganizationRoles(ctx context.Context, org uuid.UUID) ([]Ro
4545
return roles, json.NewDecoder(res.Body).Decode(&roles)
4646
}
4747

48-
func RolesFromName(roleNames []string) []Role {
49-
roles := make([]Role, 0, len(roleNames))
50-
for _, roleName := range roleNames {
51-
role, err := rbac.RoleByName(roleName)
52-
if err != nil {
53-
continue
54-
}
55-
roles = append(roles, Role{
56-
Name: role.Name,
48+
func ConvertRoles(roles []rbac.Role) []Role {
49+
converted := make([]Role, 0, len(roles))
50+
for _, role := range roles {
51+
converted = append(converted, Role{
5752
DisplayName: role.DisplayName,
53+
Name: role.Name,
5854
})
5955
}
60-
return roles
56+
return converted
6157
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy