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