]>
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, | |
74d63469 | 21 | userAutocompleteValidator, |
f076daa7 C |
22 | usersAddValidator, |
23 | usersGetValidator, | |
24 | usersRegisterValidator, | |
25 | usersRemoveValidator, | |
26 | usersSortValidator, | |
d03cd8bb C |
27 | usersUpdateValidator |
28 | } from '../../../middlewares' | |
d9eaee39 | 29 | import { |
993cef4b C |
30 | usersAskResetPasswordValidator, |
31 | usersAskSendVerifyEmailValidator, | |
32 | usersBlockingValidator, | |
33 | usersResetPasswordValidator, | |
34 | usersVerifyEmailValidator | |
d9eaee39 | 35 | } from '../../../middlewares/validators' |
d03cd8bb C |
36 | import { UserModel } from '../../../models/account/user' |
37 | import { OAuthTokenModel } from '../../../models/oauth/oauth-token' | |
993cef4b | 38 | import { auditLoggerFactory, getAuditIdFromRes, UserAuditView } from '../../../helpers/audit-logger' |
d03cd8bb | 39 | import { meRouter } from './me' |
80e36cd9 AB |
40 | |
41 | const auditLogger = auditLoggerFactory('users') | |
65fcc311 | 42 | |
490b595a C |
43 | const loginRateLimiter = new RateLimit({ |
44 | windowMs: RATES_LIMIT.LOGIN.WINDOW_MS, | |
45 | max: RATES_LIMIT.LOGIN.MAX, | |
46 | delayMs: 0 | |
47 | }) | |
c5911fd3 | 48 | |
288fe385 C |
49 | const askSendEmailLimiter = new RateLimit({ |
50 | windowMs: RATES_LIMIT.ASK_SEND_EMAIL.WINDOW_MS, | |
51 | max: RATES_LIMIT.ASK_SEND_EMAIL.MAX, | |
52 | delayMs: 0 | |
53 | }) | |
54 | ||
65fcc311 | 55 | const usersRouter = express.Router() |
06a05d5f | 56 | usersRouter.use('/', meRouter) |
9bd26629 | 57 | |
74d63469 GR |
58 | usersRouter.get('/autocomplete', |
59 | userAutocompleteValidator, | |
60 | asyncMiddleware(autocompleteUsers) | |
61 | ) | |
62 | ||
65fcc311 | 63 | usersRouter.get('/', |
86d13ec2 C |
64 | authenticate, |
65 | ensureUserHasRight(UserRight.MANAGE_USERS), | |
65fcc311 C |
66 | paginationValidator, |
67 | usersSortValidator, | |
1174a847 | 68 | setDefaultSort, |
f05a1c30 | 69 | setDefaultPagination, |
eb080476 | 70 | asyncMiddleware(listUsers) |
5c39adb7 C |
71 | ) |
72 | ||
e6921918 C |
73 | usersRouter.post('/:id/block', |
74 | authenticate, | |
75 | ensureUserHasRight(UserRight.MANAGE_USERS), | |
76 | asyncMiddleware(usersBlockingValidator), | |
77 | asyncMiddleware(blockUser) | |
78 | ) | |
79 | usersRouter.post('/:id/unblock', | |
80 | authenticate, | |
81 | ensureUserHasRight(UserRight.MANAGE_USERS), | |
82 | asyncMiddleware(usersBlockingValidator), | |
83 | asyncMiddleware(unblockUser) | |
84 | ) | |
85 | ||
8094a898 | 86 | usersRouter.get('/:id', |
94ff4c23 C |
87 | authenticate, |
88 | ensureUserHasRight(UserRight.MANAGE_USERS), | |
a2431b7d | 89 | asyncMiddleware(usersGetValidator), |
8094a898 C |
90 | getUser |
91 | ) | |
92 | ||
65fcc311 C |
93 | usersRouter.post('/', |
94 | authenticate, | |
954605a8 | 95 | ensureUserHasRight(UserRight.MANAGE_USERS), |
a2431b7d | 96 | asyncMiddleware(usersAddValidator), |
90d4bb81 | 97 | asyncRetryTransactionMiddleware(createUser) |
9bd26629 C |
98 | ) |
99 | ||
65fcc311 | 100 | usersRouter.post('/register', |
a2431b7d | 101 | asyncMiddleware(ensureUserRegistrationAllowed), |
ff2c1fe8 | 102 | ensureUserRegistrationAllowedForIP, |
a2431b7d | 103 | asyncMiddleware(usersRegisterValidator), |
90d4bb81 | 104 | asyncRetryTransactionMiddleware(registerUser) |
2c2e9092 C |
105 | ) |
106 | ||
65fcc311 C |
107 | usersRouter.put('/:id', |
108 | authenticate, | |
954605a8 | 109 | ensureUserHasRight(UserRight.MANAGE_USERS), |
a2431b7d | 110 | asyncMiddleware(usersUpdateValidator), |
eb080476 | 111 | asyncMiddleware(updateUser) |
9bd26629 C |
112 | ) |
113 | ||
65fcc311 C |
114 | usersRouter.delete('/:id', |
115 | authenticate, | |
954605a8 | 116 | ensureUserHasRight(UserRight.MANAGE_USERS), |
a2431b7d | 117 | asyncMiddleware(usersRemoveValidator), |
eb080476 | 118 | asyncMiddleware(removeUser) |
9bd26629 | 119 | ) |
6606150c | 120 | |
ecb4e35f C |
121 | usersRouter.post('/ask-reset-password', |
122 | asyncMiddleware(usersAskResetPasswordValidator), | |
123 | asyncMiddleware(askResetUserPassword) | |
124 | ) | |
125 | ||
126 | usersRouter.post('/:id/reset-password', | |
127 | asyncMiddleware(usersResetPasswordValidator), | |
128 | asyncMiddleware(resetUserPassword) | |
129 | ) | |
130 | ||
d9eaee39 | 131 | usersRouter.post('/ask-send-verify-email', |
288fe385 | 132 | askSendEmailLimiter, |
d9eaee39 JM |
133 | asyncMiddleware(usersAskSendVerifyEmailValidator), |
134 | asyncMiddleware(askSendVerifyUserEmail) | |
135 | ) | |
136 | ||
137 | usersRouter.post('/:id/verify-email', | |
138 | asyncMiddleware(usersVerifyEmailValidator), | |
139 | asyncMiddleware(verifyUserEmail) | |
140 | ) | |
141 | ||
490b595a C |
142 | usersRouter.post('/token', |
143 | loginRateLimiter, | |
144 | token, | |
145 | success | |
146 | ) | |
9bd26629 | 147 | // TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route |
9457bf88 C |
148 | |
149 | // --------------------------------------------------------------------------- | |
150 | ||
65fcc311 C |
151 | export { |
152 | usersRouter | |
153 | } | |
9457bf88 C |
154 | |
155 | // --------------------------------------------------------------------------- | |
156 | ||
90d4bb81 | 157 | async function createUser (req: express.Request, res: express.Response) { |
4771e000 | 158 | const body: UserCreate = req.body |
f05a1c30 | 159 | const userToCreate = new UserModel({ |
4771e000 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: body.role, |
bee0abff FA |
166 | videoQuota: body.videoQuota, |
167 | videoQuotaDaily: body.videoQuotaDaily | |
9bd26629 C |
168 | }) |
169 | ||
f05a1c30 | 170 | const { user, account } = await createUserAccountAndChannel(userToCreate) |
eb080476 | 171 | |
993cef4b | 172 | auditLogger.create(getAuditIdFromRes(res), new UserAuditView(user.toFormattedJSON())) |
38fa2065 | 173 | logger.info('User %s with its channel and account created.', body.username) |
f05a1c30 | 174 | |
90d4bb81 C |
175 | return res.json({ |
176 | user: { | |
177 | id: user.id, | |
178 | account: { | |
179 | id: account.id, | |
180 | uuid: account.Actor.uuid | |
181 | } | |
182 | } | |
183 | }).end() | |
47e0652b C |
184 | } |
185 | ||
90d4bb81 | 186 | async function registerUser (req: express.Request, res: express.Response) { |
77a5501f C |
187 | const body: UserCreate = req.body |
188 | ||
80e36cd9 | 189 | const userToCreate = new UserModel({ |
77a5501f C |
190 | username: body.username, |
191 | password: body.password, | |
192 | email: body.email, | |
0883b324 | 193 | nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, |
7efe153b | 194 | autoPlayVideo: true, |
954605a8 | 195 | role: UserRole.USER, |
bee0abff | 196 | videoQuota: CONFIG.USER.VIDEO_QUOTA, |
d9eaee39 JM |
197 | videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY, |
198 | emailVerified: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION ? false : null | |
77a5501f C |
199 | }) |
200 | ||
80e36cd9 | 201 | const { user } = await createUserAccountAndChannel(userToCreate) |
47e0652b | 202 | |
80e36cd9 | 203 | auditLogger.create(body.username, new UserAuditView(user.toFormattedJSON())) |
47e0652b | 204 | logger.info('User %s with its channel and account registered.', body.username) |
90d4bb81 | 205 | |
d9eaee39 JM |
206 | if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION) { |
207 | await sendVerifyUserEmail(user) | |
208 | } | |
209 | ||
90d4bb81 | 210 | return res.type('json').status(204).end() |
77a5501f C |
211 | } |
212 | ||
e6921918 C |
213 | async function unblockUser (req: express.Request, res: express.Response, next: express.NextFunction) { |
214 | const user: UserModel = res.locals.user | |
215 | ||
216 | await changeUserBlock(res, user, false) | |
217 | ||
218 | return res.status(204).end() | |
219 | } | |
220 | ||
221 | async function blockUser (req: express.Request, res: express.Response, next: express.NextFunction) { | |
222 | const user: UserModel = res.locals.user | |
eacb25c4 | 223 | const reason = req.body.reason |
e6921918 | 224 | |
eacb25c4 | 225 | await changeUserBlock(res, user, true, reason) |
e6921918 C |
226 | |
227 | return res.status(204).end() | |
228 | } | |
229 | ||
8094a898 | 230 | function getUser (req: express.Request, res: express.Response, next: express.NextFunction) { |
ce5496d6 | 231 | return res.json((res.locals.user as UserModel).toFormattedJSON()) |
8094a898 C |
232 | } |
233 | ||
74d63469 | 234 | async function autocompleteUsers (req: express.Request, res: express.Response, next: express.NextFunction) { |
5cf84858 | 235 | const resultList = await UserModel.autoComplete(req.query.search as string) |
74d63469 GR |
236 | |
237 | return res.json(resultList) | |
238 | } | |
239 | ||
eb080476 | 240 | async function listUsers (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 241 | const resultList = await UserModel.listForApi(req.query.start, req.query.count, req.query.sort) |
eb080476 C |
242 | |
243 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
9bd26629 C |
244 | } |
245 | ||
eb080476 | 246 | async function removeUser (req: express.Request, res: express.Response, next: express.NextFunction) { |
92b9d60c | 247 | const user: UserModel = res.locals.user |
eb080476 C |
248 | |
249 | await user.destroy() | |
250 | ||
993cef4b | 251 | auditLogger.delete(getAuditIdFromRes(res), new UserAuditView(user.toFormattedJSON())) |
80e36cd9 | 252 | |
eb080476 | 253 | return res.sendStatus(204) |
9bd26629 C |
254 | } |
255 | ||
eb080476 | 256 | async function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) { |
8094a898 | 257 | const body: UserUpdate = req.body |
80e36cd9 AB |
258 | const userToUpdate = res.locals.user as UserModel |
259 | const oldUserAuditView = new UserAuditView(userToUpdate.toFormattedJSON()) | |
260 | const roleChanged = body.role !== undefined && body.role !== userToUpdate.role | |
8094a898 | 261 | |
80e36cd9 AB |
262 | if (body.email !== undefined) userToUpdate.email = body.email |
263 | if (body.videoQuota !== undefined) userToUpdate.videoQuota = body.videoQuota | |
bee0abff | 264 | if (body.videoQuotaDaily !== undefined) userToUpdate.videoQuotaDaily = body.videoQuotaDaily |
80e36cd9 | 265 | if (body.role !== undefined) userToUpdate.role = body.role |
8094a898 | 266 | |
80e36cd9 | 267 | const user = await userToUpdate.save() |
eb080476 | 268 | |
f8b8c36b C |
269 | // Destroy user token to refresh rights |
270 | if (roleChanged) { | |
80e36cd9 | 271 | await OAuthTokenModel.deleteUserToken(userToUpdate.id) |
f8b8c36b C |
272 | } |
273 | ||
80e36cd9 | 274 | auditLogger.update( |
993cef4b | 275 | getAuditIdFromRes(res), |
80e36cd9 AB |
276 | new UserAuditView(user.toFormattedJSON()), |
277 | oldUserAuditView | |
278 | ) | |
279 | ||
265ba139 C |
280 | // Don't need to send this update to followers, these attributes are not propagated |
281 | ||
eb080476 | 282 | return res.sendStatus(204) |
8094a898 C |
283 | } |
284 | ||
ecb4e35f C |
285 | async function askResetUserPassword (req: express.Request, res: express.Response, next: express.NextFunction) { |
286 | const user = res.locals.user as UserModel | |
287 | ||
288 | const verificationString = await Redis.Instance.setResetPasswordVerificationString(user.id) | |
289 | const url = CONFIG.WEBSERVER.URL + '/reset-password?userId=' + user.id + '&verificationString=' + verificationString | |
290 | await Emailer.Instance.addForgetPasswordEmailJob(user.email, url) | |
291 | ||
292 | return res.status(204).end() | |
293 | } | |
294 | ||
295 | async function resetUserPassword (req: express.Request, res: express.Response, next: express.NextFunction) { | |
296 | const user = res.locals.user as UserModel | |
297 | user.password = req.body.password | |
298 | ||
299 | await user.save() | |
300 | ||
301 | return res.status(204).end() | |
302 | } | |
303 | ||
d9eaee39 JM |
304 | async function sendVerifyUserEmail (user: UserModel) { |
305 | const verificationString = await Redis.Instance.setVerifyEmailVerificationString(user.id) | |
306 | const url = CONFIG.WEBSERVER.URL + '/verify-account/email?userId=' + user.id + '&verificationString=' + verificationString | |
307 | await Emailer.Instance.addVerifyEmailJob(user.email, url) | |
308 | return | |
309 | } | |
310 | ||
311 | async function askSendVerifyUserEmail (req: express.Request, res: express.Response, next: express.NextFunction) { | |
312 | const user = res.locals.user as UserModel | |
313 | ||
314 | await sendVerifyUserEmail(user) | |
315 | ||
316 | return res.status(204).end() | |
317 | } | |
318 | ||
319 | async function verifyUserEmail (req: express.Request, res: express.Response, next: express.NextFunction) { | |
320 | const user = res.locals.user as UserModel | |
321 | user.emailVerified = true | |
322 | ||
323 | await user.save() | |
324 | ||
325 | return res.status(204).end() | |
326 | } | |
327 | ||
69818c93 | 328 | function success (req: express.Request, res: express.Response, next: express.NextFunction) { |
9457bf88 C |
329 | res.end() |
330 | } | |
e6921918 | 331 | |
eacb25c4 | 332 | async function changeUserBlock (res: express.Response, user: UserModel, block: boolean, reason?: string) { |
e6921918 C |
333 | const oldUserAuditView = new UserAuditView(user.toFormattedJSON()) |
334 | ||
335 | user.blocked = block | |
eacb25c4 | 336 | user.blockedReason = reason || null |
e6921918 C |
337 | |
338 | await sequelizeTypescript.transaction(async t => { | |
339 | await OAuthTokenModel.deleteUserToken(user.id, t) | |
340 | ||
341 | await user.save({ transaction: t }) | |
342 | }) | |
343 | ||
eacb25c4 C |
344 | await Emailer.Instance.addUserBlockJob(user, block, reason) |
345 | ||
e6921918 | 346 | auditLogger.update( |
993cef4b | 347 | getAuditIdFromRes(res), |
e6921918 C |
348 | new UserAuditView(user.toFormattedJSON()), |
349 | oldUserAuditView | |
350 | ) | |
351 | } |