]>
Commit | Line | Data |
---|---|---|
9457bf88 C |
1 | 'use strict' |
2 | ||
0c1cbbfe | 3 | const express = require('express') |
9bd26629 | 4 | const waterfall = require('async/waterfall') |
fc51fde0 | 5 | |
f253b1c1 | 6 | const constants = require('../../initializers/constants') |
feb4bdfd | 7 | const db = require('../../initializers/database') |
f253b1c1 | 8 | const logger = require('../../helpers/logger') |
55fa55a9 | 9 | const utils = require('../../helpers/utils') |
f253b1c1 | 10 | const middlewares = require('../../middlewares') |
9bd26629 C |
11 | const admin = middlewares.admin |
12 | const oAuth = middlewares.oauth | |
5c39adb7 C |
13 | const pagination = middlewares.pagination |
14 | const sort = middlewares.sort | |
15 | const validatorsPagination = middlewares.validators.pagination | |
16 | const validatorsSort = middlewares.validators.sort | |
9bd26629 | 17 | const validatorsUsers = middlewares.validators.users |
9457bf88 | 18 | |
9457bf88 C |
19 | const router = express.Router() |
20 | ||
d38b8281 C |
21 | router.get('/me', |
22 | oAuth.authenticate, | |
23 | getUserInformation | |
24 | ) | |
25 | ||
26 | router.get('/me/videos/:videoId/rating', | |
27 | oAuth.authenticate, | |
28 | validatorsUsers.usersVideoRating, | |
29 | getUserVideoRating | |
30 | ) | |
9bd26629 | 31 | |
5c39adb7 C |
32 | router.get('/', |
33 | validatorsPagination.pagination, | |
34 | validatorsSort.usersSort, | |
35 | sort.setUsersSort, | |
36 | pagination.setPagination, | |
37 | listUsers | |
38 | ) | |
39 | ||
9bd26629 C |
40 | router.post('/', |
41 | oAuth.authenticate, | |
42 | admin.ensureIsAdmin, | |
43 | validatorsUsers.usersAdd, | |
44 | createUser | |
45 | ) | |
46 | ||
47 | router.put('/:id', | |
48 | oAuth.authenticate, | |
49 | validatorsUsers.usersUpdate, | |
50 | updateUser | |
51 | ) | |
52 | ||
68a3b9f2 | 53 | router.delete('/:id', |
9bd26629 C |
54 | oAuth.authenticate, |
55 | admin.ensureIsAdmin, | |
56 | validatorsUsers.usersRemove, | |
57 | removeUser | |
58 | ) | |
6606150c | 59 | |
69b0a27c | 60 | router.post('/token', oAuth.token, success) |
9bd26629 | 61 | // TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route |
9457bf88 C |
62 | |
63 | // --------------------------------------------------------------------------- | |
64 | ||
65 | module.exports = router | |
66 | ||
67 | // --------------------------------------------------------------------------- | |
68 | ||
9bd26629 | 69 | function createUser (req, res, next) { |
feb4bdfd | 70 | const user = db.User.build({ |
9bd26629 C |
71 | username: req.body.username, |
72 | password: req.body.password, | |
ad4a8a1c | 73 | email: req.body.email, |
9bd26629 C |
74 | role: constants.USER_ROLES.USER |
75 | }) | |
76 | ||
feb4bdfd | 77 | user.save().asCallback(function (err, createdUser) { |
9bd26629 C |
78 | if (err) return next(err) |
79 | ||
80 | return res.type('json').status(204).end() | |
81 | }) | |
82 | } | |
83 | ||
99a64bfe | 84 | function getUserInformation (req, res, next) { |
feb4bdfd | 85 | db.User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) { |
99a64bfe C |
86 | if (err) return next(err) |
87 | ||
88 | return res.json(user.toFormatedJSON()) | |
89 | }) | |
90 | } | |
91 | ||
d38b8281 C |
92 | function getUserVideoRating (req, res, next) { |
93 | const videoId = req.params.videoId | |
94 | const userId = res.locals.oauth.token.User.id | |
95 | ||
96 | db.UserVideoRate.load(userId, videoId, function (err, ratingObj) { | |
97 | if (err) return next(err) | |
98 | ||
99 | const rating = ratingObj ? ratingObj.type : 'none' | |
100 | ||
101 | res.json({ | |
102 | videoId, | |
103 | rating | |
104 | }) | |
105 | }) | |
106 | } | |
107 | ||
9bd26629 | 108 | function listUsers (req, res, next) { |
feb4bdfd | 109 | db.User.listForApi(req.query.start, req.query.count, req.query.sort, function (err, usersList, usersTotal) { |
9bd26629 C |
110 | if (err) return next(err) |
111 | ||
55fa55a9 | 112 | res.json(utils.getFormatedObjects(usersList, usersTotal)) |
9bd26629 C |
113 | }) |
114 | } | |
115 | ||
116 | function removeUser (req, res, next) { | |
117 | waterfall([ | |
98ac898a | 118 | function loadUser (callback) { |
feb4bdfd | 119 | db.User.loadById(req.params.id, callback) |
9bd26629 C |
120 | }, |
121 | ||
98ac898a | 122 | function deleteUser (user, callback) { |
feb4bdfd | 123 | user.destroy().asCallback(callback) |
9bd26629 C |
124 | } |
125 | ], function andFinally (err) { | |
126 | if (err) { | |
127 | logger.error('Errors when removed the user.', { error: err }) | |
128 | return next(err) | |
129 | } | |
130 | ||
10431358 | 131 | return res.sendStatus(204) |
9bd26629 C |
132 | }) |
133 | } | |
134 | ||
135 | function updateUser (req, res, next) { | |
feb4bdfd | 136 | db.User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) { |
9bd26629 C |
137 | if (err) return next(err) |
138 | ||
139 | user.password = req.body.password | |
feb4bdfd | 140 | user.save().asCallback(function (err) { |
9bd26629 C |
141 | if (err) return next(err) |
142 | ||
10431358 | 143 | return res.sendStatus(204) |
9bd26629 C |
144 | }) |
145 | }) | |
146 | } | |
147 | ||
9457bf88 C |
148 | function success (req, res, next) { |
149 | res.end() | |
150 | } |