access_lists.js 3.2 KB

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