security-setting.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. };
  24. /**
  25. * @swagger
  26. *
  27. * /security-setting/guestMode:
  28. * put:
  29. * tags: [SecuritySetting]
  30. * description: Get restrictGuestMode
  31. * requestBody:
  32. * required: true
  33. * content:
  34. * application/json:
  35. * schama:
  36. * type: object
  37. * properties:
  38. * restructGuestMode:
  39. * description: type of restrutGuestMode
  40. * type: string
  41. * responses:
  42. * 200:
  43. * description: Succeeded to update layout and theme
  44. */
  45. router.put('guestMode', loginRequiredStrictly, adminRequired, csrf, validator.guestMode, ApiV3FormValidator, async(req, res) => {
  46. const requestParams = {
  47. 'customize:layout': req.body.layoutType,
  48. 'customize:theme': req.body.themeType,
  49. };
  50. try {
  51. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  52. const customizedParams = {
  53. layoutType: await crowi.configManager.getConfig('crowi', 'customize:layout'),
  54. themeType: await crowi.configManager.getConfig('crowi', 'customize:theme'),
  55. };
  56. return res.apiv3({ customizedParams });
  57. }
  58. catch (err) {
  59. const msg = 'Error occurred in updating layout and theme';
  60. logger.error('Error', err);
  61. return res.apiv3Err(new ErrorV3(msg, 'update-layoutTheme-failed'));
  62. }
  63. });
  64. /**
  65. * @swagger
  66. *
  67. * /customize-setting/behavior:
  68. * put:
  69. * tags: [CustomizeSetting]
  70. * description: Update behavior
  71. * requestBody:
  72. * required: true
  73. * content:
  74. * application/json:
  75. * schama:
  76. * type: object
  77. * properties:
  78. * behaviorType:
  79. * description: type of behavior
  80. * type: string
  81. * responses:
  82. * 200:
  83. * description: Succeeded to update behavior
  84. */
  85. router.put('/behavior', loginRequiredStrictly, adminRequired, csrf, validator.behavior, ApiV3FormValidator, async(req, res) => {
  86. const requestParams = {
  87. 'customize:behavior': req.body.behaviorType,
  88. };
  89. try {
  90. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  91. const customizedParams = {
  92. behaviorType: await crowi.configManager.getConfig('crowi', 'customize:behavior'),
  93. };
  94. return res.apiv3({ customizedParams });
  95. }
  96. catch (err) {
  97. const msg = 'Error occurred in updating behavior';
  98. logger.error('Error', err);
  99. return res.apiv3Err(new ErrorV3(msg, 'update-behavior-failed'));
  100. }
  101. });
  102. /**
  103. * @swagger
  104. *
  105. * /customize-setting/function:
  106. * put:
  107. * tags: [CustomizeSetting]
  108. * description: Update function
  109. * requestBody:
  110. * required: true
  111. * content:
  112. * application/json:
  113. * schama:
  114. * type: object
  115. * properties:
  116. * isEnabledTimeline:
  117. * description: is enabled timeline
  118. * type: boolean
  119. * isSavedStatesOfTabChanges:
  120. * description: is saved states of tabChanges
  121. * type: boolean
  122. * isEnabledAttachTitleHeader:
  123. * description: is enabled attach titleHeader
  124. * type: boolean
  125. * recentCreatedLimit:
  126. * description: limit of recent created
  127. * type: number
  128. * responses:
  129. * 200:
  130. * description: Succeeded to update function
  131. */
  132. router.put('/function', loginRequiredStrictly, adminRequired, csrf, validator.function, ApiV3FormValidator, async(req, res) => {
  133. const requestParams = {
  134. 'customize:isEnabledTimeline': req.body.isEnabledTimeline,
  135. 'customize:isSavedStatesOfTabChanges': req.body.isSavedStatesOfTabChanges,
  136. 'customize:isEnabledAttachTitleHeader': req.body.isEnabledAttachTitleHeader,
  137. 'customize:showRecentCreatedNumber': req.body.recentCreatedLimit,
  138. };
  139. try {
  140. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  141. const customizedParams = {
  142. isEnabledTimeline: await crowi.configManager.getConfig('crowi', 'customize:isEnabledTimeline'),
  143. isSavedStatesOfTabChanges: await crowi.configManager.getConfig('crowi', 'customize:isSavedStatesOfTabChanges'),
  144. isEnabledAttachTitleHeader: await crowi.configManager.getConfig('crowi', 'customize:isEnabledAttachTitleHeader'),
  145. recentCreatedLimit: await crowi.configManager.getConfig('crowi', 'customize:showRecentCreatedNumber'),
  146. };
  147. return res.apiv3({ customizedParams });
  148. }
  149. catch (err) {
  150. const msg = 'Error occurred in updating function';
  151. logger.error('Error', err);
  152. return res.apiv3Err(new ErrorV3(msg, 'update-function-failed'));
  153. }
  154. });
  155. return router;
  156. };