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