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
Next Next commit
Return DisplayName and Name in the roles endpoint
  • Loading branch information
BrunoQuaresma committed May 6, 2022
commit fb61fd790bebccfb65545eb993cd21509acee096
15 changes: 10 additions & 5 deletions coderd/rbac/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ var (
// admin grants all actions to all resources.
admin: func(_ string) Role {
return Role{
Name: admin,
Name: admin,
DisplayName: "Admin",
Site: permissions(map[Object][]Action{
ResourceWildcard: {WildcardSymbol},
}),
Expand All @@ -61,7 +62,8 @@ var (
// member grants all actions to all resources owned by the user
member: func(_ string) Role {
return Role{
Name: member,
Name: member,
DisplayName: "Member",
User: permissions(map[Object][]Action{
ResourceWildcard: {WildcardSymbol},
}),
Expand All @@ -73,7 +75,8 @@ var (
// TODO: Finish the auditor as we add resources.
auditor: func(_ string) Role {
return Role{
Name: "auditor",
Name: "auditor",
DisplayName: "Auditor",
Site: permissions(map[Object][]Action{
// Should be able to read all template details, even in orgs they
// are not in.
Expand All @@ -86,7 +89,8 @@ var (
// organization scope.
orgAdmin: func(organizationID string) Role {
return Role{
Name: roleName(orgAdmin, organizationID),
Name: roleName(orgAdmin, organizationID),
DisplayName: "Organization Admin",
Org: map[string][]Permission{
organizationID: {
{
Expand All @@ -104,7 +108,8 @@ var (
// in an organization.
orgMember: func(organizationID string) Role {
return Role{
Name: roleName(orgMember, organizationID),
Name: roleName(orgMember, organizationID),
DisplayName: "Organization Member",
Org: map[string][]Permission{
organizationID: {},
},
Expand Down
5 changes: 3 additions & 2 deletions coderd/rbac/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ type Permission struct {
// Users of this package should instead **only** use the role names, and
// this package will expand the role names into their json payloads.
type Role struct {
Name string `json:"name"`
Site []Permission `json:"site"`
Name string `json:"name"`
DisplayName string `json:"display_name"`
Site []Permission `json:"site"`
// Org is a map of orgid to permissions. We represent orgid as a string.
// We scope the organizations in the role so we can easily combine all the
// roles.
Expand Down
7 changes: 5 additions & 2 deletions coderd/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"

"github.com/coder/coder/coderd/httpmw"
"github.com/coder/coder/codersdk"

"github.com/coder/coder/coderd/httpapi"
"github.com/coder/coder/coderd/rbac"
Expand All @@ -13,7 +14,8 @@ 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.
roles := rbac.SiteRoles()
roleNames := rbac.SiteRoles()
roles := codersdk.RolesFromName(roleNames)
httpapi.Write(rw, http.StatusOK, roles)
}

Expand All @@ -22,6 +24,7 @@ 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)
roles := rbac.OrganizationRoles(organization.ID)
roleNames := rbac.OrganizationRoles(organization.ID)
roles := codersdk.RolesFromName(roleNames)
httpapi.Write(rw, http.StatusOK, roles)
}
26 changes: 13 additions & 13 deletions coderd/roles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,68 +45,68 @@ func TestListRoles(t *testing.T) {
testCases := []struct {
Name string
Client *codersdk.Client
APICall func() ([]string, error)
ExpectedRoles []string
APICall func() ([]codersdk.Role, error)
ExpectedRoles []codersdk.Role
AuthorizedError string
}{
{
Name: "MemberListSite",
APICall: func() ([]string, error) {
APICall: func() ([]codersdk.Role, error) {
x, err := member.ListSiteRoles(ctx)
return x, err
},
AuthorizedError: unauth,
},
{
Name: "OrgMemberListOrg",
APICall: func() ([]string, error) {
APICall: func() ([]codersdk.Role, error) {
return member.ListOrganizationRoles(ctx, admin.OrganizationID)
},
AuthorizedError: unauth,
},
{
Name: "NonOrgMemberListOrg",
APICall: func() ([]string, error) {
APICall: func() ([]codersdk.Role, error) {
return member.ListOrganizationRoles(ctx, otherOrg.ID)
},
AuthorizedError: notMember,
},
// Org admin
{
Name: "OrgAdminListSite",
APICall: func() ([]string, error) {
APICall: func() ([]codersdk.Role, error) {
return orgAdmin.ListSiteRoles(ctx)
},
AuthorizedError: unauth,
},
{
Name: "OrgAdminListOrg",
APICall: func() ([]string, error) {
APICall: func() ([]codersdk.Role, error) {
return orgAdmin.ListOrganizationRoles(ctx, admin.OrganizationID)
},
ExpectedRoles: rbac.OrganizationRoles(admin.OrganizationID),
ExpectedRoles: codersdk.RolesFromName(rbac.OrganizationRoles(admin.OrganizationID)),
},
{
Name: "OrgAdminListOtherOrg",
APICall: func() ([]string, error) {
APICall: func() ([]codersdk.Role, error) {
return orgAdmin.ListOrganizationRoles(ctx, otherOrg.ID)
},
AuthorizedError: notMember,
},
// Admin
{
Name: "AdminListSite",
APICall: func() ([]string, error) {
APICall: func() ([]codersdk.Role, error) {
return client.ListSiteRoles(ctx)
},
ExpectedRoles: rbac.SiteRoles(),
ExpectedRoles: codersdk.RolesFromName(rbac.SiteRoles()),
},
{
Name: "AdminListOrg",
APICall: func() ([]string, error) {
APICall: func() ([]codersdk.Role, error) {
return client.ListOrganizationRoles(ctx, admin.OrganizationID)
},
ExpectedRoles: rbac.OrganizationRoles(admin.OrganizationID),
ExpectedRoles: codersdk.RolesFromName(rbac.OrganizationRoles(admin.OrganizationID)),
},
}

Expand Down
8 changes: 4 additions & 4 deletions coderd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,10 @@ func (api *api) putUserSuspend(rw http.ResponseWriter, r *http.Request) {
}

func (api *api) putUserPassword(rw http.ResponseWriter, r *http.Request) {
var (
user = httpmw.UserParam(r)
params codersdk.UpdateUserPasswordRequest
)
var (
user = httpmw.UserParam(r)
params codersdk.UpdateUserPasswordRequest
)
if !httpapi.Read(rw, r, &params) {
return
}
Expand Down
29 changes: 25 additions & 4 deletions codersdk/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ import (
"fmt"
"net/http"

"github.com/coder/coder/coderd/rbac"
"github.com/google/uuid"
)

type Role struct {
Name string `json:"name"`
DisplayName string `json:"display_name"`
}

// ListSiteRoles lists all available site wide roles.
// This is not user specific.
func (c *Client) ListSiteRoles(ctx context.Context) ([]string, error) {
func (c *Client) ListSiteRoles(ctx context.Context) ([]Role, error) {
res, err := c.request(ctx, http.MethodGet, "/api/v2/users/roles", nil)
if err != nil {
return nil, err
Expand All @@ -20,13 +26,13 @@ func (c *Client) ListSiteRoles(ctx context.Context) ([]string, error) {
if res.StatusCode != http.StatusOK {
return nil, readBodyAsError(res)
}
var roles []string
var roles []Role
return roles, json.NewDecoder(res.Body).Decode(&roles)
}

// ListOrganizationRoles lists all available roles for a given organization.
// This is not user specific.
func (c *Client) ListOrganizationRoles(ctx context.Context, org uuid.UUID) ([]string, error) {
func (c *Client) ListOrganizationRoles(ctx context.Context, org uuid.UUID) ([]Role, error) {
res, err := c.request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/organizations/%s/members/roles/", org.String()), nil)
if err != nil {
return nil, err
Expand All @@ -35,6 +41,21 @@ func (c *Client) ListOrganizationRoles(ctx context.Context, org uuid.UUID) ([]st
if res.StatusCode != http.StatusOK {
return nil, readBodyAsError(res)
}
var roles []string
var roles []Role
return roles, json.NewDecoder(res.Body).Decode(&roles)
}

func RolesFromName(roleNames []string) []Role {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created this function to use in the endpoints to not break any internal implementation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of having this function that may convert a role name to a role, you should just have a function in the rbac package that returns all site role objects.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. How would you call that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I'm trying to say is we should avoid having to first get the "role names" and then convert those to "roles" in a potentially lossy fashion. We should just have a function that returns the roles instead of a function that returns the role names in the rbac package.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. I made the update, please let me know what you think about the changes. Merging it for now.

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,
DisplayName: role.DisplayName,
})
}
return roles
}