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