customize-setting.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* eslint-disable no-unused-vars */
  2. const loggerFactory = require('@alias/logger');
  3. const logger = loggerFactory('growi:routes:apiv3:customize-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: CustomizeSetting
  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. // TODO GW-533 implement accurate validation
  20. const validator = {
  21. layoutTheme: [
  22. body('layoutType').isString(),
  23. body('themeType').isString(),
  24. ],
  25. behavior: [
  26. body('behaviorType').isString(),
  27. ],
  28. function: [
  29. body('isEnabledTimeline').isBoolean(),
  30. body('isSavedStatesOfTabChanges').isBoolean(),
  31. body('isEnabledAttachTitleHeader').isBoolean(),
  32. body('recentCreatedLimit').isInt(),
  33. ],
  34. customizeCss: [
  35. body('customizeCss').isString(),
  36. ],
  37. };
  38. /**
  39. * @swagger
  40. *
  41. * /customize-setting/layoutTheme:
  42. * put:
  43. * tags: [CustomizeSetting]
  44. * description: Update layout and theme
  45. * requestBody:
  46. * required: true
  47. * content:
  48. * application/json:
  49. * schama:
  50. * type: object
  51. * properties:
  52. * layoutType:
  53. * description: type of layout
  54. * type: string
  55. * themeType:
  56. * description: type of theme
  57. * type: string
  58. * responses:
  59. * 200:
  60. * description: Succeeded to update layout and theme
  61. */
  62. router.put('/layoutTheme', loginRequiredStrictly, adminRequired, csrf, validator.layoutTheme, ApiV3FormValidator, async(req, res) => {
  63. const requestParams = {
  64. 'customize:layout': req.body.layoutType,
  65. 'customize:theme': req.body.themeType,
  66. };
  67. try {
  68. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  69. const customizedParams = {
  70. layoutType: await crowi.configManager.getConfig('crowi', 'customize:layout'),
  71. themeType: await crowi.configManager.getConfig('crowi', 'customize:theme'),
  72. };
  73. return res.apiv3({ customizedParams });
  74. }
  75. catch (err) {
  76. const msg = 'Error occurred in updating layout and theme';
  77. logger.error('Error', err);
  78. return res.apiv3Err(new ErrorV3(msg, 'update-layoutTheme-failed'));
  79. }
  80. });
  81. /**
  82. * @swagger
  83. *
  84. * /customize-setting/behavior:
  85. * put:
  86. * tags: [CustomizeSetting]
  87. * description: Update behavior
  88. * requestBody:
  89. * required: true
  90. * content:
  91. * application/json:
  92. * schama:
  93. * type: object
  94. * properties:
  95. * behaviorType:
  96. * description: type of behavior
  97. * type: string
  98. * responses:
  99. * 200:
  100. * description: Succeeded to update behavior
  101. */
  102. router.put('/behavior', loginRequiredStrictly, adminRequired, csrf, validator.behavior, ApiV3FormValidator, async(req, res) => {
  103. const requestParams = {
  104. 'customize:behavior': req.body.behaviorType,
  105. };
  106. try {
  107. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  108. const customizedParams = {
  109. behaviorType: await crowi.configManager.getConfig('crowi', 'customize:behavior'),
  110. };
  111. return res.apiv3({ customizedParams });
  112. }
  113. catch (err) {
  114. const msg = 'Error occurred in updating behavior';
  115. logger.error('Error', err);
  116. return res.apiv3Err(new ErrorV3(msg, 'update-behavior-failed'));
  117. }
  118. });
  119. /**
  120. * @swagger
  121. *
  122. * /customize-setting/function:
  123. * put:
  124. * tags: [CustomizeSetting]
  125. * description: Update function
  126. * requestBody:
  127. * required: true
  128. * content:
  129. * application/json:
  130. * schama:
  131. * type: object
  132. * properties:
  133. * isEnabledTimeline:
  134. * description: is enabled timeline
  135. * type: boolean
  136. * isSavedStatesOfTabChanges:
  137. * description: is saved states of tabChanges
  138. * type: boolean
  139. * isEnabledAttachTitleHeader:
  140. * description: is enabled attach titleHeader
  141. * type: boolean
  142. * recentCreatedLimit:
  143. * description: limit of recent created
  144. * type: number
  145. * responses:
  146. * 200:
  147. * description: Succeeded to update function
  148. */
  149. router.put('/function', loginRequiredStrictly, adminRequired, csrf, validator.function, ApiV3FormValidator, async(req, res) => {
  150. const requestParams = {
  151. 'customize:isEnabledTimeline': req.body.isEnabledTimeline,
  152. 'customize:isSavedStatesOfTabChanges': req.body.isSavedStatesOfTabChanges,
  153. 'customize:isEnabledAttachTitleHeader': req.body.isEnabledAttachTitleHeader,
  154. 'customize:showRecentCreatedNumber': req.body.recentCreatedLimit,
  155. };
  156. try {
  157. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  158. const customizedParams = {
  159. isEnabledTimeline: await crowi.configManager.getConfig('crowi', 'customize:isEnabledTimeline'),
  160. isSavedStatesOfTabChanges: await crowi.configManager.getConfig('crowi', 'customize:isSavedStatesOfTabChanges'),
  161. isEnabledAttachTitleHeader: await crowi.configManager.getConfig('crowi', 'customize:isEnabledAttachTitleHeader'),
  162. recentCreatedLimit: await crowi.configManager.getConfig('crowi', 'customize:showRecentCreatedNumber'),
  163. };
  164. return res.apiv3({ customizedParams });
  165. }
  166. catch (err) {
  167. const msg = 'Error occurred in updating function';
  168. logger.error('Error', err);
  169. return res.apiv3Err(new ErrorV3(msg, 'update-function-failed'));
  170. }
  171. });
  172. /**
  173. * @swagger
  174. *
  175. * /customize-setting/customizeCss:
  176. * put:
  177. * tags: [CustomizeSetting]
  178. * description: Update customizeCss
  179. * requestBody:
  180. * required: true
  181. * content:
  182. * application/json:
  183. * schama:
  184. * type: object
  185. * properties:
  186. * customizeCss:
  187. * description: customize css
  188. * type: string
  189. * responses:
  190. * 200:
  191. * description: Succeeded to update customize css
  192. */
  193. router.put('/customize-css', loginRequiredStrictly, adminRequired, csrf, validator.customizeCss, ApiV3FormValidator, async(req, res) => {
  194. const requestParams = {
  195. 'customize:css': req.body.customizeCss,
  196. };
  197. try {
  198. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  199. const customizedParams = {
  200. customizeCss: await crowi.configManager.getConfig('crowi', 'customize:css'),
  201. };
  202. return res.apiv3({ customizedParams });
  203. }
  204. catch (err) {
  205. const msg = 'Error occurred in updating customizeCss';
  206. logger.error('Error', err);
  207. return res.apiv3Err(new ErrorV3(msg, 'update-customizeCss-failed'));
  208. }
  209. });
  210. return router;
  211. };