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.js170
1 files changed, 170 insertions, 0 deletions
diff --git a/server/controllers/api/users.js b/server/controllers/api/users.js
new file mode 100644
index 000000000..b4d687312
--- /dev/null
+++ b/server/controllers/api/users.js
@@ -0,0 +1,170 @@
1'use strict'
2
3const each = require('async/each')
4const express = require('express')
5const mongoose = require('mongoose')
6const waterfall = require('async/waterfall')
7
8const constants = require('../../initializers/constants')
9const friends = require('../../lib/friends')
10const logger = require('../../helpers/logger')
11const middlewares = require('../../middlewares')
12const admin = middlewares.admin
13const oAuth = middlewares.oauth
14const pagination = middlewares.pagination
15const sort = middlewares.sort
16const validatorsPagination = middlewares.validators.pagination
17const validatorsSort = middlewares.validators.sort
18const validatorsUsers = middlewares.validators.users
19
20const User = mongoose.model('User')
21const Video = mongoose.model('Video')
22
23const router = express.Router()
24
25router.get('/me', oAuth.authenticate, getUserInformation)
26
27router.get('/',
28 validatorsPagination.pagination,
29 validatorsSort.usersSort,
30 sort.setUsersSort,
31 pagination.setPagination,
32 listUsers
33)
34
35router.post('/',
36 oAuth.authenticate,
37 admin.ensureIsAdmin,
38 validatorsUsers.usersAdd,
39 createUser
40)
41
42router.put('/:id',
43 oAuth.authenticate,
44 validatorsUsers.usersUpdate,
45 updateUser
46)
47
48router.delete('/:id',
49 oAuth.authenticate,
50 admin.ensureIsAdmin,
51 validatorsUsers.usersRemove,
52 removeUser
53)
54
55router.post('/token', oAuth.token, success)
56// TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route
57
58// ---------------------------------------------------------------------------
59
60module.exports = router
61
62// ---------------------------------------------------------------------------
63
64function createUser (req, res, next) {
65 const user = new User({
66 username: req.body.username,
67 password: req.body.password,
68 role: constants.USER_ROLES.USER
69 })
70
71 user.save(function (err, createdUser) {
72 if (err) return next(err)
73
74 return res.type('json').status(204).end()
75 })
76}
77
78function getUserInformation (req, res, next) {
79 User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) {
80 if (err) return next(err)
81
82 return res.json(user.toFormatedJSON())
83 })
84}
85
86function listUsers (req, res, next) {
87 User.listForApi(req.query.start, req.query.count, req.query.sort, function (err, usersList, usersTotal) {
88 if (err) return next(err)
89
90 res.json(getFormatedUsers(usersList, usersTotal))
91 })
92}
93
94function removeUser (req, res, next) {
95 waterfall([
96 function getUser (callback) {
97 User.loadById(req.params.id, callback)
98 },
99
100 function getVideos (user, callback) {
101 Video.listOwnedByAuthor(user.username, function (err, videos) {
102 return callback(err, user, videos)
103 })
104 },
105
106 function removeVideosFromDB (user, videos, callback) {
107 each(videos, function (video, callbackEach) {
108 video.remove(callbackEach)
109 }, function (err) {
110 return callback(err, user, videos)
111 })
112 },
113
114 function sendInformationToFriends (user, videos, callback) {
115 videos.forEach(function (video) {
116 const params = {
117 name: video.name,
118 magnetUri: video.magnetUri
119 }
120
121 friends.removeVideoToFriends(params)
122 })
123
124 return callback(null, user)
125 },
126
127 function removeUserFromDB (user, callback) {
128 user.remove(callback)
129 }
130 ], function andFinally (err) {
131 if (err) {
132 logger.error('Errors when removed the user.', { error: err })
133 return next(err)
134 }
135
136 return res.sendStatus(204)
137 })
138}
139
140function updateUser (req, res, next) {
141 User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) {
142 if (err) return next(err)
143
144 user.password = req.body.password
145 user.save(function (err) {
146 if (err) return next(err)
147
148 return res.sendStatus(204)
149 })
150 })
151}
152
153function success (req, res, next) {
154 res.end()
155}
156
157// ---------------------------------------------------------------------------
158
159function getFormatedUsers (users, usersTotal) {
160 const formatedUsers = []
161
162 users.forEach(function (user) {
163 formatedUsers.push(user.toFormatedJSON())
164 })
165
166 return {
167 total: usersTotal,
168 data: formatedUsers
169 }
170}