customize-setting.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. validator.layoutTheme = [
  20. body('layoutType').isString(),
  21. body('themeType').isString(),
  22. ];
  23. /**
  24. * @swagger
  25. *
  26. * /customize-setting/layoutTheme:
  27. * put:
  28. * tags: [CustomizeSetting]
  29. * description: Update layout and theme
  30. * requestBody:
  31. * required: true
  32. * content:
  33. * application/json:
  34. * schama:
  35. * type: object
  36. * properties:
  37. * layoutType:
  38. * description: type of layout
  39. * type: string
  40. * themeType:
  41. * description: type of theme
  42. * type: string
  43. * responses:
  44. * 200:
  45. * description: Succeeded to update layout and theme
  46. */
  47. router.put('/layoutTheme', loginRequiredStrictly, adminRequired, csrf, validator.layoutTheme, ApiV3FormValidator, async(req, res) => {
  48. const requestParams = {
  49. 'customize:layout': req.body.layoutType,
  50. 'customize:theme': req.body.themeType,
  51. };
  52. try {
  53. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  54. const customizedParams = {
  55. layoutType: await crowi.configManager.getConfig('crowi', 'customize:layout'),
  56. themeType: await crowi.configManager.getConfig('crowi', 'customize:theme'),
  57. };
  58. return res.apiv3({ customizedParams });
  59. }
  60. catch (err) {
  61. const msg = 'Error occurred in updating layout and theme';
  62. logger.error('Error', err);
  63. return res.apiv3Err(new ErrorV3(msg, 'update-layoutTheme-failed'));
  64. }
  65. });
  66. validator.behavior = [
  67. body('behaviorType').isString(),
  68. ];
  69. /**
  70. * @swagger
  71. *
  72. * /customize-setting/behavior:
  73. * put:
  74. * tags: [CustomizeSetting]
  75. * description: Update behavior
  76. * requestBody:
  77. * required: true
  78. * content:
  79. * application/json:
  80. * schama:
  81. * type: object
  82. * properties:
  83. * behaviorType:
  84. * description: type of behavior
  85. * type: string
  86. * responses:
  87. * 200:
  88. * description: Succeeded to update behavior
  89. */
  90. router.put('/behavior', loginRequiredStrictly, adminRequired, csrf, validator.behavior, ApiV3FormValidator, async(req, res) => {
  91. const requestParams = {
  92. 'customize:behavior': req.body.behaviorType,
  93. };
  94. try {
  95. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  96. const customizedParams = {
  97. behaviorType: await crowi.configManager.getConfig('crowi', 'customize:behavior'),
  98. };
  99. return res.apiv3({ customizedParams });
  100. }
  101. catch (err) {
  102. const msg = 'Error occurred in updating behavior';
  103. logger.error('Error', err);
  104. return res.apiv3Err(new ErrorV3(msg, 'update-behavior-failed'));
  105. }
  106. });
  107. return router;
  108. };