diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-09-05 21:29:39 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-09-05 21:29:39 +0200 |
commit | 8094a8980265a0a28e508dbd7cf7c7029e6d98b6 (patch) | |
tree | 26be2b9b43dfe485ea14eb53d3a1adb6247c35f8 /server/middlewares/validators/users.ts | |
parent | 980246ea8f1c51a137eaf0c441ef7e3b6fb88810 (diff) | |
download | PeerTube-8094a8980265a0a28e508dbd7cf7c7029e6d98b6.tar.gz PeerTube-8094a8980265a0a28e508dbd7cf7c7029e6d98b6.tar.zst PeerTube-8094a8980265a0a28e508dbd7cf7c7029e6d98b6.zip |
Add user update for admins
Diffstat (limited to 'server/middlewares/validators/users.ts')
-rw-r--r-- | server/middlewares/validators/users.ts | 41 |
1 files changed, 39 insertions, 2 deletions
diff --git a/server/middlewares/validators/users.ts b/server/middlewares/validators/users.ts index eeb0e3557..ebb343535 100644 --- a/server/middlewares/validators/users.ts +++ b/server/middlewares/validators/users.ts | |||
@@ -53,16 +53,35 @@ function usersRemoveValidator (req: express.Request, res: express.Response, next | |||
53 | 53 | ||
54 | function usersUpdateValidator (req: express.Request, res: express.Response, next: express.NextFunction) { | 54 | function usersUpdateValidator (req: express.Request, res: express.Response, next: express.NextFunction) { |
55 | req.checkParams('id', 'Should have a valid id').notEmpty().isInt() | 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) { | ||
56 | // Add old password verification | 67 | // Add old password verification |
57 | req.checkBody('password', 'Should have a valid password').optional().isUserPasswordValid() | 68 | req.checkBody('password', 'Should have a valid password').optional().isUserPasswordValid() |
69 | req.checkBody('email', 'Should have a valid email attribute').optional().isEmail() | ||
58 | req.checkBody('displayNSFW', 'Should have a valid display Not Safe For Work attribute').optional().isUserDisplayNSFWValid() | 70 | req.checkBody('displayNSFW', 'Should have a valid display Not Safe For Work attribute').optional().isUserDisplayNSFWValid() |
59 | req.checkBody('videoQuota', 'Should have a valid user quota').optional().isUserVideoQuotaValid() | ||
60 | 71 | ||
61 | logger.debug('Checking usersUpdate parameters', { parameters: req.body }) | 72 | logger.debug('Checking usersUpdate parameters', { parameters: req.body }) |
62 | 73 | ||
63 | checkErrors(req, res, next) | 74 | checkErrors(req, res, next) |
64 | } | 75 | } |
65 | 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 | |||
66 | function usersVideoRatingValidator (req: express.Request, res: express.Response, next: express.NextFunction) { | 85 | function usersVideoRatingValidator (req: express.Request, res: express.Response, next: express.NextFunction) { |
67 | req.checkParams('videoId', 'Should have a valid video id').notEmpty().isVideoIdOrUUIDValid() | 86 | req.checkParams('videoId', 'Should have a valid video id').notEmpty().isVideoIdOrUUIDValid() |
68 | 87 | ||
@@ -106,6 +125,24 @@ export { | |||
106 | usersAddValidator, | 125 | usersAddValidator, |
107 | usersRemoveValidator, | 126 | usersRemoveValidator, |
108 | usersUpdateValidator, | 127 | usersUpdateValidator, |
128 | usersUpdateMeValidator, | ||
109 | usersVideoRatingValidator, | 129 | usersVideoRatingValidator, |
110 | ensureUserRegistrationAllowed | 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 | }) | ||
111 | } | 148 | } |