-
Notifications
You must be signed in to change notification settings - Fork 894
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting. How would you call that? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
There was a problem hiding this comment.
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.