users.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. const express = require('express');
  2. const validator = require('../lib/validator');
  3. const jwtdecode = require('../lib/express/jwt-decode');
  4. const userIdFromMe = require('../lib/express/user-id-from-me');
  5. const internalUser = require('../internal/user');
  6. const apiValidator = require('../lib/validator/api');
  7. const schema = require('../schema');
  8. let router = express.Router({
  9. caseSensitive: true,
  10. strict: true,
  11. mergeParams: true
  12. });
  13. /**
  14. * /api/users
  15. */
  16. router
  17. .route('/')
  18. .options((_, res) => {
  19. res.sendStatus(204);
  20. })
  21. .all(jwtdecode())
  22. /**
  23. * GET /api/users
  24. *
  25. * Retrieve all users
  26. */
  27. .get((req, res, next) => {
  28. validator({
  29. additionalProperties: false,
  30. properties: {
  31. expand: {
  32. $ref: 'common#/definitions/expand'
  33. },
  34. query: {
  35. $ref: 'common#/definitions/query'
  36. }
  37. }
  38. }, {
  39. expand: (typeof req.query.expand === 'string' ? req.query.expand.split(',') : null),
  40. query: (typeof req.query.query === 'string' ? req.query.query : null)
  41. })
  42. .then((data) => {
  43. return internalUser.getAll(res.locals.access, data.expand, data.query);
  44. })
  45. .then((users) => {
  46. res.status(200)
  47. .send(users);
  48. })
  49. .catch((err) => {
  50. console.log(err);
  51. next(err);
  52. });
  53. //.catch(next);
  54. })
  55. /**
  56. * POST /api/users
  57. *
  58. * Create a new User
  59. */
  60. .post((req, res, next) => {
  61. apiValidator(schema.getValidationSchema('/users', 'post'), req.body)
  62. .then((payload) => {
  63. return internalUser.create(res.locals.access, payload);
  64. })
  65. .then((result) => {
  66. res.status(201)
  67. .send(result);
  68. })
  69. .catch(next);
  70. });
  71. /**
  72. * Specific user
  73. *
  74. * /api/users/123
  75. */
  76. router
  77. .route('/:user_id')
  78. .options((_, res) => {
  79. res.sendStatus(204);
  80. })
  81. .all(jwtdecode())
  82. .all(userIdFromMe)
  83. /**
  84. * GET /users/123 or /users/me
  85. *
  86. * Retrieve a specific user
  87. */
  88. .get((req, res, next) => {
  89. validator({
  90. required: ['user_id'],
  91. additionalProperties: false,
  92. properties: {
  93. user_id: {
  94. $ref: 'common#/definitions/id'
  95. },
  96. expand: {
  97. $ref: 'common#/definitions/expand'
  98. }
  99. }
  100. }, {
  101. user_id: req.params.user_id,
  102. expand: (typeof req.query.expand === 'string' ? req.query.expand.split(',') : null)
  103. })
  104. .then((data) => {
  105. return internalUser.get(res.locals.access, {
  106. id: data.user_id,
  107. expand: data.expand,
  108. omit: internalUser.getUserOmisionsByAccess(res.locals.access, data.user_id)
  109. });
  110. })
  111. .then((user) => {
  112. res.status(200)
  113. .send(user);
  114. })
  115. .catch((err) => {
  116. console.log(err);
  117. next(err);
  118. });
  119. })
  120. /**
  121. * PUT /api/users/123
  122. *
  123. * Update and existing user
  124. */
  125. .put((req, res, next) => {
  126. apiValidator(schema.getValidationSchema('/users/{userID}', 'put'), req.body)
  127. .then((payload) => {
  128. payload.id = req.params.user_id;
  129. return internalUser.update(res.locals.access, payload);
  130. })
  131. .then((result) => {
  132. res.status(200)
  133. .send(result);
  134. })
  135. .catch(next);
  136. })
  137. /**
  138. * DELETE /api/users/123
  139. *
  140. * Update and existing user
  141. */
  142. .delete((req, res, next) => {
  143. internalUser.delete(res.locals.access, {id: req.params.user_id})
  144. .then((result) => {
  145. res.status(200)
  146. .send(result);
  147. })
  148. .catch(next);
  149. });
  150. /**
  151. * Specific user auth
  152. *
  153. * /api/users/123/auth
  154. */
  155. router
  156. .route('/:user_id/auth')
  157. .options((req, res) => {
  158. res.sendStatus(204);
  159. })
  160. .all(jwtdecode())
  161. .all(userIdFromMe)
  162. /**
  163. * PUT /api/users/123/auth
  164. *
  165. * Update password for a user
  166. */
  167. .put((req, res, next) => {
  168. apiValidator(schema.getValidationSchema('/users/{userID}/auth', 'put'), req.body)
  169. .then((payload) => {
  170. payload.id = req.params.user_id;
  171. return internalUser.setPassword(res.locals.access, payload);
  172. })
  173. .then((result) => {
  174. res.status(201)
  175. .send(result);
  176. })
  177. .catch(next);
  178. });
  179. /**
  180. * Specific user permissions
  181. *
  182. * /api/users/123/permissions
  183. */
  184. router
  185. .route('/:user_id/permissions')
  186. .options((req, res) => {
  187. res.sendStatus(204);
  188. })
  189. .all(jwtdecode())
  190. .all(userIdFromMe)
  191. /**
  192. * PUT /api/users/123/permissions
  193. *
  194. * Set some or all permissions for a user
  195. */
  196. .put((req, res, next) => {
  197. apiValidator(schema.getValidationSchema('/users/{userID}/permissions', 'put'), req.body)
  198. .then((payload) => {
  199. payload.id = req.params.user_id;
  200. return internalUser.setPermissions(res.locals.access, payload);
  201. })
  202. .then((result) => {
  203. res.status(201)
  204. .send(result);
  205. })
  206. .catch(next);
  207. });
  208. /**
  209. * Specific user login as
  210. *
  211. * /api/users/123/login
  212. */
  213. router
  214. .route('/:user_id/login')
  215. .options((_, res) => {
  216. res.sendStatus(204);
  217. })
  218. .all(jwtdecode())
  219. /**
  220. * POST /api/users/123/login
  221. *
  222. * Log in as a user
  223. */
  224. .post((req, res, next) => {
  225. internalUser.loginAs(res.locals.access, {id: parseInt(req.params.user_id, 10)})
  226. .then((result) => {
  227. res.status(201)
  228. .send(result);
  229. })
  230. .catch(next);
  231. });
  232. module.exports = router;