Skip to content

Commit 2a180d4

Browse files
committed
Get user permissions
1 parent 64a8b4a commit 2a180d4

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed

coderd/rbac/object.go

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ var (
1717
Type: "template",
1818
}
1919

20+
ResourceUser = Object{
21+
Type: "user",
22+
}
23+
2024
// ResourceUserRole might be expanded later to allow more granular permissions
2125
// to modifying roles. For now, this covers all possible roles, so having this permission
2226
// allows granting/deleting **ALL** roles.

site/src/api/api.ts

+11
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ export const getAuthMethods = async (): Promise<TypesGen.AuthMethods> => {
7676
return response.data
7777
}
7878

79+
export const getUserPermissions = async (
80+
userId: string,
81+
params: TypesGen.UserPermissionCheckRequest,
82+
): Promise<TypesGen.UserPermissionCheckResponse> => {
83+
const response = await axios.post<TypesGen.UserPermissionCheckResponse>(
84+
`/api/v2/users/${userId}/authorization`,
85+
params,
86+
)
87+
return response.data
88+
}
89+
7990
export const getApiKey = async (): Promise<TypesGen.GenerateAPIKeyResponse> => {
8091
const response = await axios.post<TypesGen.GenerateAPIKeyResponse>("/api/v2/users/me/keys")
8192
return response.data

site/src/api/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ export interface ReconnectingPTYRequest {
1010
readonly height?: number
1111
readonly width?: number
1212
}
13+
14+
export type UserPermissionCheckResponse = Record<string, boolean>

site/src/xServices/auth/authXService.ts

+53-1
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,24 @@ export const Language = {
77
successProfileUpdate: "Updated preferences.",
88
}
99

10+
const permissionsToCheck: Record<string, TypesGen.UserPermissionCheck> = {
11+
readAllUsers: {
12+
object: {
13+
resource_type: "user",
14+
},
15+
action: "read",
16+
},
17+
}
18+
1019
export interface AuthContext {
1120
getUserError?: Error | unknown
1221
getMethodsError?: Error | unknown
1322
authError?: Error | unknown
1423
updateProfileError?: Error | unknown
1524
me?: TypesGen.User
1625
methods?: TypesGen.AuthMethods
26+
permissions?: Types.UserPermissionCheckResponse
27+
getPermissionsError?: Error | unknown
1728
}
1829

1930
export type AuthEvent =
@@ -50,6 +61,9 @@ export const authMachine =
5061
updateProfile: {
5162
data: TypesGen.User
5263
}
64+
getPermissions: {
65+
data: Types.UserPermissionCheckResponse
66+
}
5367
},
5468
},
5569
id: "authState",
@@ -88,7 +102,7 @@ export const authMachine =
88102
onDone: [
89103
{
90104
actions: ["assignMe", "clearGetUserError"],
91-
target: "signedIn",
105+
target: "gettingPermissions",
92106
},
93107
],
94108
onError: [
@@ -100,6 +114,26 @@ export const authMachine =
100114
},
101115
tags: "loading",
102116
},
117+
gettingPermissions: {
118+
entry: "clearGetPermissionsError",
119+
invoke: {
120+
src: "getPermissions",
121+
id: "getPermissions",
122+
onDone: [
123+
{
124+
actions: ["assignPermissions"],
125+
target: "signedIn",
126+
},
127+
],
128+
onError: [
129+
{
130+
actions: "assignGetPermissionsError",
131+
target: "signedOut",
132+
},
133+
],
134+
},
135+
tags: "loading",
136+
},
103137
gettingMethods: {
104138
invoke: {
105139
src: "getMethods",
@@ -200,6 +234,15 @@ export const authMachine =
200234

201235
return API.updateProfile(context.me.id, event.data)
202236
},
237+
getPermissions: async (context) => {
238+
if (!context.me) {
239+
throw new Error("No current user found")
240+
}
241+
242+
return API.getUserPermissions(context.me.id, {
243+
checks: permissionsToCheck,
244+
})
245+
},
203246
},
204247
actions: {
205248
assignMe: assign({
@@ -242,6 +285,15 @@ export const authMachine =
242285
clearUpdateProfileError: assign({
243286
updateProfileError: (_) => undefined,
244287
}),
288+
assignPermissions: assign({
289+
permissions: (_, event) => event.data,
290+
}),
291+
assignGetPermissionsError: assign({
292+
getPermissionsError: (_, event) => event.data,
293+
}),
294+
clearGetPermissionsError: assign({
295+
getPermissionsError: (_) => undefined,
296+
}),
245297
},
246298
},
247299
)

0 commit comments

Comments
 (0)