Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fb61fd7

Browse files
committed
Return DisplayName and Name in the roles endpoint
1 parent 57bb108 commit fb61fd7

File tree

6 files changed

+60
-30
lines changed

6 files changed

+60
-30
lines changed

coderd/rbac/builtin.go

Lines changed: 10 additions & 5 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
},

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: 5 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"
@@ -13,7 +14,8 @@ import (
1314
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.
16-
roles := rbac.SiteRoles()
17+
roleNames := rbac.SiteRoles()
18+
roles := codersdk.RolesFromName(roleNames)
1719
httpapi.Write(rw, http.StatusOK, roles)
1820
}
1921

@@ -22,6 +24,7 @@ func (*api) assignableOrgRoles(rw http.ResponseWriter, r *http.Request) {
2224
// TODO: @emyrk in the future, allow granular subsets of roles to be returned based on the
2325
// role of the user.
2426
organization := httpmw.OrganizationParam(r)
25-
roles := rbac.OrganizationRoles(organization.ID)
27+
roleNames := rbac.OrganizationRoles(organization.ID)
28+
roles := codersdk.RolesFromName(roleNames)
2629
httpapi.Write(rw, http.StatusOK, roles)
2730
}

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.RolesFromName(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.RolesFromName(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.RolesFromName(rbac.OrganizationRoles(admin.OrganizationID)),
110110
},
111111
}
112112

coderd/users.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,10 +361,10 @@ func (api *api) putUserSuspend(rw http.ResponseWriter, r *http.Request) {
361361
}
362362

363363
func (api *api) putUserPassword(rw http.ResponseWriter, r *http.Request) {
364-
var (
365-
user = httpmw.UserParam(r)
366-
params codersdk.UpdateUserPasswordRequest
367-
)
364+
var (
365+
user = httpmw.UserParam(r)
366+
params codersdk.UpdateUserPasswordRequest
367+
)
368368
if !httpapi.Read(rw, r, &params) {
369369
return
370370
}

codersdk/roles.go

Lines changed: 25 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,21 @@ 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 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,
57+
DisplayName: role.DisplayName,
58+
})
59+
}
60+
return roles
61+
}

0 commit comments

Comments
 (0)