diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-10-02 15:39:09 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-10-02 15:39:09 +0200 |
commit | a6375e69668ea42e19531c6bc68dcd37f3f7cbd7 (patch) | |
tree | 03204a408d56311692c3528bedcf95d2455e94f2 /server/controllers/api/v1/users.js | |
parent | 052937db8a8d282eccdbdf38d487ed8d85d3c0a7 (diff) | |
parent | c4403b29ad4db097af528a7f04eea07e0ed320d0 (diff) | |
download | PeerTube-a6375e69668ea42e19531c6bc68dcd37f3f7cbd7.tar.gz PeerTube-a6375e69668ea42e19531c6bc68dcd37f3f7cbd7.tar.zst PeerTube-a6375e69668ea42e19531c6bc68dcd37f3f7cbd7.zip |
Merge branch 'master' into webseed-merged
Diffstat (limited to 'server/controllers/api/v1/users.js')
-rw-r--r-- | server/controllers/api/v1/users.js | 165 |
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 | ||
3 | const config = require('config') | 3 | const each = require('async/each') |
4 | const mongoose = require('mongoose') | ||
5 | const express = require('express') | 4 | const express = require('express') |
5 | const mongoose = require('mongoose') | ||
6 | const waterfall = require('async/waterfall') | ||
6 | 7 | ||
7 | const oAuth = require('../../../middlewares').oauth | 8 | const constants = require('../../../initializers/constants') |
9 | const friends = require('../../../lib/friends') | ||
10 | const logger = require('../../../helpers/logger') | ||
11 | const middlewares = require('../../../middlewares') | ||
12 | const admin = middlewares.admin | ||
13 | const oAuth = middlewares.oauth | ||
14 | const pagination = middlewares.pagination | ||
15 | const sort = middlewares.sort | ||
16 | const validatorsPagination = middlewares.validators.pagination | ||
17 | const validatorsSort = middlewares.validators.sort | ||
18 | const validatorsUsers = middlewares.validators.users | ||
8 | 19 | ||
9 | const Client = mongoose.model('OAuthClient') | 20 | const User = mongoose.model('User') |
21 | const Video = mongoose.model('Video') | ||
10 | 22 | ||
11 | const router = express.Router() | 23 | const router = express.Router() |
12 | 24 | ||
13 | router.get('/client', getAngularClient) | 25 | router.get('/me', oAuth.authenticate, getUserInformation) |
26 | |||
27 | router.get('/', | ||
28 | validatorsPagination.pagination, | ||
29 | validatorsSort.usersSort, | ||
30 | sort.setUsersSort, | ||
31 | pagination.setPagination, | ||
32 | listUsers | ||
33 | ) | ||
34 | |||
35 | router.post('/', | ||
36 | oAuth.authenticate, | ||
37 | admin.ensureIsAdmin, | ||
38 | validatorsUsers.usersAdd, | ||
39 | createUser | ||
40 | ) | ||
41 | |||
42 | router.put('/:id', | ||
43 | oAuth.authenticate, | ||
44 | validatorsUsers.usersUpdate, | ||
45 | updateUser | ||
46 | ) | ||
47 | |||
48 | router.delete('/:id', | ||
49 | oAuth.authenticate, | ||
50 | admin.ensureIsAdmin, | ||
51 | validatorsUsers.usersRemove, | ||
52 | removeUser | ||
53 | ) | ||
54 | |||
14 | router.post('/token', oAuth.token, success) | 55 | router.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 | ||
23 | function getAngularClient (req, res, next) { | 64 | function 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 | |||
78 | function 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 | |||
86 | function 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 | |||
94 | function 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 | |||
140 | function 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) { | |||
47 | function success (req, res, next) { | 153 | function success (req, res, next) { |
48 | res.end() | 154 | res.end() |
49 | } | 155 | } |
156 | |||
157 | // --------------------------------------------------------------------------- | ||
158 | |||
159 | function 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 | } | ||