3 const express
= require('express')
4 const waterfall
= require('async/waterfall')
6 const constants
= require('../../initializers/constants')
7 const db
= require('../../initializers/database')
8 const logger
= require('../../helpers/logger')
9 const utils
= require('../../helpers/utils')
10 const middlewares
= require('../../middlewares')
11 const admin
= middlewares
.admin
12 const oAuth
= middlewares
.oauth
13 const pagination
= middlewares
.pagination
14 const sort
= middlewares
.sort
15 const validatorsPagination
= middlewares
.validators
.pagination
16 const validatorsSort
= middlewares
.validators
.sort
17 const validatorsUsers
= middlewares
.validators
.users
19 const router
= express
.Router()
26 router
.get('/me/videos/:videoId/rating',
28 validatorsUsers
.usersVideoRating
,
33 validatorsPagination
.pagination
,
34 validatorsSort
.usersSort
,
36 pagination
.setPagination
,
43 validatorsUsers
.usersAdd
,
47 router
.post('/register',
48 ensureRegistrationEnabled
,
49 validatorsUsers
.usersAdd
,
55 validatorsUsers
.usersUpdate
,
62 validatorsUsers
.usersRemove
,
66 router
.post('/token', oAuth
.token
, success
)
67 // TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route
69 // ---------------------------------------------------------------------------
71 module
.exports
= router
73 // ---------------------------------------------------------------------------
75 function ensureRegistrationEnabled (req
, res
, next
) {
76 const registrationEnabled
= constants
.CONFIG
.SIGNUP
.ENABLED
78 if (registrationEnabled
=== true) {
82 return res
.status(400).send('User registration is not enabled.')
85 function createUser (req
, res
, next
) {
86 const user
= db
.User
.build({
87 username: req
.body
.username
,
88 password: req
.body
.password
,
89 email: req
.body
.email
,
91 role: constants
.USER_ROLES
.USER
94 user
.save().asCallback(function (err
, createdUser
) {
95 if (err
) return next(err
)
97 return res
.type('json').status(204).end()
101 function getUserInformation (req
, res
, next
) {
102 db
.User
.loadByUsername(res
.locals
.oauth
.token
.user
.username
, function (err
, user
) {
103 if (err
) return next(err
)
105 return res
.json(user
.toFormatedJSON())
109 function getUserVideoRating (req
, res
, next
) {
110 const videoId
= req
.params
.videoId
111 const userId
= res
.locals
.oauth
.token
.User
.id
113 db
.UserVideoRate
.load(userId
, videoId
, function (err
, ratingObj
) {
114 if (err
) return next(err
)
116 const rating
= ratingObj
? ratingObj
.type : 'none'
125 function listUsers (req
, res
, next
) {
126 db
.User
.listForApi(req
.query
.start
, req
.query
.count
, req
.query
.sort
, function (err
, usersList
, usersTotal
) {
127 if (err
) return next(err
)
129 res
.json(utils
.getFormatedObjects(usersList
, usersTotal
))
133 function removeUser (req
, res
, next
) {
135 function loadUser (callback
) {
136 db
.User
.loadById(req
.params
.id
, callback
)
139 function deleteUser (user
, callback
) {
140 user
.destroy().asCallback(callback
)
142 ], function andFinally (err
) {
144 logger
.error('Errors when removed the user.', { error: err
})
148 return res
.sendStatus(204)
152 function updateUser (req
, res
, next
) {
153 db
.User
.loadByUsername(res
.locals
.oauth
.token
.user
.username
, function (err
, user
) {
154 if (err
) return next(err
)
156 if (req
.body
.password
) user
.password
= req
.body
.password
157 if (req
.body
.displayNSFW
!== undefined) user
.displayNSFW
= req
.body
.displayNSFW
159 user
.save().asCallback(function (err
) {
160 if (err
) return next(err
)
162 return res
.sendStatus(204)
167 function success (req
, res
, next
) {