]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/users.ts
Add user update for admins
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / users.ts
1 import 'express-validator'
2 import * as express from 'express'
3 import * as Promise from 'bluebird'
4 import * as validator from 'validator'
5
6 import { database as db } from '../../initializers/database'
7 import { checkErrors } from './utils'
8 import { isSignupAllowed, logger } from '../../helpers'
9 import { VideoInstance } from '../../models'
10
11 function usersAddValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
12 req.checkBody('username', 'Should have a valid username').isUserUsernameValid()
13 req.checkBody('password', 'Should have a valid password').isUserPasswordValid()
14 req.checkBody('email', 'Should have a valid email').isEmail()
15 req.checkBody('videoQuota', 'Should have a valid user quota').isUserVideoQuotaValid()
16
17 logger.debug('Checking usersAdd parameters', { parameters: req.body })
18
19 checkErrors(req, res, () => {
20 db.User.loadByUsernameOrEmail(req.body.username, req.body.email)
21 .then(user => {
22 if (user) return res.status(409).send('User already exists.')
23
24 next()
25 })
26 .catch(err => {
27 logger.error('Error in usersAdd request validator.', err)
28 return res.sendStatus(500)
29 })
30 })
31 }
32
33 function usersRemoveValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
34 req.checkParams('id', 'Should have a valid id').notEmpty().isInt()
35
36 logger.debug('Checking usersRemove parameters', { parameters: req.params })
37
38 checkErrors(req, res, () => {
39 db.User.loadById(req.params.id)
40 .then(user => {
41 if (!user) return res.status(404).send('User not found')
42
43 if (user.username === 'root') return res.status(400).send('Cannot remove the root user')
44
45 next()
46 })
47 .catch(err => {
48 logger.error('Error in usersRemove request validator.', err)
49 return res.sendStatus(500)
50 })
51 })
52 }
53
54 function usersUpdateValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
55 req.checkParams('id', 'Should have a valid id').notEmpty().isInt()
56 req.checkBody('email', 'Should have a valid email attribute').optional().isEmail()
57 req.checkBody('videoQuota', 'Should have a valid user quota').optional().isUserVideoQuotaValid()
58
59 logger.debug('Checking usersUpdate parameters', { parameters: req.body })
60
61 checkErrors(req, res, () => {
62 checkUserExists(req.params.id, res, next)
63 })
64 }
65
66 function usersUpdateMeValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
67 // Add old password verification
68 req.checkBody('password', 'Should have a valid password').optional().isUserPasswordValid()
69 req.checkBody('email', 'Should have a valid email attribute').optional().isEmail()
70 req.checkBody('displayNSFW', 'Should have a valid display Not Safe For Work attribute').optional().isUserDisplayNSFWValid()
71
72 logger.debug('Checking usersUpdate parameters', { parameters: req.body })
73
74 checkErrors(req, res, next)
75 }
76
77 function usersGetValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
78 req.checkParams('id', 'Should have a valid id').notEmpty().isInt()
79
80 checkErrors(req, res, () => {
81 checkUserExists(req.params.id, res, next)
82 })
83 }
84
85 function usersVideoRatingValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
86 req.checkParams('videoId', 'Should have a valid video id').notEmpty().isVideoIdOrUUIDValid()
87
88 logger.debug('Checking usersVideoRating parameters', { parameters: req.params })
89
90 checkErrors(req, res, () => {
91 let videoPromise: Promise<VideoInstance>
92
93 if (validator.isUUID(req.params.videoId)) {
94 videoPromise = db.Video.loadByUUID(req.params.videoId)
95 } else {
96 videoPromise = db.Video.load(req.params.videoId)
97 }
98
99 videoPromise
100 .then(video => {
101 if (!video) return res.status(404).send('Video not found')
102
103 next()
104 })
105 .catch(err => {
106 logger.error('Error in user request validator.', err)
107 return res.sendStatus(500)
108 })
109 })
110 }
111
112 function ensureUserRegistrationAllowed (req: express.Request, res: express.Response, next: express.NextFunction) {
113 isSignupAllowed().then(allowed => {
114 if (allowed === false) {
115 return res.status(403).send('User registration is not enabled or user limit is reached.')
116 }
117
118 return next()
119 })
120 }
121
122 // ---------------------------------------------------------------------------
123
124 export {
125 usersAddValidator,
126 usersRemoveValidator,
127 usersUpdateValidator,
128 usersUpdateMeValidator,
129 usersVideoRatingValidator,
130 ensureUserRegistrationAllowed,
131 usersGetValidator
132 }
133
134 // ---------------------------------------------------------------------------
135
136 function checkUserExists (id: number, res: express.Response, callback: () => void) {
137 db.User.loadById(id)
138 .then(user => {
139 if (!user) return res.status(404).send('User not found')
140
141 res.locals.user = user
142 callback()
143 })
144 .catch(err => {
145 logger.error('Error in user request validator.', err)
146 return res.sendStatus(500)
147 })
148 }