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