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