]>
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, |
1d49e1e2 | 74 | displayNSFW: false, |
9bd26629 C |
75 | role: constants.USER_ROLES.USER |
76 | }) | |
77 | ||
feb4bdfd | 78 | user.save().asCallback(function (err, createdUser) { |
9bd26629 C |
79 | if (err) return next(err) |
80 | ||
81 | return res.type('json').status(204).end() | |
82 | }) | |
83 | } | |
84 | ||
99a64bfe | 85 | function getUserInformation (req, res, next) { |
feb4bdfd | 86 | db.User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) { |
99a64bfe C |
87 | if (err) return next(err) |
88 | ||
89 | return res.json(user.toFormatedJSON()) | |
90 | }) | |
91 | } | |
92 | ||
d38b8281 C |
93 | function getUserVideoRating (req, res, next) { |
94 | const videoId = req.params.videoId | |
95 | const userId = res.locals.oauth.token.User.id | |
96 | ||
97 | db.UserVideoRate.load(userId, videoId, function (err, ratingObj) { | |
98 | if (err) return next(err) | |
99 | ||
100 | const rating = ratingObj ? ratingObj.type : 'none' | |
101 | ||
102 | res.json({ | |
103 | videoId, | |
104 | rating | |
105 | }) | |
106 | }) | |
107 | } | |
108 | ||
9bd26629 | 109 | function listUsers (req, res, next) { |
feb4bdfd | 110 | db.User.listForApi(req.query.start, req.query.count, req.query.sort, function (err, usersList, usersTotal) { |
9bd26629 C |
111 | if (err) return next(err) |
112 | ||
55fa55a9 | 113 | res.json(utils.getFormatedObjects(usersList, usersTotal)) |
9bd26629 C |
114 | }) |
115 | } | |
116 | ||
117 | function removeUser (req, res, next) { | |
118 | waterfall([ | |
98ac898a | 119 | function loadUser (callback) { |
feb4bdfd | 120 | db.User.loadById(req.params.id, callback) |
9bd26629 C |
121 | }, |
122 | ||
98ac898a | 123 | function deleteUser (user, callback) { |
feb4bdfd | 124 | user.destroy().asCallback(callback) |
9bd26629 C |
125 | } |
126 | ], function andFinally (err) { | |
127 | if (err) { | |
128 | logger.error('Errors when removed the user.', { error: err }) | |
129 | return next(err) | |
130 | } | |
131 | ||
10431358 | 132 | return res.sendStatus(204) |
9bd26629 C |
133 | }) |
134 | } | |
135 | ||
136 | function updateUser (req, res, next) { | |
feb4bdfd | 137 | db.User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) { |
9bd26629 C |
138 | if (err) return next(err) |
139 | ||
1d49e1e2 C |
140 | if (req.body.password) user.password = req.body.password |
141 | if (req.body.displayNSFW !== undefined) user.displayNSFW = req.body.displayNSFW | |
142 | ||
feb4bdfd | 143 | user.save().asCallback(function (err) { |
9bd26629 C |
144 | if (err) return next(err) |
145 | ||
10431358 | 146 | return res.sendStatus(204) |
9bd26629 C |
147 | }) |
148 | }) | |
149 | } | |
150 | ||
9457bf88 C |
151 | function success (req, res, next) { |
152 | res.end() | |
153 | } |