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