]>
Commit | Line | Data |
---|---|---|
9457bf88 C |
1 | 'use strict' |
2 | ||
9bd26629 | 3 | const each = require('async/each') |
0c1cbbfe | 4 | const express = require('express') |
9bd26629 C |
5 | const mongoose = require('mongoose') |
6 | const waterfall = require('async/waterfall') | |
fc51fde0 | 7 | |
9bd26629 C |
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 | |
5c39adb7 C |
14 | const pagination = middlewares.pagination |
15 | const sort = middlewares.sort | |
16 | const validatorsPagination = middlewares.validators.pagination | |
17 | const validatorsSort = middlewares.validators.sort | |
9bd26629 | 18 | const validatorsUsers = middlewares.validators.users |
9457bf88 | 19 | |
9bd26629 C |
20 | const User = mongoose.model('User') |
21 | const Video = mongoose.model('Video') | |
9457bf88 C |
22 | |
23 | const router = express.Router() | |
24 | ||
99a64bfe | 25 | router.get('/me', oAuth.authenticate, getUserInformation) |
9bd26629 | 26 | |
5c39adb7 C |
27 | router.get('/', |
28 | validatorsPagination.pagination, | |
29 | validatorsSort.usersSort, | |
30 | sort.setUsersSort, | |
31 | pagination.setPagination, | |
32 | listUsers | |
33 | ) | |
34 | ||
9bd26629 C |
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 | ||
68a3b9f2 | 48 | router.delete('/:id', |
9bd26629 C |
49 | oAuth.authenticate, |
50 | admin.ensureIsAdmin, | |
51 | validatorsUsers.usersRemove, | |
52 | removeUser | |
53 | ) | |
6606150c | 54 | |
69b0a27c | 55 | router.post('/token', oAuth.token, success) |
9bd26629 | 56 | // TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route |
9457bf88 C |
57 | |
58 | // --------------------------------------------------------------------------- | |
59 | ||
60 | module.exports = router | |
61 | ||
62 | // --------------------------------------------------------------------------- | |
63 | ||
9bd26629 C |
64 | function 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 | ||
99a64bfe C |
78 | function 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 | ||
9bd26629 | 86 | function listUsers (req, res, next) { |
5c39adb7 | 87 | User.listForApi(req.query.start, req.query.count, req.query.sort, function (err, usersList, usersTotal) { |
9bd26629 C |
88 | if (err) return next(err) |
89 | ||
5c39adb7 | 90 | res.json(getFormatedUsers(usersList, usersTotal)) |
9bd26629 C |
91 | }) |
92 | } | |
93 | ||
94 | function removeUser (req, res, next) { | |
95 | waterfall([ | |
96 | function getUser (callback) { | |
68a3b9f2 | 97 | User.loadById(req.params.id, callback) |
9bd26629 C |
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 | ||
10431358 | 136 | return res.sendStatus(204) |
9bd26629 C |
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 | ||
10431358 | 148 | return res.sendStatus(204) |
9bd26629 C |
149 | }) |
150 | }) | |
151 | } | |
152 | ||
9457bf88 C |
153 | function success (req, res, next) { |
154 | res.end() | |
155 | } | |
9bd26629 C |
156 | |
157 | // --------------------------------------------------------------------------- | |
158 | ||
5c39adb7 | 159 | function getFormatedUsers (users, usersTotal) { |
9bd26629 C |
160 | const formatedUsers = [] |
161 | ||
162 | users.forEach(function (user) { | |
163 | formatedUsers.push(user.toFormatedJSON()) | |
164 | }) | |
165 | ||
166 | return { | |
5c39adb7 | 167 | total: usersTotal, |
9bd26629 C |
168 | data: formatedUsers |
169 | } | |
170 | } |