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