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