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

Skip to content

refactor: Return DisplayName and Name in the roles endpoint #1328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Return the Role struct instead of only the name
  • Loading branch information
BrunoQuaresma committed May 6, 2022
commit 85d6f69344ae923ace900caddc89de6ef266ea02
14 changes: 7 additions & 7 deletions coderd/rbac/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@ func IsOrgRole(roleName string) (string, bool) {
//
// This should be a list in a database, but until then we build
// the list from the builtins.
func OrganizationRoles(organizationID uuid.UUID) []string {
var roles []string
func OrganizationRoles(organizationID uuid.UUID) []Role {
var roles []Role
for _, roleF := range builtInRoles {
role := roleF(organizationID.String()).Name
_, scope, err := roleSplit(role)
role := roleF(organizationID.String())
_, scope, err := roleSplit(role.Name)
if err != nil {
// This should never happen
continue
Expand All @@ -177,8 +177,8 @@ func OrganizationRoles(organizationID uuid.UUID) []string {
//
// This should be a list in a database, but until then we build
// the list from the builtins.
func SiteRoles() []string {
var roles []string
func SiteRoles() []Role {
var roles []Role
for _, roleF := range builtInRoles {
role := roleF("random")
_, scope, err := roleSplit(role.Name)
Expand All @@ -187,7 +187,7 @@ func SiteRoles() []string {
continue
}
if scope == "" {
roles = append(roles, role.Name)
roles = append(roles, role)
}
}
return roles
Expand Down
16 changes: 14 additions & 2 deletions coderd/rbac/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,31 @@ func TestIsOrgRole(t *testing.T) {
func TestListRoles(t *testing.T) {
t.Parallel()

siteRoles := rbac.SiteRoles()
siteRoleNames := make([]string, 0, len(siteRoles))
for _, role := range siteRoles {
siteRoleNames = append(siteRoleNames, role.Name)
}

// If this test is ever failing, just update the list to the roles
// expected from the builtin set.
require.ElementsMatch(t, []string{
"admin",
"member",
"auditor",
},
rbac.SiteRoles())
siteRoleNames)

orgID := uuid.New()
orgRoles := rbac.OrganizationRoles(orgID)
orgRoleNames := make([]string, 0, len(orgRoles))
for _, role := range orgRoles {
orgRoleNames = append(orgRoleNames, role.Name)
}

require.ElementsMatch(t, []string{
fmt.Sprintf("organization-admin:%s", orgID.String()),
fmt.Sprintf("organization-member:%s", orgID.String()),
},
rbac.OrganizationRoles(orgID))
orgRoleNames)
}
10 changes: 4 additions & 6 deletions coderd/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@ import (
func (*api) assignableSiteRoles(rw http.ResponseWriter, _ *http.Request) {
// TODO: @emyrk in the future, allow granular subsets of roles to be returned based on the
// role of the user.
roleNames := rbac.SiteRoles()
roles := codersdk.RolesFromName(roleNames)
httpapi.Write(rw, http.StatusOK, roles)
roles := rbac.SiteRoles()
httpapi.Write(rw, http.StatusOK, codersdk.ConvertRoles(roles))
}

// assignableSiteRoles returns all site wide roles that can be assigned.
func (*api) assignableOrgRoles(rw http.ResponseWriter, r *http.Request) {
// TODO: @emyrk in the future, allow granular subsets of roles to be returned based on the
// role of the user.
organization := httpmw.OrganizationParam(r)
roleNames := rbac.OrganizationRoles(organization.ID)
roles := codersdk.RolesFromName(roleNames)
httpapi.Write(rw, http.StatusOK, roles)
roles := rbac.OrganizationRoles(organization.ID)
httpapi.Write(rw, http.StatusOK, codersdk.ConvertRoles(roles))
}
6 changes: 3 additions & 3 deletions coderd/roles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestListRoles(t *testing.T) {
APICall: func() ([]codersdk.Role, error) {
return orgAdmin.ListOrganizationRoles(ctx, admin.OrganizationID)
},
ExpectedRoles: codersdk.RolesFromName(rbac.OrganizationRoles(admin.OrganizationID)),
ExpectedRoles: codersdk.ConvertRoles(rbac.OrganizationRoles(admin.OrganizationID)),
},
{
Name: "OrgAdminListOtherOrg",
Expand All @@ -99,14 +99,14 @@ func TestListRoles(t *testing.T) {
APICall: func() ([]codersdk.Role, error) {
return client.ListSiteRoles(ctx)
},
ExpectedRoles: codersdk.RolesFromName(rbac.SiteRoles()),
ExpectedRoles: codersdk.ConvertRoles(rbac.SiteRoles()),
},
{
Name: "AdminListOrg",
APICall: func() ([]codersdk.Role, error) {
return client.ListOrganizationRoles(ctx, admin.OrganizationID)
},
ExpectedRoles: codersdk.RolesFromName(rbac.OrganizationRoles(admin.OrganizationID)),
ExpectedRoles: codersdk.ConvertRoles(rbac.OrganizationRoles(admin.OrganizationID)),
},
}

Expand Down
16 changes: 6 additions & 10 deletions codersdk/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,13 @@ func (c *Client) ListOrganizationRoles(ctx context.Context, org uuid.UUID) ([]Ro
return roles, json.NewDecoder(res.Body).Decode(&roles)
}

func RolesFromName(roleNames []string) []Role {
roles := make([]Role, 0, len(roleNames))
for _, roleName := range roleNames {
role, err := rbac.RoleByName(roleName)
if err != nil {
continue
}
roles = append(roles, Role{
Name: role.Name,
func ConvertRoles(roles []rbac.Role) []Role {
converted := make([]Role, 0, len(roles))
for _, role := range roles {
converted = append(converted, Role{
DisplayName: role.DisplayName,
Name: role.Name,
})
}
return roles
return converted
}