]>
Commit | Line | Data |
---|---|---|
1 | 'use strict' | |
2 | ||
3 | const express = require('express') | |
4 | const waterfall = require('async/waterfall') | |
5 | ||
6 | const constants = require('../../initializers/constants') | |
7 | const db = require('../../initializers/database') | |
8 | const logger = require('../../helpers/logger') | |
9 | const utils = require('../../helpers/utils') | |
10 | const middlewares = require('../../middlewares') | |
11 | const admin = middlewares.admin | |
12 | const oAuth = middlewares.oauth | |
13 | const pagination = middlewares.pagination | |
14 | const sort = middlewares.sort | |
15 | const validatorsPagination = middlewares.validators.pagination | |
16 | const validatorsSort = middlewares.validators.sort | |
17 | const validatorsUsers = middlewares.validators.users | |
18 | ||
19 | const router = express.Router() | |
20 | ||
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 | ) | |
31 | ||
32 | router.get('/', | |
33 | validatorsPagination.pagination, | |
34 | validatorsSort.usersSort, | |
35 | sort.setUsersSort, | |
36 | pagination.setPagination, | |
37 | listUsers | |
38 | ) | |
39 | ||
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 | ||
53 | router.delete('/:id', | |
54 | oAuth.authenticate, | |
55 | admin.ensureIsAdmin, | |
56 | validatorsUsers.usersRemove, | |
57 | removeUser | |
58 | ) | |
59 | ||
60 | router.post('/token', oAuth.token, success) | |
61 | // TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route | |
62 | ||
63 | // --------------------------------------------------------------------------- | |
64 | ||
65 | module.exports = router | |
66 | ||
67 | // --------------------------------------------------------------------------- | |
68 | ||
69 | function createUser (req, res, next) { | |
70 | const user = db.User.build({ | |
71 | username: req.body.username, | |
72 | password: req.body.password, | |
73 | email: req.body.email, | |
74 | role: constants.USER_ROLES.USER | |
75 | }) | |
76 | ||
77 | user.save().asCallback(function (err, createdUser) { | |
78 | if (err) return next(err) | |
79 | ||
80 | return res.type('json').status(204).end() | |
81 | }) | |
82 | } | |
83 | ||
84 | function getUserInformation (req, res, next) { | |
85 | db.User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) { | |
86 | if (err) return next(err) | |
87 | ||
88 | return res.json(user.toFormatedJSON()) | |
89 | }) | |
90 | } | |
91 | ||
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 | ||
108 | function listUsers (req, res, next) { | |
109 | db.User.listForApi(req.query.start, req.query.count, req.query.sort, function (err, usersList, usersTotal) { | |
110 | if (err) return next(err) | |
111 | ||
112 | res.json(utils.getFormatedObjects(usersList, usersTotal)) | |
113 | }) | |
114 | } | |
115 | ||
116 | function removeUser (req, res, next) { | |
117 | waterfall([ | |
118 | function loadUser (callback) { | |
119 | db.User.loadById(req.params.id, callback) | |
120 | }, | |
121 | ||
122 | function deleteUser (user, callback) { | |
123 | user.destroy().asCallback(callback) | |
124 | } | |
125 | ], function andFinally (err) { | |
126 | if (err) { | |
127 | logger.error('Errors when removed the user.', { error: err }) | |
128 | return next(err) | |
129 | } | |
130 | ||
131 | return res.sendStatus(204) | |
132 | }) | |
133 | } | |
134 | ||
135 | function updateUser (req, res, next) { | |
136 | db.User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) { | |
137 | if (err) return next(err) | |
138 | ||
139 | user.password = req.body.password | |
140 | user.save().asCallback(function (err) { | |
141 | if (err) return next(err) | |
142 | ||
143 | return res.sendStatus(204) | |
144 | }) | |
145 | }) | |
146 | } | |
147 | ||
148 | function success (req, res, next) { | |
149 | res.end() | |
150 | } |