]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as express from 'express' |
490b595a | 2 | import * as RateLimit from 'express-rate-limit' |
d03cd8bb C |
3 | import { UserCreate, UserRight, UserRole, UserUpdate } from '../../../../shared' |
4 | import { logger } from '../../../helpers/logger' | |
5 | import { getFormattedObjects } from '../../../helpers/utils' | |
6 | import { CONFIG, RATES_LIMIT, sequelizeTypescript } from '../../../initializers' | |
7 | import { Emailer } from '../../../lib/emailer' | |
8 | import { Redis } from '../../../lib/redis' | |
9 | import { createUserAccountAndChannel } from '../../../lib/user' | |
65fcc311 | 10 | import { |
f076daa7 | 11 | asyncMiddleware, |
90d4bb81 | 12 | asyncRetryTransactionMiddleware, |
f076daa7 C |
13 | authenticate, |
14 | ensureUserHasRight, | |
15 | ensureUserRegistrationAllowed, | |
ff2c1fe8 | 16 | ensureUserRegistrationAllowedForIP, |
f076daa7 C |
17 | paginationValidator, |
18 | setDefaultPagination, | |
19 | setDefaultSort, | |
20 | token, | |
21 | usersAddValidator, | |
22 | usersGetValidator, | |
23 | usersRegisterValidator, | |
24 | usersRemoveValidator, | |
25 | usersSortValidator, | |
d03cd8bb C |
26 | usersUpdateValidator |
27 | } from '../../../middlewares' | |
28 | import { usersAskResetPasswordValidator, usersBlockingValidator, usersResetPasswordValidator } from '../../../middlewares/validators' | |
29 | import { UserModel } from '../../../models/account/user' | |
30 | import { OAuthTokenModel } from '../../../models/oauth/oauth-token' | |
31 | import { auditLoggerFactory, UserAuditView } from '../../../helpers/audit-logger' | |
d03cd8bb | 32 | import { meRouter } from './me' |
80e36cd9 AB |
33 | |
34 | const auditLogger = auditLoggerFactory('users') | |
65fcc311 | 35 | |
490b595a C |
36 | const loginRateLimiter = new RateLimit({ |
37 | windowMs: RATES_LIMIT.LOGIN.WINDOW_MS, | |
38 | max: RATES_LIMIT.LOGIN.MAX, | |
39 | delayMs: 0 | |
40 | }) | |
c5911fd3 | 41 | |
65fcc311 | 42 | const usersRouter = express.Router() |
06a05d5f | 43 | usersRouter.use('/', meRouter) |
9bd26629 | 44 | |
65fcc311 | 45 | usersRouter.get('/', |
86d13ec2 C |
46 | authenticate, |
47 | ensureUserHasRight(UserRight.MANAGE_USERS), | |
65fcc311 C |
48 | paginationValidator, |
49 | usersSortValidator, | |
1174a847 | 50 | setDefaultSort, |
f05a1c30 | 51 | setDefaultPagination, |
eb080476 | 52 | asyncMiddleware(listUsers) |
5c39adb7 C |
53 | ) |
54 | ||
e6921918 C |
55 | usersRouter.post('/:id/block', |
56 | authenticate, | |
57 | ensureUserHasRight(UserRight.MANAGE_USERS), | |
58 | asyncMiddleware(usersBlockingValidator), | |
59 | asyncMiddleware(blockUser) | |
60 | ) | |
61 | usersRouter.post('/:id/unblock', | |
62 | authenticate, | |
63 | ensureUserHasRight(UserRight.MANAGE_USERS), | |
64 | asyncMiddleware(usersBlockingValidator), | |
65 | asyncMiddleware(unblockUser) | |
66 | ) | |
67 | ||
8094a898 | 68 | usersRouter.get('/:id', |
94ff4c23 C |
69 | authenticate, |
70 | ensureUserHasRight(UserRight.MANAGE_USERS), | |
a2431b7d | 71 | asyncMiddleware(usersGetValidator), |
8094a898 C |
72 | getUser |
73 | ) | |
74 | ||
65fcc311 C |
75 | usersRouter.post('/', |
76 | authenticate, | |
954605a8 | 77 | ensureUserHasRight(UserRight.MANAGE_USERS), |
a2431b7d | 78 | asyncMiddleware(usersAddValidator), |
90d4bb81 | 79 | asyncRetryTransactionMiddleware(createUser) |
9bd26629 C |
80 | ) |
81 | ||
65fcc311 | 82 | usersRouter.post('/register', |
a2431b7d | 83 | asyncMiddleware(ensureUserRegistrationAllowed), |
ff2c1fe8 | 84 | ensureUserRegistrationAllowedForIP, |
a2431b7d | 85 | asyncMiddleware(usersRegisterValidator), |
90d4bb81 | 86 | asyncRetryTransactionMiddleware(registerUser) |
2c2e9092 C |
87 | ) |
88 | ||
65fcc311 C |
89 | usersRouter.put('/:id', |
90 | authenticate, | |
954605a8 | 91 | ensureUserHasRight(UserRight.MANAGE_USERS), |
a2431b7d | 92 | asyncMiddleware(usersUpdateValidator), |
eb080476 | 93 | asyncMiddleware(updateUser) |
9bd26629 C |
94 | ) |
95 | ||
65fcc311 C |
96 | usersRouter.delete('/:id', |
97 | authenticate, | |
954605a8 | 98 | ensureUserHasRight(UserRight.MANAGE_USERS), |
a2431b7d | 99 | asyncMiddleware(usersRemoveValidator), |
eb080476 | 100 | asyncMiddleware(removeUser) |
9bd26629 | 101 | ) |
6606150c | 102 | |
ecb4e35f C |
103 | usersRouter.post('/ask-reset-password', |
104 | asyncMiddleware(usersAskResetPasswordValidator), | |
105 | asyncMiddleware(askResetUserPassword) | |
106 | ) | |
107 | ||
108 | usersRouter.post('/:id/reset-password', | |
109 | asyncMiddleware(usersResetPasswordValidator), | |
110 | asyncMiddleware(resetUserPassword) | |
111 | ) | |
112 | ||
490b595a C |
113 | usersRouter.post('/token', |
114 | loginRateLimiter, | |
115 | token, | |
116 | success | |
117 | ) | |
9bd26629 | 118 | // TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route |
9457bf88 C |
119 | |
120 | // --------------------------------------------------------------------------- | |
121 | ||
65fcc311 C |
122 | export { |
123 | usersRouter | |
124 | } | |
9457bf88 C |
125 | |
126 | // --------------------------------------------------------------------------- | |
127 | ||
90d4bb81 | 128 | async function createUser (req: express.Request, res: express.Response) { |
4771e000 | 129 | const body: UserCreate = req.body |
f05a1c30 | 130 | const userToCreate = new UserModel({ |
4771e000 C |
131 | username: body.username, |
132 | password: body.password, | |
133 | email: body.email, | |
0883b324 | 134 | nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, |
7efe153b | 135 | autoPlayVideo: true, |
954605a8 | 136 | role: body.role, |
b0f9f39e | 137 | videoQuota: body.videoQuota |
9bd26629 C |
138 | }) |
139 | ||
f05a1c30 | 140 | const { user, account } = await createUserAccountAndChannel(userToCreate) |
eb080476 | 141 | |
80e36cd9 | 142 | auditLogger.create(res.locals.oauth.token.User.Account.Actor.getIdentifier(), new UserAuditView(user.toFormattedJSON())) |
38fa2065 | 143 | logger.info('User %s with its channel and account created.', body.username) |
f05a1c30 | 144 | |
90d4bb81 C |
145 | return res.json({ |
146 | user: { | |
147 | id: user.id, | |
148 | account: { | |
149 | id: account.id, | |
150 | uuid: account.Actor.uuid | |
151 | } | |
152 | } | |
153 | }).end() | |
47e0652b C |
154 | } |
155 | ||
90d4bb81 | 156 | async function registerUser (req: express.Request, res: express.Response) { |
77a5501f C |
157 | const body: UserCreate = req.body |
158 | ||
80e36cd9 | 159 | const userToCreate = new UserModel({ |
77a5501f C |
160 | username: body.username, |
161 | password: body.password, | |
162 | email: body.email, | |
0883b324 | 163 | nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, |
7efe153b | 164 | autoPlayVideo: true, |
954605a8 | 165 | role: UserRole.USER, |
77a5501f C |
166 | videoQuota: CONFIG.USER.VIDEO_QUOTA |
167 | }) | |
168 | ||
80e36cd9 | 169 | const { user } = await createUserAccountAndChannel(userToCreate) |
47e0652b | 170 | |
80e36cd9 | 171 | auditLogger.create(body.username, new UserAuditView(user.toFormattedJSON())) |
47e0652b | 172 | logger.info('User %s with its channel and account registered.', body.username) |
90d4bb81 C |
173 | |
174 | return res.type('json').status(204).end() | |
77a5501f C |
175 | } |
176 | ||
e6921918 C |
177 | async function unblockUser (req: express.Request, res: express.Response, next: express.NextFunction) { |
178 | const user: UserModel = res.locals.user | |
179 | ||
180 | await changeUserBlock(res, user, false) | |
181 | ||
182 | return res.status(204).end() | |
183 | } | |
184 | ||
185 | async function blockUser (req: express.Request, res: express.Response, next: express.NextFunction) { | |
186 | const user: UserModel = res.locals.user | |
eacb25c4 | 187 | const reason = req.body.reason |
e6921918 | 188 | |
eacb25c4 | 189 | await changeUserBlock(res, user, true, reason) |
e6921918 C |
190 | |
191 | return res.status(204).end() | |
192 | } | |
193 | ||
8094a898 | 194 | function getUser (req: express.Request, res: express.Response, next: express.NextFunction) { |
ce5496d6 | 195 | return res.json((res.locals.user as UserModel).toFormattedJSON()) |
8094a898 C |
196 | } |
197 | ||
eb080476 | 198 | async function listUsers (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 199 | const resultList = await UserModel.listForApi(req.query.start, req.query.count, req.query.sort) |
eb080476 C |
200 | |
201 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
9bd26629 C |
202 | } |
203 | ||
eb080476 | 204 | async function removeUser (req: express.Request, res: express.Response, next: express.NextFunction) { |
92b9d60c | 205 | const user: UserModel = res.locals.user |
eb080476 C |
206 | |
207 | await user.destroy() | |
208 | ||
80e36cd9 AB |
209 | auditLogger.delete(res.locals.oauth.token.User.Account.Actor.getIdentifier(), new UserAuditView(user.toFormattedJSON())) |
210 | ||
eb080476 | 211 | return res.sendStatus(204) |
9bd26629 C |
212 | } |
213 | ||
eb080476 | 214 | async function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) { |
8094a898 | 215 | const body: UserUpdate = req.body |
80e36cd9 AB |
216 | const userToUpdate = res.locals.user as UserModel |
217 | const oldUserAuditView = new UserAuditView(userToUpdate.toFormattedJSON()) | |
218 | const roleChanged = body.role !== undefined && body.role !== userToUpdate.role | |
8094a898 | 219 | |
80e36cd9 AB |
220 | if (body.email !== undefined) userToUpdate.email = body.email |
221 | if (body.videoQuota !== undefined) userToUpdate.videoQuota = body.videoQuota | |
222 | if (body.role !== undefined) userToUpdate.role = body.role | |
8094a898 | 223 | |
80e36cd9 | 224 | const user = await userToUpdate.save() |
eb080476 | 225 | |
f8b8c36b C |
226 | // Destroy user token to refresh rights |
227 | if (roleChanged) { | |
80e36cd9 | 228 | await OAuthTokenModel.deleteUserToken(userToUpdate.id) |
f8b8c36b C |
229 | } |
230 | ||
80e36cd9 AB |
231 | auditLogger.update( |
232 | res.locals.oauth.token.User.Account.Actor.getIdentifier(), | |
233 | new UserAuditView(user.toFormattedJSON()), | |
234 | oldUserAuditView | |
235 | ) | |
236 | ||
265ba139 C |
237 | // Don't need to send this update to followers, these attributes are not propagated |
238 | ||
eb080476 | 239 | return res.sendStatus(204) |
8094a898 C |
240 | } |
241 | ||
ecb4e35f C |
242 | async function askResetUserPassword (req: express.Request, res: express.Response, next: express.NextFunction) { |
243 | const user = res.locals.user as UserModel | |
244 | ||
245 | const verificationString = await Redis.Instance.setResetPasswordVerificationString(user.id) | |
246 | const url = CONFIG.WEBSERVER.URL + '/reset-password?userId=' + user.id + '&verificationString=' + verificationString | |
247 | await Emailer.Instance.addForgetPasswordEmailJob(user.email, url) | |
248 | ||
249 | return res.status(204).end() | |
250 | } | |
251 | ||
252 | async function resetUserPassword (req: express.Request, res: express.Response, next: express.NextFunction) { | |
253 | const user = res.locals.user as UserModel | |
254 | user.password = req.body.password | |
255 | ||
256 | await user.save() | |
257 | ||
258 | return res.status(204).end() | |
259 | } | |
260 | ||
69818c93 | 261 | function success (req: express.Request, res: express.Response, next: express.NextFunction) { |
9457bf88 C |
262 | res.end() |
263 | } | |
e6921918 | 264 | |
eacb25c4 | 265 | async function changeUserBlock (res: express.Response, user: UserModel, block: boolean, reason?: string) { |
e6921918 C |
266 | const oldUserAuditView = new UserAuditView(user.toFormattedJSON()) |
267 | ||
268 | user.blocked = block | |
eacb25c4 | 269 | user.blockedReason = reason || null |
e6921918 C |
270 | |
271 | await sequelizeTypescript.transaction(async t => { | |
272 | await OAuthTokenModel.deleteUserToken(user.id, t) | |
273 | ||
274 | await user.save({ transaction: t }) | |
275 | }) | |
276 | ||
eacb25c4 C |
277 | await Emailer.Instance.addUserBlockJob(user, block, reason) |
278 | ||
e6921918 C |
279 | auditLogger.update( |
280 | res.locals.oauth.token.User.Account.Actor.getIdentifier(), | |
281 | new UserAuditView(user.toFormattedJSON()), | |
282 | oldUserAuditView | |
283 | ) | |
284 | } |