mirror of
https://github.com/coder/coder.git
synced 2025-07-12 00:14:10 +00:00
refactor: Return the display_name and name in the roles endpoint (#1328)
This commit is contained in:
@ -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,17 @@ 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 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 converted
|
||||
}
|
||||
|
Reference in New Issue
Block a user