markdown-setting.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* eslint-disable no-unused-vars */
  2. const loggerFactory = require('@alias/logger');
  3. const logger = loggerFactory('growi:routes:apiv3:user-group');
  4. const express = require('express');
  5. const router = express.Router();
  6. const { body } = require('express-validator/check');
  7. const validator = {};
  8. /**
  9. * @swagger
  10. * tags:
  11. * name: MarkDownSetting
  12. */
  13. module.exports = (crowi) => {
  14. const loginRequiredStrictly = require('../../middleware/login-required')(crowi);
  15. const adminRequired = require('../../middleware/admin-required')(crowi);
  16. const csrf = require('../../middleware/csrf')(crowi);
  17. const {
  18. ErrorV3,
  19. Config,
  20. } = crowi.models;
  21. const { ApiV3FormValidator } = crowi.middlewares;
  22. validator.lineBreak = [
  23. body('isEnabledLinebreaks').isBoolean(),
  24. body('isEnabledLinebreaksInComments').isBoolean(),
  25. ];
  26. /**
  27. * @swagger
  28. *
  29. * paths:
  30. * /_api/v3/markdown-setting/lineBreak:
  31. * put:
  32. * tags: [MarkDownSetting]
  33. * description: Update lineBreak
  34. * parameters:
  35. * - name: isEnabledLinebreaks
  36. * in: query
  37. * description: enable lineBreak
  38. * schema:
  39. * type: boolean
  40. * - name: isEnabledLinebreaksInComments
  41. * in: query
  42. * description: enable lineBreak in comment
  43. * schema:
  44. * type: boolean
  45. * responses:
  46. * 200:
  47. * description: Updating lineBreak success
  48. * content:
  49. * application/json:
  50. * schema:
  51. * properties:
  52. * xssParams:
  53. * type: object
  54. * description: new lineBreak params
  55. */
  56. router.put('/lineBreak', loginRequiredStrictly, adminRequired, csrf, validator.lineBreak, ApiV3FormValidator, async(req, res) => {
  57. const lineBreakParams = {
  58. 'markdown:isEnabledLinebreaks': req.body.isEnabledLinebreaks,
  59. 'markdown:isEnabledLinebreaksInComments': req.body.isEnabledLinebreaksInComments,
  60. };
  61. try {
  62. await crowi.configManager.updateConfigsInTheSameNamespace('markdown', lineBreakParams);
  63. return res.apiv3({ lineBreakParams });
  64. }
  65. catch (err) {
  66. const msg = 'Error occurred in updating lineBreak';
  67. logger.error('Error', err);
  68. return res.apiv3Err(new ErrorV3(msg, 'update-lineBreak-failed'));
  69. }
  70. });
  71. validator.presentationSetting = [
  72. body('pageBreakSeparator').isInt().not().isEmpty(),
  73. ];
  74. /**
  75. * @swagger
  76. *
  77. * paths:
  78. * /_api/v3/markdown-setting/presentation:
  79. * put:
  80. * tags: [Users]
  81. * description: Update presentation
  82. * parameters:
  83. * - name: markdown:presentation:pageBreakSeparator
  84. * in: query
  85. * description: pageBreakSeparator
  86. * schema:
  87. * type: number
  88. * responses:
  89. * 200:
  90. * description: Updating presentation success
  91. * content:
  92. * application/json:
  93. * schema:
  94. * properties:
  95. * presentationParams:
  96. * type: object
  97. * description: new presentation params
  98. */
  99. router.put('/presentation', loginRequiredStrictly, adminRequired, csrf, validator.presentationSetting, ApiV3FormValidator, async(req, res) => {
  100. if (req.body.pageBreakSeparator === 3 && req.body.pageBreakCustomSeparator === '') {
  101. return res.apiv3Err(new ErrorV3('customRegularExpression is required'));
  102. }
  103. const presentationParams = {
  104. 'markdown:presentation:pageBreakSeparator': req.body.pageBreakSeparator,
  105. 'markdown:presentation:pageBreakCustomSeparator': req.body.pageBreakCustomSeparator,
  106. };
  107. try {
  108. await crowi.configManager.updateConfigsInTheSameNamespace('markdown', presentationParams);
  109. return res.apiv3({ presentationParams });
  110. }
  111. catch (err) {
  112. const msg = 'Error occurred in updating presentation';
  113. logger.error('Error', err);
  114. return res.apiv3Err(new ErrorV3(msg, 'update-presentation-failed'));
  115. }
  116. });
  117. validator.xssSetting = [
  118. body('isEnabledXss').isBoolean(),
  119. body('tagWhiteList').isArray(),
  120. body('attrWhiteList').isArray(),
  121. ];
  122. /**
  123. * @swagger
  124. *
  125. * paths:
  126. * /_api/v3/markdown-setting/xss:
  127. * put:
  128. * tags: [MarkDownSetting]
  129. * description: Update xss
  130. * parameters:
  131. * - name: isEnabledPrevention
  132. * in: query
  133. * description: enable xss
  134. * schema:
  135. * type: boolean
  136. * - name: option
  137. * in: query
  138. * description: xss option
  139. * schema:
  140. * type: number
  141. * - name: tagWhiteList
  142. * in: query
  143. * description: custom tag whitelist
  144. * schema:
  145. * type: array
  146. * items:
  147. * type: string
  148. * description: tag whitelist
  149. * - name: attrWhiteList
  150. * in: query
  151. * description: custom attr whitelist
  152. * schema:
  153. * type: array
  154. * items:
  155. * type: string
  156. * description: tag whitelist
  157. * responses:
  158. * 200:
  159. * description: Updating xss success
  160. * content:
  161. * application/json:
  162. * schema:
  163. * properties:
  164. * xssParams:
  165. * type: object
  166. * description: new xss params
  167. */
  168. router.put('/xss', loginRequiredStrictly, adminRequired, csrf, validator.xssSetting, ApiV3FormValidator, async(req, res) => {
  169. if (req.body.isEnabledXss && req.body.xssOption == null) {
  170. return res.apiv3Err(new ErrorV3('xss option is required'));
  171. }
  172. const xssParams = {
  173. 'markdown:xss:isEnabledPrevention': req.body.isEnabledXss,
  174. 'markdown:xss:option': req.body.xssOption,
  175. 'markdown:xss:tagWhiteList': req.body.tagWhiteList,
  176. 'markdown:xss:attrWhiteList': req.body.attrWhiteList,
  177. };
  178. try {
  179. await crowi.configManager.updateConfigsInTheSameNamespace('markdown', xssParams);
  180. return res.apiv3({ xssParams });
  181. }
  182. catch (err) {
  183. const msg = 'Error occurred in updating xss';
  184. logger.error('Error', err);
  185. return res.apiv3Err(new ErrorV3(msg, 'update-xss-failed'));
  186. }
  187. });
  188. return router;
  189. };