Skip to content

Commit 0080658

Browse files
refactor: Return the display_name and name in the roles endpoint (#1328)
1 parent 97ee560 commit 0080658

File tree

6 files changed

+71
-35
lines changed

6 files changed

+71
-35
lines changed

coderd/rbac/builtin.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ var (
5151
// admin grants all actions to all resources.
5252
admin: func(_ string) Role {
5353
return Role{
54-
Name: admin,
54+
Name: admin,
55+
DisplayName: "Admin",
5556
Site: permissions(map[Object][]Action{
5657
ResourceWildcard: {WildcardSymbol},
5758
}),
@@ -61,7 +62,8 @@ var (
6162
// member grants all actions to all resources owned by the user
6263
member: func(_ string) Role {
6364
return Role{
64-
Name: member,
65+
Name: member,
66+
DisplayName: "Member",
6567
User: permissions(map[Object][]Action{
6668
ResourceWildcard: {WildcardSymbol},
6769
}),
@@ -73,7 +75,8 @@ var (
7375
// TODO: Finish the auditor as we add resources.
7476
auditor: func(_ string) Role {
7577
return Role{
76-
Name: "auditor",
78+
Name: "auditor",
79+
DisplayName: "Auditor",
7780
Site: permissions(map[Object][]Action{
7881
// Should be able to read all template details, even in orgs they
7982
// are not in.
@@ -86,7 +89,8 @@ var (
8689
// organization scope.
8790
orgAdmin: func(organizationID string) Role {
8891
return Role{
89-
Name: roleName(orgAdmin, organizationID),
92+
Name: roleName(orgAdmin, organizationID),
93+
DisplayName: "Organization Admin",
9094
Org: map[string][]Permission{
9195
organizationID: {
9296
{
@@ -104,7 +108,8 @@ var (
104108
// in an organization.
105109
orgMember: func(organizationID string) Role {
106110
return Role{
107-
Name: roleName(orgMember, organizationID),
111+
Name: roleName(orgMember, organizationID),
112+
DisplayName: "Organization Member",
108113
Org: map[string][]Permission{
109114
organizationID: {},
110115
},
@@ -151,11 +156,11 @@ func IsOrgRole(roleName string) (string, bool) {
151156
//
152157
// This should be a list in a database, but until then we build
153158
// the list from the builtins.
154-
func OrganizationRoles(organizationID uuid.UUID) []string {
155-
var roles []string
159+
func OrganizationRoles(organizationID uuid.UUID) []Role {
160+
var roles []Role
156161
for _, roleF := range builtInRoles {
157-
role := roleF(organizationID.String()).Name
158-
_, scope, err := roleSplit(role)
162+
role := roleF(organizationID.String())
163+
_, scope, err := roleSplit(role.Name)
159164
if err != nil {
160165
// This should never happen
161166
continue
@@ -172,8 +177,8 @@ func OrganizationRoles(organizationID uuid.UUID) []string {
172177
//
173178
// This should be a list in a database, but until then we build
174179
// the list from the builtins.
175-
func SiteRoles() []string {
176-
var roles []string
180+
func SiteRoles() []Role {
181+
var roles []Role
177182
for _, roleF := range builtInRoles {
178183
role := roleF("random")
179184
_, scope, err := roleSplit(role.Name)
@@ -182,7 +187,7 @@ func SiteRoles() []string {
182187
continue
183188
}
184189
if scope == "" {
185-
roles = append(roles, role.Name)
190+
roles = append(roles, role)
186191
}
187192
}
188193
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/rbac/role.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ type Permission struct {
1717
// Users of this package should instead **only** use the role names, and
1818
// this package will expand the role names into their json payloads.
1919
type Role struct {
20-
Name string `json:"name"`
21-
Site []Permission `json:"site"`
20+
Name string `json:"name"`
21+
DisplayName string `json:"display_name"`
22+
Site []Permission `json:"site"`
2223
// Org is a map of orgid to permissions. We represent orgid as a string.
2324
// We scope the organizations in the role so we can easily combine all the
2425
// roles.

coderd/roles.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"net/http"
55

66
"github.com/coder/coder/coderd/httpmw"
7+
"github.com/coder/coder/codersdk"
78

89
"github.com/coder/coder/coderd/httpapi"
910
"github.com/coder/coder/coderd/rbac"
@@ -14,7 +15,7 @@ func (*api) assignableSiteRoles(rw http.ResponseWriter, _ *http.Request) {
1415
// TODO: @emyrk in the future, allow granular subsets of roles to be returned based on the
1516
// role of the user.
1617
roles := rbac.SiteRoles()
17-
httpapi.Write(rw, http.StatusOK, roles)
18+
httpapi.Write(rw, http.StatusOK, codersdk.ConvertRoles(roles))
1819
}
1920

2021
// assignableSiteRoles returns all site wide roles that can be assigned.
@@ -23,5 +24,5 @@ func (*api) assignableOrgRoles(rw http.ResponseWriter, r *http.Request) {
2324
// role of the user.
2425
organization := httpmw.OrganizationParam(r)
2526
roles := rbac.OrganizationRoles(organization.ID)
26-
httpapi.Write(rw, http.StatusOK, roles)
27+
httpapi.Write(rw, http.StatusOK, codersdk.ConvertRoles(roles))
2728
}

coderd/roles_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,68 +45,68 @@ func TestListRoles(t *testing.T) {
4545
testCases := []struct {
4646
Name string
4747
Client *codersdk.Client
48-
APICall func() ([]string, error)
49-
ExpectedRoles []string
48+
APICall func() ([]codersdk.Role, error)
49+
ExpectedRoles []codersdk.Role
5050
AuthorizedError string
5151
}{
5252
{
5353
Name: "MemberListSite",
54-
APICall: func() ([]string, error) {
54+
APICall: func() ([]codersdk.Role, error) {
5555
x, err := member.ListSiteRoles(ctx)
5656
return x, err
5757
},
5858
AuthorizedError: unauth,
5959
},
6060
{
6161
Name: "OrgMemberListOrg",
62-
APICall: func() ([]string, error) {
62+
APICall: func() ([]codersdk.Role, error) {
6363
return member.ListOrganizationRoles(ctx, admin.OrganizationID)
6464
},
6565
AuthorizedError: unauth,
6666
},
6767
{
6868
Name: "NonOrgMemberListOrg",
69-
APICall: func() ([]string, error) {
69+
APICall: func() ([]codersdk.Role, error) {
7070
return member.ListOrganizationRoles(ctx, otherOrg.ID)
7171
},
7272
AuthorizedError: notMember,
7373
},
7474
// Org admin
7575
{
7676
Name: "OrgAdminListSite",
77-
APICall: func() ([]string, error) {
77+
APICall: func() ([]codersdk.Role, error) {
7878
return orgAdmin.ListSiteRoles(ctx)
7979
},
8080
AuthorizedError: unauth,
8181
},
8282
{
8383
Name: "OrgAdminListOrg",
84-
APICall: func() ([]string, error) {
84+
APICall: func() ([]codersdk.Role, error) {
8585
return orgAdmin.ListOrganizationRoles(ctx, admin.OrganizationID)
8686
},
87-
ExpectedRoles: rbac.OrganizationRoles(admin.OrganizationID),
87+
ExpectedRoles: codersdk.ConvertRoles(rbac.OrganizationRoles(admin.OrganizationID)),
8888
},
8989
{
9090
Name: "OrgAdminListOtherOrg",
91-
APICall: func() ([]string, error) {
91+
APICall: func() ([]codersdk.Role, error) {
9292
return orgAdmin.ListOrganizationRoles(ctx, otherOrg.ID)
9393
},
9494
AuthorizedError: notMember,
9595
},
9696
// Admin
9797
{
9898
Name: "AdminListSite",
99-
APICall: func() ([]string, error) {
99+
APICall: func() ([]codersdk.Role, error) {
100100
return client.ListSiteRoles(ctx)
101101
},
102-
ExpectedRoles: rbac.SiteRoles(),
102+
ExpectedRoles: codersdk.ConvertRoles(rbac.SiteRoles()),
103103
},
104104
{
105105
Name: "AdminListOrg",
106-
APICall: func() ([]string, error) {
106+
APICall: func() ([]codersdk.Role, error) {
107107
return client.ListOrganizationRoles(ctx, admin.OrganizationID)
108108
},
109-
ExpectedRoles: rbac.OrganizationRoles(admin.OrganizationID),
109+
ExpectedRoles: codersdk.ConvertRoles(rbac.OrganizationRoles(admin.OrganizationID)),
110110
},
111111
}
112112

codersdk/roles.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@ import (
66
"fmt"
77
"net/http"
88

9+
"github.com/coder/coder/coderd/rbac"
910
"github.com/google/uuid"
1011
)
1112

13+
type Role struct {
14+
Name string `json:"name"`
15+
DisplayName string `json:"display_name"`
16+
}
17+
1218
// ListSiteRoles lists all available site wide roles.
1319
// This is not user specific.
14-
func (c *Client) ListSiteRoles(ctx context.Context) ([]string, error) {
20+
func (c *Client) ListSiteRoles(ctx context.Context) ([]Role, error) {
1521
res, err := c.request(ctx, http.MethodGet, "/api/v2/users/roles", nil)
1622
if err != nil {
1723
return nil, err
@@ -20,13 +26,13 @@ func (c *Client) ListSiteRoles(ctx context.Context) ([]string, error) {
2026
if res.StatusCode != http.StatusOK {
2127
return nil, readBodyAsError(res)
2228
}
23-
var roles []string
29+
var roles []Role
2430
return roles, json.NewDecoder(res.Body).Decode(&roles)
2531
}
2632

2733
// ListOrganizationRoles lists all available roles for a given organization.
2834
// This is not user specific.
29-
func (c *Client) ListOrganizationRoles(ctx context.Context, org uuid.UUID) ([]string, error) {
35+
func (c *Client) ListOrganizationRoles(ctx context.Context, org uuid.UUID) ([]Role, error) {
3036
res, err := c.request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/organizations/%s/members/roles/", org.String()), nil)
3137
if err != nil {
3238
return nil, err
@@ -35,6 +41,17 @@ func (c *Client) ListOrganizationRoles(ctx context.Context, org uuid.UUID) ([]st
3541
if res.StatusCode != http.StatusOK {
3642
return nil, readBodyAsError(res)
3743
}
38-
var roles []string
44+
var roles []Role
3945
return roles, json.NewDecoder(res.Body).Decode(&roles)
4046
}
47+
48+
func ConvertRoles(roles []rbac.Role) []Role {
49+
converted := make([]Role, 0, len(roles))
50+
for _, role := range roles {
51+
converted = append(converted, Role{
52+
DisplayName: role.DisplayName,
53+
Name: role.Name,
54+
})
55+
}
56+
return converted
57+
}

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