]>
Commit | Line | Data |
---|---|---|
ecb4e35f | 1 | import * as Bluebird from 'bluebird' |
69818c93 | 2 | import * as express from 'express' |
a2431b7d C |
3 | import 'express-validator' |
4 | import { body, param } from 'express-validator/check' | |
ecb4e35f | 5 | import { omit } from 'lodash' |
3fd3ab2d | 6 | import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' |
b60e5f38 | 7 | import { |
eacb25c4 | 8 | isUserAutoPlayVideoValid, isUserBlockedReasonValid, |
4bbfc6c6 C |
9 | isUserDescriptionValid, |
10 | isUserDisplayNameValid, | |
0883b324 | 11 | isUserNSFWPolicyValid, |
ecb4e35f C |
12 | isUserPasswordValid, |
13 | isUserRoleValid, | |
14 | isUserUsernameValid, | |
3fd3ab2d C |
15 | isUserVideoQuotaValid |
16 | } from '../../helpers/custom-validators/users' | |
47564bbe | 17 | import { isVideoExist } from '../../helpers/custom-validators/videos' |
da854ddd | 18 | import { logger } from '../../helpers/logger' |
06215f15 | 19 | import { isSignupAllowed, isSignupAllowedForCurrentIP } from '../../helpers/signup' |
ecb4e35f | 20 | import { Redis } from '../../lib/redis' |
3fd3ab2d | 21 | import { UserModel } from '../../models/account/user' |
a2431b7d | 22 | import { areValidationErrors } from './utils' |
2ef6a063 | 23 | import { ActorModel } from '../../models/activitypub/actor' |
9bd26629 | 24 | |
b60e5f38 | 25 | const usersAddValidator = [ |
563d032e | 26 | body('username').custom(isUserUsernameValid).withMessage('Should have a valid username (lowercase alphanumeric characters)'), |
b60e5f38 C |
27 | body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'), |
28 | body('email').isEmail().withMessage('Should have a valid email'), | |
29 | body('videoQuota').custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'), | |
954605a8 | 30 | body('role').custom(isUserRoleValid).withMessage('Should have a valid role'), |
9bd26629 | 31 | |
a2431b7d | 32 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
ce97fe36 | 33 | logger.debug('Checking usersAdd parameters', { parameters: omit(req.body, 'password') }) |
9bd26629 | 34 | |
a2431b7d C |
35 | if (areValidationErrors(req, res)) return |
36 | if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return | |
37 | ||
38 | return next() | |
b60e5f38 C |
39 | } |
40 | ] | |
6fcd19ba | 41 | |
b60e5f38 C |
42 | const usersRegisterValidator = [ |
43 | body('username').custom(isUserUsernameValid).withMessage('Should have a valid username'), | |
44 | body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'), | |
45 | body('email').isEmail().withMessage('Should have a valid email'), | |
77a5501f | 46 | |
a2431b7d | 47 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
ce97fe36 | 48 | logger.debug('Checking usersRegister parameters', { parameters: omit(req.body, 'password') }) |
77a5501f | 49 | |
a2431b7d C |
50 | if (areValidationErrors(req, res)) return |
51 | if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return | |
52 | ||
53 | return next() | |
b60e5f38 C |
54 | } |
55 | ] | |
9bd26629 | 56 | |
b60e5f38 C |
57 | const usersRemoveValidator = [ |
58 | param('id').isInt().not().isEmpty().withMessage('Should have a valid id'), | |
9bd26629 | 59 | |
a2431b7d | 60 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
b60e5f38 | 61 | logger.debug('Checking usersRemove parameters', { parameters: req.params }) |
9bd26629 | 62 | |
a2431b7d C |
63 | if (areValidationErrors(req, res)) return |
64 | if (!await checkUserIdExist(req.params.id, res)) return | |
65 | ||
66 | const user = res.locals.user | |
67 | if (user.username === 'root') { | |
68 | return res.status(400) | |
69 | .send({ error: 'Cannot remove the root user' }) | |
70 | .end() | |
71 | } | |
72 | ||
73 | return next() | |
b60e5f38 C |
74 | } |
75 | ] | |
8094a898 | 76 | |
e6921918 C |
77 | const usersBlockingValidator = [ |
78 | param('id').isInt().not().isEmpty().withMessage('Should have a valid id'), | |
eacb25c4 | 79 | body('reason').optional().custom(isUserBlockedReasonValid).withMessage('Should have a valid blocking reason'), |
e6921918 C |
80 | |
81 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
eacb25c4 | 82 | logger.debug('Checking usersBlocking parameters', { parameters: req.params }) |
e6921918 C |
83 | |
84 | if (areValidationErrors(req, res)) return | |
85 | if (!await checkUserIdExist(req.params.id, res)) return | |
86 | ||
87 | const user = res.locals.user | |
88 | if (user.username === 'root') { | |
89 | return res.status(400) | |
90 | .send({ error: 'Cannot block the root user' }) | |
91 | .end() | |
92 | } | |
93 | ||
94 | return next() | |
95 | } | |
96 | ] | |
97 | ||
92b9d60c C |
98 | const deleteMeValidator = [ |
99 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
100 | const user: UserModel = res.locals.oauth.token.User | |
101 | if (user.username === 'root') { | |
102 | return res.status(400) | |
103 | .send({ error: 'You cannot delete your root account.' }) | |
104 | .end() | |
105 | } | |
106 | ||
107 | return next() | |
108 | } | |
109 | ] | |
110 | ||
b60e5f38 C |
111 | const usersUpdateValidator = [ |
112 | param('id').isInt().not().isEmpty().withMessage('Should have a valid id'), | |
113 | body('email').optional().isEmail().withMessage('Should have a valid email attribute'), | |
114 | body('videoQuota').optional().custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'), | |
954605a8 | 115 | body('role').optional().custom(isUserRoleValid).withMessage('Should have a valid role'), |
8094a898 | 116 | |
a2431b7d | 117 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
b60e5f38 | 118 | logger.debug('Checking usersUpdate parameters', { parameters: req.body }) |
9bd26629 | 119 | |
a2431b7d C |
120 | if (areValidationErrors(req, res)) return |
121 | if (!await checkUserIdExist(req.params.id, res)) return | |
122 | ||
f8b8c36b C |
123 | const user = res.locals.user |
124 | if (user.username === 'root' && req.body.role !== undefined && user.role !== req.body.role) { | |
125 | return res.status(400) | |
126 | .send({ error: 'Cannot change root role.' }) | |
127 | .end() | |
128 | } | |
129 | ||
a2431b7d | 130 | return next() |
b60e5f38 C |
131 | } |
132 | ] | |
9bd26629 | 133 | |
b60e5f38 | 134 | const usersUpdateMeValidator = [ |
ed56ad11 | 135 | body('displayName').optional().custom(isUserDisplayNameValid).withMessage('Should have a valid display name'), |
2422c46b | 136 | body('description').optional().custom(isUserDescriptionValid).withMessage('Should have a valid description'), |
b60e5f38 C |
137 | body('password').optional().custom(isUserPasswordValid).withMessage('Should have a valid password'), |
138 | body('email').optional().isEmail().withMessage('Should have a valid email attribute'), | |
0883b324 | 139 | body('nsfwPolicy').optional().custom(isUserNSFWPolicyValid).withMessage('Should have a valid display Not Safe For Work policy'), |
7efe153b | 140 | body('autoPlayVideo').optional().custom(isUserAutoPlayVideoValid).withMessage('Should have a valid automatically plays video attribute'), |
9bd26629 | 141 | |
b60e5f38 C |
142 | (req: express.Request, res: express.Response, next: express.NextFunction) => { |
143 | // TODO: Add old password verification | |
ce97fe36 | 144 | logger.debug('Checking usersUpdateMe parameters', { parameters: omit(req.body, 'password') }) |
8094a898 | 145 | |
a2431b7d C |
146 | if (areValidationErrors(req, res)) return |
147 | ||
148 | return next() | |
b60e5f38 C |
149 | } |
150 | ] | |
8094a898 | 151 | |
b60e5f38 C |
152 | const usersGetValidator = [ |
153 | param('id').isInt().not().isEmpty().withMessage('Should have a valid id'), | |
d38b8281 | 154 | |
a2431b7d | 155 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
ce97fe36 | 156 | logger.debug('Checking usersGet parameters', { parameters: req.params }) |
a2431b7d C |
157 | |
158 | if (areValidationErrors(req, res)) return | |
159 | if (!await checkUserIdExist(req.params.id, res)) return | |
160 | ||
161 | return next() | |
b60e5f38 C |
162 | } |
163 | ] | |
d38b8281 | 164 | |
b60e5f38 | 165 | const usersVideoRatingValidator = [ |
72c7248b | 166 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'), |
0a6658fd | 167 | |
a2431b7d | 168 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
b60e5f38 | 169 | logger.debug('Checking usersVideoRating parameters', { parameters: req.params }) |
0a6658fd | 170 | |
a2431b7d C |
171 | if (areValidationErrors(req, res)) return |
172 | if (!await isVideoExist(req.params.videoId, res)) return | |
173 | ||
174 | return next() | |
b60e5f38 C |
175 | } |
176 | ] | |
177 | ||
178 | const ensureUserRegistrationAllowed = [ | |
a2431b7d C |
179 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
180 | const allowed = await isSignupAllowed() | |
181 | if (allowed === false) { | |
182 | return res.status(403) | |
183 | .send({ error: 'User registration is not enabled or user limit is reached.' }) | |
184 | .end() | |
185 | } | |
186 | ||
187 | return next() | |
b60e5f38 C |
188 | } |
189 | ] | |
291e8d3e | 190 | |
ff2c1fe8 RK |
191 | const ensureUserRegistrationAllowedForIP = [ |
192 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
193 | const allowed = isSignupAllowedForCurrentIP(req.ip) | |
194 | ||
195 | if (allowed === false) { | |
196 | return res.status(403) | |
197 | .send({ error: 'You are not on a network authorized for registration.' }) | |
198 | .end() | |
199 | } | |
200 | ||
201 | return next() | |
202 | } | |
203 | ] | |
204 | ||
ecb4e35f C |
205 | const usersAskResetPasswordValidator = [ |
206 | body('email').isEmail().not().isEmpty().withMessage('Should have a valid email'), | |
207 | ||
208 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
209 | logger.debug('Checking usersAskResetPassword parameters', { parameters: req.body }) | |
210 | ||
211 | if (areValidationErrors(req, res)) return | |
212 | const exists = await checkUserEmailExist(req.body.email, res, false) | |
213 | if (!exists) { | |
214 | logger.debug('User with email %s does not exist (asking reset password).', req.body.email) | |
215 | // Do not leak our emails | |
216 | return res.status(204).end() | |
217 | } | |
218 | ||
219 | return next() | |
220 | } | |
221 | ] | |
222 | ||
223 | const usersResetPasswordValidator = [ | |
224 | param('id').isInt().not().isEmpty().withMessage('Should have a valid id'), | |
225 | body('verificationString').not().isEmpty().withMessage('Should have a valid verification string'), | |
226 | body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'), | |
227 | ||
228 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
229 | logger.debug('Checking usersResetPassword parameters', { parameters: req.params }) | |
230 | ||
231 | if (areValidationErrors(req, res)) return | |
232 | if (!await checkUserIdExist(req.params.id, res)) return | |
233 | ||
234 | const user = res.locals.user as UserModel | |
235 | const redisVerificationString = await Redis.Instance.getResetPasswordLink(user.id) | |
236 | ||
237 | if (redisVerificationString !== req.body.verificationString) { | |
238 | return res | |
239 | .status(403) | |
240 | .send({ error: 'Invalid verification string.' }) | |
f076daa7 | 241 | .end() |
ecb4e35f C |
242 | } |
243 | ||
244 | return next() | |
245 | } | |
246 | ] | |
247 | ||
9bd26629 C |
248 | // --------------------------------------------------------------------------- |
249 | ||
65fcc311 C |
250 | export { |
251 | usersAddValidator, | |
92b9d60c | 252 | deleteMeValidator, |
77a5501f | 253 | usersRegisterValidator, |
e6921918 | 254 | usersBlockingValidator, |
65fcc311 C |
255 | usersRemoveValidator, |
256 | usersUpdateValidator, | |
8094a898 | 257 | usersUpdateMeValidator, |
291e8d3e | 258 | usersVideoRatingValidator, |
8094a898 | 259 | ensureUserRegistrationAllowed, |
ff2c1fe8 | 260 | ensureUserRegistrationAllowedForIP, |
c5911fd3 | 261 | usersGetValidator, |
ecb4e35f C |
262 | usersAskResetPasswordValidator, |
263 | usersResetPasswordValidator | |
8094a898 C |
264 | } |
265 | ||
266 | // --------------------------------------------------------------------------- | |
267 | ||
ecb4e35f C |
268 | function checkUserIdExist (id: number, res: express.Response) { |
269 | return checkUserExist(() => UserModel.loadById(id), res) | |
270 | } | |
a2431b7d | 271 | |
ecb4e35f C |
272 | function checkUserEmailExist (email: string, res: express.Response, abortResponse = true) { |
273 | return checkUserExist(() => UserModel.loadByEmail(email), res, abortResponse) | |
65fcc311 | 274 | } |
77a5501f | 275 | |
a2431b7d | 276 | async function checkUserNameOrEmailDoesNotAlreadyExist (username: string, email: string, res: express.Response) { |
3fd3ab2d | 277 | const user = await UserModel.loadByUsernameOrEmail(username, email) |
a2431b7d C |
278 | |
279 | if (user) { | |
280 | res.status(409) | |
edf7f40a | 281 | .send({ error: 'User with this username or email already exists.' }) |
a2431b7d C |
282 | .end() |
283 | return false | |
284 | } | |
285 | ||
2ef6a063 C |
286 | const actor = await ActorModel.loadLocalByName(username) |
287 | if (actor) { | |
288 | res.status(409) | |
289 | .send({ error: 'Another actor (account/channel) with this name already exists.' }) | |
290 | .end() | |
291 | return false | |
292 | } | |
293 | ||
a2431b7d | 294 | return true |
77a5501f | 295 | } |
ecb4e35f C |
296 | |
297 | async function checkUserExist (finder: () => Bluebird<UserModel>, res: express.Response, abortResponse = true) { | |
298 | const user = await finder() | |
299 | ||
300 | if (!user) { | |
301 | if (abortResponse === true) { | |
302 | res.status(404) | |
303 | .send({ error: 'User not found' }) | |
304 | .end() | |
305 | } | |
306 | ||
307 | return false | |
308 | } | |
309 | ||
310 | res.locals.user = user | |
311 | ||
312 | return true | |
313 | } |