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

Commit85d6f69

Browse files
committed
Return the Role struct instead of only the name
1 parentfb61fd7 commit85d6f69

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-
funcOrganizationRoles(organizationID uuid.UUID) []string {
160-
varroles []string
159+
funcOrganizationRoles(organizationID uuid.UUID) []Role {
160+
varroles []Role
161161
for_,roleF:=rangebuiltInRoles {
162-
role:=roleF(organizationID.String()).Name
163-
_,scope,err:=roleSplit(role)
162+
role:=roleF(organizationID.String())
163+
_,scope,err:=roleSplit(role.Name)
164164
iferr!=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-
funcSiteRoles() []string {
181-
varroles []string
180+
funcSiteRoles() []Role {
181+
varroles []Role
182182
for_,roleF:=rangebuiltInRoles {
183183
role:=roleF("random")
184184
_,scope,err:=roleSplit(role.Name)
@@ -187,7 +187,7 @@ func SiteRoles() []string {
187187
continue
188188
}
189189
ifscope=="" {
190-
roles=append(roles,role.Name)
190+
roles=append(roles,role)
191191
}
192192
}
193193
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/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
returnorgAdmin.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
returnclient.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
returnclient.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
returnroles,json.NewDecoder(res.Body).Decode(&roles)
4646
}
4747

48-
funcRolesFromName(roleNames []string) []Role {
49-
roles:=make([]Role,0,len(roleNames))
50-
for_,roleName:=rangeroleNames {
51-
role,err:=rbac.RoleByName(roleName)
52-
iferr!=nil {
53-
continue
54-
}
55-
roles=append(roles,Role{
56-
Name:role.Name,
48+
funcConvertRoles(roles []rbac.Role) []Role {
49+
converted:=make([]Role,0,len(roles))
50+
for_,role:=rangeroles {
51+
converted=append(converted,Role{
5752
DisplayName:role.DisplayName,
53+
Name:role.Name,
5854
})
5955
}
60-
returnroles
56+
returnconverted
6157
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp