aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/users.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers/api/users.js')
-rw-r--r--server/controllers/api/users.js169
1 files changed, 0 insertions, 169 deletions
diff --git a/server/controllers/api/users.js b/server/controllers/api/users.js
deleted file mode 100644
index c7fe7bf85..000000000
--- a/server/controllers/api/users.js
+++ /dev/null
@@ -1,169 +0,0 @@
1'use strict'
2
3const express = require('express')
4const waterfall = require('async/waterfall')
5
6const constants = require('../../initializers/constants')
7const db = require('../../initializers/database')
8const logger = require('../../helpers/logger')
9const utils = require('../../helpers/utils')
10const middlewares = require('../../middlewares')
11const admin = middlewares.admin
12const oAuth = middlewares.oauth
13const pagination = middlewares.pagination
14const sort = middlewares.sort
15const validatorsPagination = middlewares.validators.pagination
16const validatorsSort = middlewares.validators.sort
17const validatorsUsers = middlewares.validators.users
18
19const router = express.Router()
20
21router.get('/me',
22 oAuth.authenticate,
23 getUserInformation
24)
25
26router.get('/me/videos/:videoId/rating',
27 oAuth.authenticate,
28 validatorsUsers.usersVideoRating,
29 getUserVideoRating
30)
31
32router.get('/',
33 validatorsPagination.pagination,
34 validatorsSort.usersSort,
35 sort.setUsersSort,
36 pagination.setPagination,
37 listUsers
38)
39
40router.post('/',
41 oAuth.authenticate,
42 admin.ensureIsAdmin,
43 validatorsUsers.usersAdd,
44 createUser
45)
46
47router.post('/register',
48 ensureRegistrationEnabled,
49 validatorsUsers.usersAdd,
50 createUser
51)
52
53router.put('/:id',
54 oAuth.authenticate,
55 validatorsUsers.usersUpdate,
56 updateUser
57)
58
59router.delete('/:id',
60 oAuth.authenticate,
61 admin.ensureIsAdmin,
62 validatorsUsers.usersRemove,
63 removeUser
64)
65
66router.post('/token', oAuth.token, success)
67// TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route
68
69// ---------------------------------------------------------------------------
70
71module.exports = router
72
73// ---------------------------------------------------------------------------
74
75function ensureRegistrationEnabled (req, res, next) {
76 const registrationEnabled = constants.CONFIG.SIGNUP.ENABLED
77
78 if (registrationEnabled === true) {
79 return next()
80 }
81
82 return res.status(400).send('User registration is not enabled.')
83}
84
85function 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,
90 displayNSFW: false,
91 role: constants.USER_ROLES.USER
92 })
93
94 user.save().asCallback(function (err, createdUser) {
95 if (err) return next(err)
96
97 return res.type('json').status(204).end()
98 })
99}
100
101function getUserInformation (req, res, next) {
102 db.User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) {
103 if (err) return next(err)
104
105 return res.json(user.toFormatedJSON())
106 })
107}
108
109function getUserVideoRating (req, res, next) {
110 const videoId = req.params.videoId
111 const userId = res.locals.oauth.token.User.id
112
113 db.UserVideoRate.load(userId, videoId, function (err, ratingObj) {
114 if (err) return next(err)
115
116 const rating = ratingObj ? ratingObj.type : 'none'
117
118 res.json({
119 videoId,
120 rating
121 })
122 })
123}
124
125function 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)
128
129 res.json(utils.getFormatedObjects(usersList, usersTotal))
130 })
131}
132
133function removeUser (req, res, next) {
134 waterfall([
135 function loadUser (callback) {
136 db.User.loadById(req.params.id, callback)
137 },
138
139 function deleteUser (user, callback) {
140 user.destroy().asCallback(callback)
141 }
142 ], function andFinally (err) {
143 if (err) {
144 logger.error('Errors when removed the user.', { error: err })
145 return next(err)
146 }
147
148 return res.sendStatus(204)
149 })
150}
151
152function updateUser (req, res, next) {
153 db.User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) {
154 if (err) return next(err)
155
156 if (req.body.password) user.password = req.body.password
157 if (req.body.displayNSFW !== undefined) user.displayNSFW = req.body.displayNSFW
158
159 user.save().asCallback(function (err) {
160 if (err) return next(err)
161
162 return res.sendStatus(204)
163 })
164 })
165}
166
167function success (req, res, next) {
168 res.end()
169}