2017-05-15 15:22:03 -05:00
|
|
|
import express = require('express')
|
|
|
|
import { waterfall } from 'async'
|
2016-03-21 05:56:33 -05:00
|
|
|
|
2017-05-22 13:58:25 -05:00
|
|
|
import { database as db } from '../../initializers/database'
|
2017-05-15 15:22:03 -05:00
|
|
|
import { CONFIG, USER_ROLES } from '../../initializers'
|
|
|
|
import { logger, getFormatedObjects } from '../../helpers'
|
|
|
|
import {
|
|
|
|
authenticate,
|
|
|
|
ensureIsAdmin,
|
|
|
|
usersAddValidator,
|
|
|
|
usersUpdateValidator,
|
|
|
|
usersRemoveValidator,
|
|
|
|
usersVideoRatingValidator,
|
|
|
|
paginationValidator,
|
|
|
|
setPagination,
|
|
|
|
usersSortValidator,
|
|
|
|
setUsersSort,
|
|
|
|
token
|
|
|
|
} from '../../middlewares'
|
|
|
|
|
|
|
|
const usersRouter = express.Router()
|
|
|
|
|
|
|
|
usersRouter.get('/me',
|
|
|
|
authenticate,
|
2017-03-08 14:35:43 -06:00
|
|
|
getUserInformation
|
|
|
|
)
|
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
usersRouter.get('/me/videos/:videoId/rating',
|
|
|
|
authenticate,
|
|
|
|
usersVideoRatingValidator,
|
2017-03-08 14:35:43 -06:00
|
|
|
getUserVideoRating
|
|
|
|
)
|
2016-08-04 15:32:36 -05:00
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
usersRouter.get('/',
|
|
|
|
paginationValidator,
|
|
|
|
usersSortValidator,
|
|
|
|
setUsersSort,
|
|
|
|
setPagination,
|
2016-08-16 15:31:45 -05:00
|
|
|
listUsers
|
|
|
|
)
|
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
usersRouter.post('/',
|
|
|
|
authenticate,
|
|
|
|
ensureIsAdmin,
|
|
|
|
usersAddValidator,
|
2016-08-04 15:32:36 -05:00
|
|
|
createUser
|
|
|
|
)
|
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
usersRouter.post('/register',
|
2017-04-09 05:08:36 -05:00
|
|
|
ensureRegistrationEnabled,
|
2017-05-15 15:22:03 -05:00
|
|
|
usersAddValidator,
|
2017-04-09 05:08:36 -05:00
|
|
|
createUser
|
|
|
|
)
|
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
usersRouter.put('/:id',
|
|
|
|
authenticate,
|
|
|
|
usersUpdateValidator,
|
2016-08-04 15:32:36 -05:00
|
|
|
updateUser
|
|
|
|
)
|
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
usersRouter.delete('/:id',
|
|
|
|
authenticate,
|
|
|
|
ensureIsAdmin,
|
|
|
|
usersRemoveValidator,
|
2016-08-04 15:32:36 -05:00
|
|
|
removeUser
|
|
|
|
)
|
2016-08-05 09:09:39 -05:00
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
usersRouter.post('/token', token, success)
|
2016-08-04 15:32:36 -05:00
|
|
|
// TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route
|
2016-03-21 05:56:33 -05:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
export {
|
|
|
|
usersRouter
|
|
|
|
}
|
2016-03-21 05:56:33 -05:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-04-09 05:08:36 -05:00
|
|
|
function ensureRegistrationEnabled (req, res, next) {
|
2017-05-15 15:22:03 -05:00
|
|
|
const registrationEnabled = CONFIG.SIGNUP.ENABLED
|
2017-04-09 05:08:36 -05:00
|
|
|
|
|
|
|
if (registrationEnabled === true) {
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.status(400).send('User registration is not enabled.')
|
|
|
|
}
|
|
|
|
|
2016-08-04 15:32:36 -05:00
|
|
|
function createUser (req, res, next) {
|
2016-12-11 14:50:51 -06:00
|
|
|
const user = db.User.build({
|
2016-08-04 15:32:36 -05:00
|
|
|
username: req.body.username,
|
|
|
|
password: req.body.password,
|
2017-02-18 02:29:59 -06:00
|
|
|
email: req.body.email,
|
2017-04-03 14:24:36 -05:00
|
|
|
displayNSFW: false,
|
2017-05-15 15:22:03 -05:00
|
|
|
role: USER_ROLES.USER
|
2016-08-04 15:32:36 -05:00
|
|
|
})
|
|
|
|
|
2016-12-11 14:50:51 -06:00
|
|
|
user.save().asCallback(function (err, createdUser) {
|
2016-08-04 15:32:36 -05:00
|
|
|
if (err) return next(err)
|
|
|
|
|
|
|
|
return res.type('json').status(204).end()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-08-05 10:19:08 -05:00
|
|
|
function getUserInformation (req, res, next) {
|
2016-12-11 14:50:51 -06:00
|
|
|
db.User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) {
|
2016-08-05 10:19:08 -05:00
|
|
|
if (err) return next(err)
|
|
|
|
|
|
|
|
return res.json(user.toFormatedJSON())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-03-08 14:35:43 -06:00
|
|
|
function getUserVideoRating (req, res, next) {
|
|
|
|
const videoId = req.params.videoId
|
|
|
|
const userId = res.locals.oauth.token.User.id
|
|
|
|
|
2017-05-22 13:58:25 -05:00
|
|
|
db.UserVideoRate.load(userId, videoId, null, function (err, ratingObj) {
|
2017-03-08 14:35:43 -06:00
|
|
|
if (err) return next(err)
|
|
|
|
|
|
|
|
const rating = ratingObj ? ratingObj.type : 'none'
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
videoId,
|
|
|
|
rating
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-08-04 15:32:36 -05:00
|
|
|
function listUsers (req, res, next) {
|
2016-12-11 14:50:51 -06:00
|
|
|
db.User.listForApi(req.query.start, req.query.count, req.query.sort, function (err, usersList, usersTotal) {
|
2016-08-04 15:32:36 -05:00
|
|
|
if (err) return next(err)
|
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
res.json(getFormatedObjects(usersList, usersTotal))
|
2016-08-04 15:32:36 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeUser (req, res, next) {
|
|
|
|
waterfall([
|
2016-12-29 04:17:11 -06:00
|
|
|
function loadUser (callback) {
|
2016-12-11 14:50:51 -06:00
|
|
|
db.User.loadById(req.params.id, callback)
|
2016-08-04 15:32:36 -05:00
|
|
|
},
|
|
|
|
|
2016-12-29 04:17:11 -06:00
|
|
|
function deleteUser (user, callback) {
|
2016-12-11 14:50:51 -06:00
|
|
|
user.destroy().asCallback(callback)
|
2016-08-04 15:32:36 -05:00
|
|
|
}
|
|
|
|
], function andFinally (err) {
|
|
|
|
if (err) {
|
|
|
|
logger.error('Errors when removed the user.', { error: err })
|
|
|
|
return next(err)
|
|
|
|
}
|
|
|
|
|
2016-08-05 11:08:55 -05:00
|
|
|
return res.sendStatus(204)
|
2016-08-04 15:32:36 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateUser (req, res, next) {
|
2016-12-11 14:50:51 -06:00
|
|
|
db.User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) {
|
2016-08-04 15:32:36 -05:00
|
|
|
if (err) return next(err)
|
|
|
|
|
2017-04-03 14:24:36 -05:00
|
|
|
if (req.body.password) user.password = req.body.password
|
|
|
|
if (req.body.displayNSFW !== undefined) user.displayNSFW = req.body.displayNSFW
|
|
|
|
|
2016-12-11 14:50:51 -06:00
|
|
|
user.save().asCallback(function (err) {
|
2016-08-04 15:32:36 -05:00
|
|
|
if (err) return next(err)
|
|
|
|
|
2016-08-05 11:08:55 -05:00
|
|
|
return res.sendStatus(204)
|
2016-08-04 15:32:36 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-03-21 05:56:33 -05:00
|
|
|
function success (req, res, next) {
|
|
|
|
res.end()
|
|
|
|
}
|