]>
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 | ||
99a64bfe | 21 | router.get('/me', oAuth.authenticate, getUserInformation) |
9bd26629 | 22 | |
5c39adb7 C |
23 | router.get('/', |
24 | validatorsPagination.pagination, | |
25 | validatorsSort.usersSort, | |
26 | sort.setUsersSort, | |
27 | pagination.setPagination, | |
28 | listUsers | |
29 | ) | |
30 | ||
9bd26629 C |
31 | router.post('/', |
32 | oAuth.authenticate, | |
33 | admin.ensureIsAdmin, | |
34 | validatorsUsers.usersAdd, | |
35 | createUser | |
36 | ) | |
37 | ||
38 | router.put('/:id', | |
39 | oAuth.authenticate, | |
40 | validatorsUsers.usersUpdate, | |
41 | updateUser | |
42 | ) | |
43 | ||
68a3b9f2 | 44 | router.delete('/:id', |
9bd26629 C |
45 | oAuth.authenticate, |
46 | admin.ensureIsAdmin, | |
47 | validatorsUsers.usersRemove, | |
48 | removeUser | |
49 | ) | |
6606150c | 50 | |
69b0a27c | 51 | router.post('/token', oAuth.token, success) |
9bd26629 | 52 | // TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route |
9457bf88 C |
53 | |
54 | // --------------------------------------------------------------------------- | |
55 | ||
56 | module.exports = router | |
57 | ||
58 | // --------------------------------------------------------------------------- | |
59 | ||
9bd26629 | 60 | function createUser (req, res, next) { |
feb4bdfd | 61 | const user = db.User.build({ |
9bd26629 C |
62 | username: req.body.username, |
63 | password: req.body.password, | |
64 | role: constants.USER_ROLES.USER | |
65 | }) | |
66 | ||
feb4bdfd | 67 | user.save().asCallback(function (err, createdUser) { |
9bd26629 C |
68 | if (err) return next(err) |
69 | ||
70 | return res.type('json').status(204).end() | |
71 | }) | |
72 | } | |
73 | ||
99a64bfe | 74 | function getUserInformation (req, res, next) { |
feb4bdfd | 75 | db.User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) { |
99a64bfe C |
76 | if (err) return next(err) |
77 | ||
78 | return res.json(user.toFormatedJSON()) | |
79 | }) | |
80 | } | |
81 | ||
9bd26629 | 82 | function listUsers (req, res, next) { |
feb4bdfd | 83 | db.User.listForApi(req.query.start, req.query.count, req.query.sort, function (err, usersList, usersTotal) { |
9bd26629 C |
84 | if (err) return next(err) |
85 | ||
55fa55a9 | 86 | res.json(utils.getFormatedObjects(usersList, usersTotal)) |
9bd26629 C |
87 | }) |
88 | } | |
89 | ||
90 | function removeUser (req, res, next) { | |
91 | waterfall([ | |
98ac898a | 92 | function loadUser (callback) { |
feb4bdfd | 93 | db.User.loadById(req.params.id, callback) |
9bd26629 C |
94 | }, |
95 | ||
98ac898a | 96 | function deleteUser (user, callback) { |
feb4bdfd | 97 | user.destroy().asCallback(callback) |
9bd26629 C |
98 | } |
99 | ], function andFinally (err) { | |
100 | if (err) { | |
101 | logger.error('Errors when removed the user.', { error: err }) | |
102 | return next(err) | |
103 | } | |
104 | ||
10431358 | 105 | return res.sendStatus(204) |
9bd26629 C |
106 | }) |
107 | } | |
108 | ||
109 | function updateUser (req, res, next) { | |
feb4bdfd | 110 | db.User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) { |
9bd26629 C |
111 | if (err) return next(err) |
112 | ||
113 | user.password = req.body.password | |
feb4bdfd | 114 | user.save().asCallback(function (err) { |
9bd26629 C |
115 | if (err) return next(err) |
116 | ||
10431358 | 117 | return res.sendStatus(204) |
9bd26629 C |
118 | }) |
119 | }) | |
120 | } | |
121 | ||
9457bf88 C |
122 | function success (req, res, next) { |
123 | res.end() | |
124 | } |