Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit15fadb1

Browse files
BrunoQuaresmakylecarbs
authored andcommitted
refactor: Return the display_name and name in the roles endpoint (#1328)
1 parentc5d349f commit15fadb1

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
returnRole{
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
returnRole{
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
returnRole{
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(organizationIDstring)Role {
8891
returnRole{
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(organizationIDstring)Role {
106110
returnRole{
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-
funcOrganizationRoles(organizationID uuid.UUID) []string {
155-
varroles []string
159+
funcOrganizationRoles(organizationID uuid.UUID) []Role {
160+
varroles []Role
156161
for_,roleF:=rangebuiltInRoles {
157-
role:=roleF(organizationID.String()).Name
158-
_,scope,err:=roleSplit(role)
162+
role:=roleF(organizationID.String())
163+
_,scope,err:=roleSplit(role.Name)
159164
iferr!=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-
funcSiteRoles() []string {
176-
varroles []string
180+
funcSiteRoles() []Role {
181+
varroles []Role
177182
for_,roleF:=rangebuiltInRoles {
178183
role:=roleF("random")
179184
_,scope,err:=roleSplit(role.Name)
@@ -182,7 +187,7 @@ func SiteRoles() []string {
182187
continue
183188
}
184189
ifscope=="" {
185-
roles=append(roles,role.Name)
190+
roles=append(roles,role)
186191
}
187192
}
188193
returnroles

‎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
funcTestListRoles(t*testing.T) {
6666
t.Parallel()
6767

68+
siteRoles:=rbac.SiteRoles()
69+
siteRoleNames:=make([]string,0,len(siteRoles))
70+
for_,role:=rangesiteRoles {
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:=rangeorgRoles {
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
typeRolestruct {
20-
Namestring`json:"name"`
21-
Site []Permission`json:"site"`
20+
Namestring`json:"name"`
21+
DisplayNamestring`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
Namestring
4747
Client*codersdk.Client
48-
APICallfunc() ([]string,error)
49-
ExpectedRoles []string
48+
APICallfunc() ([]codersdk.Role,error)
49+
ExpectedRoles []codersdk.Role
5050
AuthorizedErrorstring
5151
}{
5252
{
5353
Name:"MemberListSite",
54-
APICall:func() ([]string,error) {
54+
APICall:func() ([]codersdk.Role,error) {
5555
x,err:=member.ListSiteRoles(ctx)
5656
returnx,err
5757
},
5858
AuthorizedError:unauth,
5959
},
6060
{
6161
Name:"OrgMemberListOrg",
62-
APICall:func() ([]string,error) {
62+
APICall:func() ([]codersdk.Role,error) {
6363
returnmember.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
returnmember.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
returnorgAdmin.ListSiteRoles(ctx)
7979
},
8080
AuthorizedError:unauth,
8181
},
8282
{
8383
Name:"OrgAdminListOrg",
84-
APICall:func() ([]string,error) {
84+
APICall:func() ([]codersdk.Role,error) {
8585
returnorgAdmin.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
returnorgAdmin.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
returnclient.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
returnclient.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+
typeRolestruct {
14+
Namestring`json:"name"`
15+
DisplayNamestring`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
iferr!=nil {
1723
returnnil,err
@@ -20,13 +26,13 @@ func (c *Client) ListSiteRoles(ctx context.Context) ([]string, error) {
2026
ifres.StatusCode!=http.StatusOK {
2127
returnnil,readBodyAsError(res)
2228
}
23-
varroles []string
29+
varroles []Role
2430
returnroles,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
iferr!=nil {
3238
returnnil,err
@@ -35,6 +41,17 @@ func (c *Client) ListOrganizationRoles(ctx context.Context, org uuid.UUID) ([]st
3541
ifres.StatusCode!=http.StatusOK {
3642
returnnil,readBodyAsError(res)
3743
}
38-
varroles []string
44+
varroles []Role
3945
returnroles,json.NewDecoder(res.Body).Decode(&roles)
4046
}
47+
48+
funcConvertRoles(roles []rbac.Role) []Role {
49+
converted:=make([]Role,0,len(roles))
50+
for_,role:=rangeroles {
51+
converted=append(converted,Role{
52+
DisplayName:role.DisplayName,
53+
Name:role.Name,
54+
})
55+
}
56+
returnconverted
57+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp