aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/v1/users.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers/api/v1/users.js')
-rw-r--r--server/controllers/api/v1/users.js165
1 files changed, 143 insertions, 22 deletions
diff --git a/server/controllers/api/v1/users.js b/server/controllers/api/v1/users.js
index fbbe6e472..975e25e68 100644
--- a/server/controllers/api/v1/users.js
+++ b/server/controllers/api/v1/users.js
@@ -1,18 +1,59 @@
1'use strict' 1'use strict'
2 2
3const config = require('config') 3const each = require('async/each')
4const mongoose = require('mongoose')
5const express = require('express') 4const express = require('express')
5const mongoose = require('mongoose')
6const waterfall = require('async/waterfall')
6 7
7const oAuth = require('../../../middlewares').oauth 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
8 19
9const Client = mongoose.model('OAuthClient') 20const User = mongoose.model('User')
21const Video = mongoose.model('Video')
10 22
11const router = express.Router() 23const router = express.Router()
12 24
13router.get('/client', getAngularClient) 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
14router.post('/token', oAuth.token, success) 55router.post('/token', oAuth.token, success)
15// TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged,, implement revoke token route 56// TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route
16 57
17// --------------------------------------------------------------------------- 58// ---------------------------------------------------------------------------
18 59
@@ -20,26 +61,91 @@ module.exports = router
20 61
21// --------------------------------------------------------------------------- 62// ---------------------------------------------------------------------------
22 63
23function getAngularClient (req, res, next) { 64function createUser (req, res, next) {
24 const serverHost = config.get('webserver.host') 65 const user = new User({
25 const serverPort = config.get('webserver.port') 66 username: req.body.username,
26 let headerHostShouldBe = serverHost 67 password: req.body.password,
27 if (serverPort !== 80 && serverPort !== 443) { 68 role: constants.USER_ROLES.USER
28 headerHostShouldBe += ':' + serverPort 69 })
29 }
30 70
31 // Don't make this check if this is a test instance 71 user.save(function (err, createdUser) {
32 if (process.env.NODE_ENV !== 'test' && req.get('host') !== headerHostShouldBe) { 72 if (err) return next(err)
33 return res.type('json').status(403).end()
34 }
35 73
36 Client.loadFirstClient(function (err, client) { 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) {
37 if (err) return next(err) 80 if (err) return next(err)
38 if (!client) return next(new Error('No client available.'))
39 81
40 res.json({ 82 return res.json(user.toFormatedJSON())
41 client_id: client._id, 83 })
42 client_secret: client.clientSecret 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)
43 }) 149 })
44 }) 150 })
45} 151}
@@ -47,3 +153,18 @@ function getAngularClient (req, res, next) {
47function success (req, res, next) { 153function success (req, res, next) {
48 res.end() 154 res.end()
49} 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}