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