security-setting.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* eslint-disable no-unused-vars */
  2. const loggerFactory = require('@alias/logger');
  3. const logger = loggerFactory('growi:routes:apiv3:security-setting');
  4. const express = require('express');
  5. const router = express.Router();
  6. const { body } = require('express-validator/check');
  7. const ErrorV3 = require('../../models/vo/error-apiv3');
  8. const validator = {};
  9. /**
  10. * @swagger
  11. * tags:
  12. * name: SecuritySetting
  13. */
  14. module.exports = (crowi) => {
  15. const loginRequiredStrictly = require('../../middleware/login-required')(crowi);
  16. const adminRequired = require('../../middleware/admin-required')(crowi);
  17. const csrf = require('../../middleware/csrf')(crowi);
  18. const { ApiV3FormValidator } = crowi.middlewares;
  19. const validator = {
  20. guestMode: [
  21. body('restrictGuestMode').isString(),
  22. ],
  23. pageDeletion: [
  24. body('pageCompleteDeletionAuthority').isString(),
  25. ],
  26. function: [
  27. body('hideRestrictedByOwner').isBoolean(),
  28. body('hideRestrictedByGroup').isBoolean(),
  29. ],
  30. };
  31. /**
  32. * @swagger
  33. *
  34. * /security-setting/guestMode:
  35. * put:
  36. * tags: [SecuritySetting]
  37. * description: Get restrictGuestMode
  38. * requestBody:
  39. * required: true
  40. * content:
  41. * application/json:
  42. * schama:
  43. * type: object
  44. * properties:
  45. * restrictGuestMode:
  46. * description: type of restrictGuestMode
  47. * type: string
  48. * responses:
  49. * 200:
  50. * description: Succeeded to update restrictGuestMode
  51. */
  52. router.put('/guestMode', loginRequiredStrictly, adminRequired, csrf, validator.guestMode, ApiV3FormValidator, async(req, res) => {
  53. const requestParams = {
  54. 'security:restrictGuestMode': req.body.restrictGuestMode,
  55. };
  56. try {
  57. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  58. const securitySettingParams = {
  59. restrictGuestMode: await crowi.configManager.getConfig('crowi', 'security:restrictGuestMode'),
  60. };
  61. return res.apiv3({ securitySettingParams });
  62. }
  63. catch (err) {
  64. const msg = 'Error occurred in updating layout and theme';
  65. logger.error('Error', err);
  66. return res.apiv3Err(new ErrorV3(msg, 'update-layoutTheme-failed'));
  67. }
  68. });
  69. /**
  70. * @swagger
  71. *
  72. * /security-setting/pageDeletion:
  73. * put:
  74. * tags: [SecuritySetting]
  75. * description: Update pageDeletion Setting
  76. * requestBody:
  77. * required: true
  78. * content:
  79. * application/json:
  80. * schama:
  81. * type: object
  82. * properties:
  83. * pageCompleteDeletionAuthority:
  84. * description: type of pageCompleteDeletionAuthority
  85. * type: string
  86. * responses:
  87. * 200:
  88. * description: Succeeded to update behavior
  89. */
  90. router.put('/pageDeletion', loginRequiredStrictly, adminRequired, csrf, validator.pageDeletion, ApiV3FormValidator, async(req, res) => {
  91. const requestParams = {
  92. 'security:pageCompleteDeletionAuthority': req.body.pageCompleteDeletionAuthority,
  93. };
  94. try {
  95. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  96. const securitySettingParams = {
  97. pageCompleteDeletionAuthority: await crowi.configManager.getConfig('crowi', 'security:pageCompleteDeletionAuthority'),
  98. };
  99. return res.apiv3({ securitySettingParams });
  100. }
  101. catch (err) {
  102. const msg = 'Error occurred in updating page-deletion-setting';
  103. logger.error('Error', err);
  104. return res.apiv3Err(new ErrorV3(msg, 'update-page-deletion-setting-failed'));
  105. }
  106. });
  107. /**
  108. * @swagger
  109. *
  110. * /security-setting/function:
  111. * put:
  112. * tags: [SecuritySetting]
  113. * description: Update function
  114. * requestBody:
  115. * required: true
  116. * content:
  117. * application/json:
  118. * schama:
  119. * type: object
  120. * properties:
  121. * hideRestrictedByOwner:
  122. * description: is enabled hideRestrictedByOwner
  123. * type: boolean
  124. * ihideRestrictedByGroup:
  125. * description: is enabled hideRestrictedBygroup
  126. * type: boolean
  127. * responses:
  128. * 200:
  129. * description: Succeeded to update function
  130. */
  131. router.put('/function', loginRequiredStrictly, adminRequired, csrf, validator.function, ApiV3FormValidator, async(req, res) => {
  132. const requestParams = {
  133. 'security:list-policy:hideRestrictedByOwner': req.body.hideRestrictedByOwner,
  134. 'security:list-policy:hideRestrictedByGroup': req.body.hideRestrictedByGroup,
  135. };
  136. try {
  137. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  138. const securitySettingParams = {
  139. hideRestrictedByOwner: await crowi.configManager.getConfig('crowi', 'security:list-policy:hideRestrictedByOwner'),
  140. hideRestrictedByGroup: await crowi.configManager.getConfig('crowi', 'customize:security:list-policy:hideRestrictedByGroup'),
  141. };
  142. return res.apiv3({ securitySettingParams });
  143. }
  144. catch (err) {
  145. const msg = 'Error occurred in updating function';
  146. logger.error('Error', err);
  147. return res.apiv3Err(new ErrorV3(msg, 'update-function-failed'));
  148. }
  149. });
  150. return router;
  151. };