]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/middlewares/validators/users.ts
Add user update for admins
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / users.ts
index eeb0e3557571744acae76b70dd489ef91c8b809e..ebb3435355a50ef8632ca45d42c86112b7413abd 100644 (file)
@@ -53,16 +53,35 @@ function usersRemoveValidator (req: express.Request, res: express.Response, next
 
 function usersUpdateValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
   req.checkParams('id', 'Should have a valid id').notEmpty().isInt()
+  req.checkBody('email', 'Should have a valid email attribute').optional().isEmail()
+  req.checkBody('videoQuota', 'Should have a valid user quota').optional().isUserVideoQuotaValid()
+
+  logger.debug('Checking usersUpdate parameters', { parameters: req.body })
+
+  checkErrors(req, res, () => {
+    checkUserExists(req.params.id, res, next)
+  })
+}
+
+function usersUpdateMeValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
   // Add old password verification
   req.checkBody('password', 'Should have a valid password').optional().isUserPasswordValid()
+  req.checkBody('email', 'Should have a valid email attribute').optional().isEmail()
   req.checkBody('displayNSFW', 'Should have a valid display Not Safe For Work attribute').optional().isUserDisplayNSFWValid()
-  req.checkBody('videoQuota', 'Should have a valid user quota').optional().isUserVideoQuotaValid()
 
   logger.debug('Checking usersUpdate parameters', { parameters: req.body })
 
   checkErrors(req, res, next)
 }
 
+function usersGetValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
+  req.checkParams('id', 'Should have a valid id').notEmpty().isInt()
+
+  checkErrors(req, res, () => {
+    checkUserExists(req.params.id, res, next)
+  })
+}
+
 function usersVideoRatingValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
   req.checkParams('videoId', 'Should have a valid video id').notEmpty().isVideoIdOrUUIDValid()
 
@@ -106,6 +125,24 @@ export {
   usersAddValidator,
   usersRemoveValidator,
   usersUpdateValidator,
+  usersUpdateMeValidator,
   usersVideoRatingValidator,
-  ensureUserRegistrationAllowed
+  ensureUserRegistrationAllowed,
+  usersGetValidator
+}
+
+// ---------------------------------------------------------------------------
+
+function checkUserExists (id: number, res: express.Response, callback: () => void) {
+  db.User.loadById(id)
+    .then(user => {
+      if (!user) return res.status(404).send('User not found')
+
+      res.locals.user = user
+      callback()
+    })
+    .catch(err => {
+      logger.error('Error in user request validator.', err)
+      return res.sendStatus(500)
+    })
 }