Skip to content

Commit b27a5ed

Browse files
committed
refactor: Add roles into the user response
1 parent ad8d9dd commit b27a5ed

File tree

4 files changed

+37
-17
lines changed

4 files changed

+37
-17
lines changed

coderd/users.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,14 +807,22 @@ func (api *api) createUser(ctx context.Context, req codersdk.CreateUserRequest)
807807
}
808808

809809
func convertUser(user database.User, organizationIDs []uuid.UUID) codersdk.User {
810-
return codersdk.User{
810+
convertedUser := codersdk.User{
811811
ID: user.ID,
812812
Email: user.Email,
813813
CreatedAt: user.CreatedAt,
814814
Username: user.Username,
815815
Status: codersdk.UserStatus(user.Status),
816816
OrganizationIDs: organizationIDs,
817+
Roles: make([]codersdk.Role, 0),
817818
}
819+
820+
for _, roleName := range user.RBACRoles {
821+
rbacRole, _ := rbac.RoleByName(roleName)
822+
convertedUser.Roles = append(convertedUser.Roles, codersdk.ConvertRole(rbacRole))
823+
}
824+
825+
return convertedUser
818826
}
819827

820828
func convertUsers(users []database.User, organizationIDsByUserID map[uuid.UUID][]uuid.UUID) []codersdk.User {

codersdk/roles.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,17 @@ func (c *Client) ListOrganizationRoles(ctx context.Context, org uuid.UUID) ([]Ro
4545
return roles, json.NewDecoder(res.Body).Decode(&roles)
4646
}
4747

48+
func ConvertRole(role rbac.Role) Role {
49+
return Role{
50+
DisplayName: role.DisplayName,
51+
Name: role.Name,
52+
}
53+
}
54+
4855
func ConvertRoles(roles []rbac.Role) []Role {
4956
converted := make([]Role, 0, len(roles))
5057
for _, role := range roles {
51-
converted = append(converted, Role{
52-
DisplayName: role.DisplayName,
53-
Name: role.Name,
54-
})
58+
converted = append(converted, ConvertRole(role))
5559
}
5660
return converted
5761
}

codersdk/users.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type User struct {
4545
Username string `json:"username" validate:"required"`
4646
Status UserStatus `json:"status"`
4747
OrganizationIDs []uuid.UUID `json:"organization_ids"`
48+
Roles []Role `json:"roles"`
4849
}
4950

5051
type CreateFirstUserRequest struct {

site/src/api/typesGenerated.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface AgentGitSSHKey {
1212
readonly private_key: string
1313
}
1414

15-
// From codersdk/users.go:109:6
15+
// From codersdk/users.go:110:6
1616
export interface AuthMethods {
1717
readonly password: boolean
1818
readonly github: boolean
@@ -30,21 +30,21 @@ export interface BuildInfoResponse {
3030
readonly version: string
3131
}
3232

33-
// From codersdk/users.go:50:6
33+
// From codersdk/users.go:51:6
3434
export interface CreateFirstUserRequest {
3535
readonly email: string
3636
readonly username: string
3737
readonly password: string
3838
readonly organization: string
3939
}
4040

41-
// From codersdk/users.go:58:6
41+
// From codersdk/users.go:59:6
4242
export interface CreateFirstUserResponse {
4343
readonly user_id: string
4444
readonly organization_id: string
4545
}
4646

47-
// From codersdk/users.go:104:6
47+
// From codersdk/users.go:105:6
4848
export interface CreateOrganizationRequest {
4949
readonly name: string
5050
}
@@ -77,7 +77,7 @@ export interface CreateTemplateVersionRequest {
7777
readonly parameter_values: CreateParameterRequest[]
7878
}
7979

80-
// From codersdk/users.go:63:6
80+
// From codersdk/users.go:64:6
8181
export interface CreateUserRequest {
8282
readonly email: string
8383
readonly username: string
@@ -101,7 +101,7 @@ export interface CreateWorkspaceRequest {
101101
readonly parameter_values: CreateParameterRequest[]
102102
}
103103

104-
// From codersdk/users.go:100:6
104+
// From codersdk/users.go:101:6
105105
export interface GenerateAPIKeyResponse {
106106
readonly key: string
107107
}
@@ -119,13 +119,13 @@ export interface GoogleInstanceIdentityToken {
119119
readonly json_web_token: string
120120
}
121121

122-
// From codersdk/users.go:89:6
122+
// From codersdk/users.go:90:6
123123
export interface LoginWithPasswordRequest {
124124
readonly email: string
125125
readonly password: string
126126
}
127127

128-
// From codersdk/users.go:95:6
128+
// From codersdk/users.go:96:6
129129
export interface LoginWithPasswordResponse {
130130
readonly session_token: string
131131
}
@@ -195,6 +195,12 @@ export interface ProvisionerJobLog {
195195
readonly output: string
196196
}
197197

198+
// From codersdk/roles.go:13:6
199+
export interface Role {
200+
readonly name: string
201+
readonly display_name: string
202+
}
203+
198204
// From codersdk/templates.go:17:6
199205
export interface Template {
200206
readonly id: string
@@ -255,17 +261,17 @@ export interface UpdateActiveTemplateVersion {
255261
readonly id: string
256262
}
257263

258-
// From codersdk/users.go:79:6
264+
// From codersdk/users.go:80:6
259265
export interface UpdateRoles {
260266
readonly roles: string[]
261267
}
262268

263-
// From codersdk/users.go:75:6
269+
// From codersdk/users.go:76:6
264270
export interface UpdateUserPasswordRequest {
265271
readonly password: string
266272
}
267273

268-
// From codersdk/users.go:70:6
274+
// From codersdk/users.go:71:6
269275
export interface UpdateUserProfileRequest {
270276
readonly email: string
271277
readonly username: string
@@ -294,9 +300,10 @@ export interface User {
294300
readonly username: string
295301
readonly status: UserStatus
296302
readonly organization_ids: string[]
303+
readonly roles: Role[]
297304
}
298305

299-
// From codersdk/users.go:83:6
306+
// From codersdk/users.go:84:6
300307
export interface UserRoles {
301308
readonly roles: string[]
302309
readonly organization_roles: Record<string, string[]>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy