-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusersController.ts
107 lines (85 loc) · 3.11 KB
/
usersController.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import shortUuid from 'short-uuid';
import { AuthressClient } from '@authress/sdk';
import express, { NextFunction, Request, Response } from 'express';
import authressPermissionsWrapper from '../authressPermissionsWrapper';
import accountsRepository from '../accounts/accountsRepository';
const usersController = express.Router();
export default usersController;
// set a user role
usersController.post('/users', async (request: Request, response: Response, next: NextFunction) => {
const callerUserId = response.locals.userId;
const accountId = request.params.accountId;
const userId = request.body.userId;
const newRoles = request.body.roles;
if (!accountId) {
response.status(404);
return;
}
const hasAccess = await authressPermissionsWrapper.hasAccessToResource(callerUserId, `accounts/${accountId}/users/${userId}`, 'users:update');
if (!hasAccess) {
response.status(403).json({});
return;
}
const account = await accountsRepository.getAccount(accountId);
if (!account) {
response.status(404).json({});
return;
}
await authressPermissionsWrapper.setRoleForUser(accountId, userId, `accounts/${accountId}`, newRoles);
response.status(202).json({});
});
// Get users for the account
usersController.get('/users', async (request: Request, response: Response, next: NextFunction) => {
const callerUserId = response.locals.userId;
const accountId = request.params.accountId;
const userId = request.body.userId;
const newRoles = request.body.roles;
if (!accountId) {
response.status(404);
return;
}
const hasAccess = await authressPermissionsWrapper.hasAccessToResource(callerUserId, `accounts/${accountId}/users`, 'users:read');
if (!hasAccess) {
response.status(403).json({});
return;
}
const account = await accountsRepository.getAccount(accountId);
if (!account) {
response.status(404).json({});
return;
}
const usersWithRoles = await authressPermissionsWrapper.getUsersThatHaveAccessToAccount(accountId);
const userDataMap = await authressPermissionsWrapper.getUserDataMap(usersWithRoles.map(u => u.userId));
response.status(200).json({
users: usersWithRoles.map(u => ({
userId: u.userId,
roles: u.roles,
data: {
picture: userDataMap[u.userId]?.picture,
name: userDataMap[u.userId]?.name
}
}))
});
});
// remove a user from the account
usersController.delete('/users/:userId', async (request: Request, response: Response, next: NextFunction) => {
const callerUserId = response.locals.userId;
const accountId = request.params.accountId;
const userId = request.params.userId;
if (!accountId) {
response.status(404).json({});
return;
}
const hasAccess = await authressPermissionsWrapper.hasAccessToResource(callerUserId, `accounts/${accountId}/users`, 'users:update');
if (!hasAccess) {
response.status(403).json({});
return;
}
const account = await accountsRepository.getAccount(accountId);
if (!account) {
response.status(404).json({});
return;
}
await authressPermissionsWrapper.removeUserFromAccount(accountId, userId);
response.status(204);
});