access_lists.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. const express = require('express');
  2. const validator = require('../../lib/validator');
  3. const jwtdecode = require('../../lib/express/jwt-decode');
  4. const apiValidator = require('../../lib/validator/api');
  5. const internalAccessList = require('../../internal/access-list');
  6. const schema = require('../../schema');
  7. let router = express.Router({
  8. caseSensitive: true,
  9. strict: true,
  10. mergeParams: true
  11. });
  12. /**
  13. * /api/nginx/access-lists
  14. */
  15. router
  16. .route('/')
  17. .options((req, res) => {
  18. res.sendStatus(204);
  19. })
  20. .all(jwtdecode())
  21. /**
  22. * GET /api/nginx/access-lists
  23. *
  24. * Retrieve all access-lists
  25. */
  26. .get((req, res, next) => {
  27. validator({
  28. additionalProperties: false,
  29. properties: {
  30. expand: {
  31. $ref: 'common#/properties/expand'
  32. },
  33. query: {
  34. $ref: 'common#/properties/query'
  35. }
  36. }
  37. }, {
  38. expand: (typeof req.query.expand === 'string' ? req.query.expand.split(',') : null),
  39. query: (typeof req.query.query === 'string' ? req.query.query : null)
  40. })
  41. .then((data) => {
  42. return internalAccessList.getAll(res.locals.access, data.expand, data.query);
  43. })
  44. .then((rows) => {
  45. res.status(200)
  46. .send(rows);
  47. })
  48. .catch(next);
  49. })
  50. /**
  51. * POST /api/nginx/access-lists
  52. *
  53. * Create a new access-list
  54. */
  55. .post((req, res, next) => {
  56. apiValidator(schema.getValidationSchema('/nginx/access-lists', 'post'), req.body)
  57. .then((payload) => {
  58. return internalAccessList.create(res.locals.access, payload);
  59. })
  60. .then((result) => {
  61. res.status(201)
  62. .send(result);
  63. })
  64. .catch(next);
  65. });
  66. /**
  67. * Specific access-list
  68. *
  69. * /api/nginx/access-lists/123
  70. */
  71. router
  72. .route('/:list_id')
  73. .options((_, res) => {
  74. res.sendStatus(204);
  75. })
  76. .all(jwtdecode())
  77. /**
  78. * GET /api/nginx/access-lists/123
  79. *
  80. * Retrieve a specific access-list
  81. */
  82. .get((req, res, next) => {
  83. validator({
  84. required: ['list_id'],
  85. additionalProperties: false,
  86. properties: {
  87. list_id: {
  88. $ref: 'common#/properties/id'
  89. },
  90. expand: {
  91. $ref: 'common#/properties/expand'
  92. }
  93. }
  94. }, {
  95. list_id: req.params.list_id,
  96. expand: (typeof req.query.expand === 'string' ? req.query.expand.split(',') : null)
  97. })
  98. .then((data) => {
  99. return internalAccessList.get(res.locals.access, {
  100. id: parseInt(data.list_id, 10),
  101. expand: data.expand
  102. });
  103. })
  104. .then((row) => {
  105. res.status(200)
  106. .send(row);
  107. })
  108. .catch(next);
  109. })
  110. /**
  111. * PUT /api/nginx/access-lists/123
  112. *
  113. * Update and existing access-list
  114. */
  115. .put((req, res, next) => {
  116. apiValidator(schema.getValidationSchema('/nginx/access-lists/{listID}', 'put'), req.body)
  117. .then((payload) => {
  118. payload.id = parseInt(req.params.list_id, 10);
  119. return internalAccessList.update(res.locals.access, payload);
  120. })
  121. .then((result) => {
  122. res.status(200)
  123. .send(result);
  124. })
  125. .catch(next);
  126. })
  127. /**
  128. * DELETE /api/nginx/access-lists/123
  129. *
  130. * Delete and existing access-list
  131. */
  132. .delete((req, res, next) => {
  133. internalAccessList.delete(res.locals.access, {id: parseInt(req.params.list_id, 10)})
  134. .then((result) => {
  135. res.status(200)
  136. .send(result);
  137. })
  138. .catch(next);
  139. });
  140. module.exports = router;