audit-log.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import express from "express";
  2. import internalAuditLog from "../internal/audit-log.js";
  3. import jwtdecode from "../lib/express/jwt-decode.js";
  4. import validator from "../lib/validator/index.js";
  5. import { express as logger } from "../logger.js";
  6. const router = express.Router({
  7. caseSensitive: true,
  8. strict: true,
  9. mergeParams: true,
  10. });
  11. /**
  12. * /api/audit-log
  13. */
  14. router
  15. .route("/")
  16. .options((_, res) => {
  17. res.sendStatus(204);
  18. })
  19. .all(jwtdecode())
  20. /**
  21. * GET /api/audit-log
  22. *
  23. * Retrieve all logs
  24. */
  25. .get(async (req, res, next) => {
  26. try {
  27. const data = await validator(
  28. {
  29. additionalProperties: false,
  30. properties: {
  31. expand: {
  32. $ref: "common#/properties/expand",
  33. },
  34. query: {
  35. $ref: "common#/properties/query",
  36. },
  37. },
  38. },
  39. {
  40. expand: typeof req.query.expand === "string" ? req.query.expand.split(",") : null,
  41. query: typeof req.query.query === "string" ? req.query.query : null,
  42. },
  43. );
  44. const rows = await internalAuditLog.getAll(res.locals.access, data.expand, data.query);
  45. res.status(200).send(rows);
  46. } catch (err) {
  47. logger.debug(`${req.method.toUpperCase()} ${req.path}: ${err}`);
  48. next(err);
  49. }
  50. });
  51. /**
  52. * Specific audit log entry
  53. *
  54. * /api/audit-log/123
  55. */
  56. router
  57. .route("/:event_id")
  58. .options((_, res) => {
  59. res.sendStatus(204);
  60. })
  61. .all(jwtdecode())
  62. /**
  63. * GET /api/audit-log/123
  64. *
  65. * Retrieve a specific entry
  66. */
  67. .get(async (req, res, next) => {
  68. try {
  69. const data = await validator(
  70. {
  71. required: ["event_id"],
  72. additionalProperties: false,
  73. properties: {
  74. event_id: {
  75. $ref: "common#/properties/id",
  76. },
  77. expand: {
  78. $ref: "common#/properties/expand",
  79. },
  80. },
  81. },
  82. {
  83. event_id: req.params.event_id,
  84. expand:
  85. typeof req.query.expand === "string"
  86. ? req.query.expand.split(",")
  87. : null,
  88. },
  89. );
  90. const item = await internalAuditLog.get(res.locals.access, {
  91. id: data.event_id,
  92. expand: data.expand,
  93. });
  94. res.status(200).send(item);
  95. } catch (err) {
  96. logger.debug(`${req.method.toUpperCase()} ${req.path}: ${err}`);
  97. next(err);
  98. }
  99. });
  100. export default router;