From 9bd2662976a75d3b03364cdbe6419e57c80f99a6 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 4 Aug 2016 22:32:36 +0200 Subject: Implement user API (create, update, remove, list) --- server/middlewares/validators/index.js | 2 ++ server/middlewares/validators/users.js | 57 +++++++++++++++++++++++++++++++++ server/middlewares/validators/videos.js | 1 + 3 files changed, 60 insertions(+) create mode 100644 server/middlewares/validators/users.js (limited to 'server/middlewares/validators') diff --git a/server/middlewares/validators/index.js b/server/middlewares/validators/index.js index 0471b3f92..6c3a9c2b4 100644 --- a/server/middlewares/validators/index.js +++ b/server/middlewares/validators/index.js @@ -4,6 +4,7 @@ const paginationValidators = require('./pagination') const podsValidators = require('./pods') const remoteValidators = require('./remote') const sortValidators = require('./sort') +const usersValidators = require('./users') const videosValidators = require('./videos') const validators = { @@ -11,6 +12,7 @@ const validators = { pods: podsValidators, remote: remoteValidators, sort: sortValidators, + users: usersValidators, videos: videosValidators } diff --git a/server/middlewares/validators/users.js b/server/middlewares/validators/users.js new file mode 100644 index 000000000..175d90bcb --- /dev/null +++ b/server/middlewares/validators/users.js @@ -0,0 +1,57 @@ +'use strict' + +const mongoose = require('mongoose') + +const checkErrors = require('./utils').checkErrors +const logger = require('../../helpers/logger') + +const User = mongoose.model('User') + +const validatorsUsers = { + usersAdd: usersAdd, + usersRemove: usersRemove, + usersUpdate: usersUpdate +} + +function usersAdd (req, res, next) { + req.checkBody('username', 'Should have a valid username').isUserUsernameValid() + req.checkBody('password', 'Should have a valid password').isUserPasswordValid() + + // TODO: check we don't have already the same username + + logger.debug('Checking usersAdd parameters', { parameters: req.body }) + + checkErrors(req, res, next) +} + +function usersRemove (req, res, next) { + req.checkParams('username', 'Should have a valid username').isUserUsernameValid() + + logger.debug('Checking usersRemove parameters', { parameters: req.params }) + + checkErrors(req, res, function () { + User.loadByUsername(req.params.username, function (err, user) { + if (err) { + logger.error('Error in usersRemove request validator.', { error: err }) + return res.sendStatus(500) + } + + if (!user) return res.status(404).send('User not found') + + next() + }) + }) +} + +function usersUpdate (req, res, next) { + // Add old password verification + req.checkBody('password', 'Should have a valid password').isUserPasswordValid() + + logger.debug('Checking usersUpdate parameters', { parameters: req.body }) + + checkErrors(req, res, next) +} + +// --------------------------------------------------------------------------- + +module.exports = validatorsUsers diff --git a/server/middlewares/validators/videos.js b/server/middlewares/validators/videos.js index 422f3642f..9d21ee16f 100644 --- a/server/middlewares/validators/videos.js +++ b/server/middlewares/validators/videos.js @@ -18,6 +18,7 @@ const validatorsVideos = { function videosAdd (req, res, next) { req.checkFiles('videofile[0].originalname', 'Should have an input video').notEmpty() + // TODO: move to constants and function req.checkFiles('videofile[0].mimetype', 'Should have a correct mime type').matches(/video\/(webm)|(mp4)|(ogg)/i) req.checkBody('name', 'Should have a valid name').isVideoNameValid() req.checkBody('description', 'Should have a valid description').isVideoDescriptionValid() -- cgit v1.2.3