ソースを参照

Merge pull request #1345 from weseek/rewritte-swagger

Rewritte swagger
itizawa 6 年 前
コミット
b67d2b6494
2 ファイル変更141 行追加116 行削除
  1. 141 89
      src/server/routes/apiv3/markdown-setting.js
  2. 0 27
      src/server/views/admin/markdown.html

+ 141 - 89
src/server/routes/apiv3/markdown-setting.js

@@ -11,7 +11,21 @@ const { body } = require('express-validator/check');
 
 
 const ErrorV3 = require('../../models/vo/error-apiv3');
 const ErrorV3 = require('../../models/vo/error-apiv3');
 
 
-const validator = {};
+const validator = {
+  lineBreak: [
+    body('isEnabledLinebreaks').isBoolean(),
+    body('isEnabledLinebreaksInComments').isBoolean(),
+  ],
+  presentationSetting: [
+    body('pageBreakSeparator').isInt().not().isEmpty(),
+  ],
+  xssSetting: [
+    body('isEnabledXss').isBoolean(),
+    body('tagWhiteList').isArray(),
+    body('attrWhiteList').isArray(),
+  ],
+};
+
 
 
 /**
 /**
  * @swagger
  * @swagger
@@ -19,62 +33,103 @@ const validator = {};
  *    name: MarkDownSetting
  *    name: MarkDownSetting
  */
  */
 
 
+/**
+ * @swagger
+ *
+ *  components:
+ *    schemas:
+ *      LineBreakParams:
+ *        type: object
+ *        properties:
+ *          isEnabledLinebreaks:
+ *            type: boolean
+ *            description: enable lineBreak
+ *          isEnabledLinebreaksInComments:
+ *            type: boolean
+ *            description: enable lineBreak in comment
+ *      PresentationParams:
+ *        type: object
+ *        properties:
+ *          pageBreakSeparator:
+ *            type: number
+ *            description: number of pageBreakSeparator
+ *          pageBreakCustomSeparator:
+ *            type: string
+ *            description: string of pageBreakCustomSeparator
+ *      XssParams:
+ *        type: object
+ *        properties:
+ *          isEnabledPrevention:
+ *            type: boolean
+ *            description: enable xss
+ *          xssOption:
+ *            type: number
+ *            description: number of xss option
+ *          tagWhiteList:
+ *            type: array
+ *            description: array of tag whiteList
+ *            items:
+ *              type: string
+ *              description: tag whitelist
+ *          attrWhiteList:
+ *            type: array
+ *            description: array of attr whiteList
+ *            items:
+ *              type: string
+ *              description: attr whitelist
+ */
+
 module.exports = (crowi) => {
 module.exports = (crowi) => {
   const loginRequiredStrictly = require('../../middleware/login-required')(crowi);
   const loginRequiredStrictly = require('../../middleware/login-required')(crowi);
   const adminRequired = require('../../middleware/admin-required')(crowi);
   const adminRequired = require('../../middleware/admin-required')(crowi);
   const csrf = require('../../middleware/csrf')(crowi);
   const csrf = require('../../middleware/csrf')(crowi);
 
 
-  // const {
-  //   Config,
-  // } = crowi.models;
-
   const { ApiV3FormValidator } = crowi.middlewares;
   const { ApiV3FormValidator } = crowi.middlewares;
 
 
-  validator.lineBreak = [
-    body('isEnabledLinebreaks').isBoolean(),
-    body('isEnabledLinebreaksInComments').isBoolean(),
-  ];
-
   /**
   /**
    * @swagger
    * @swagger
    *
    *
-   *  paths:
-   *    /_api/v3/markdown-setting/lineBreak:
+   *    /markdown-setting/lineBreak:
    *      put:
    *      put:
    *        tags: [MarkDownSetting]
    *        tags: [MarkDownSetting]
-   *        description: Update lineBreak
-   *        parameters:
-   *          - name: isEnabledLinebreaks
-   *            in: query
-   *            description: enable lineBreak
-   *            schema:
-   *              type: boolean
-   *          - name: isEnabledLinebreaksInComments
-   *            in: query
-   *            description: enable lineBreak in comment
-   *            schema:
-   *              type: boolean
+   *        description: Update lineBreak setting
+   *        requestBody:
+   *          required: true
+   *          content:
+   *            application/json:
+   *              schema:
+   *                type: object
+   *                properties:
+   *                  isEnabledLinebreaks:
+   *                    description: enable lineBreak
+   *                    type: boolean
+   *                  isEnabledLinebreaksInComments:
+   *                    description: enable lineBreak in comment
+   *                    type: boolean
    *        responses:
    *        responses:
    *          200:
    *          200:
-   *            description: Updating lineBreak success
+   *            description: Succeeded to update lineBreak setting
    *            content:
    *            content:
    *              application/json:
    *              application/json:
    *                schema:
    *                schema:
    *                  properties:
    *                  properties:
-   *                    xssParams:
-   *                      type: object
-   *                      description: new lineBreak params
+   *                    status:
+   *                      $ref: '#/components/schemas/lineBreakParams'
    */
    */
   router.put('/lineBreak', loginRequiredStrictly, adminRequired, csrf, validator.lineBreak, ApiV3FormValidator, async(req, res) => {
   router.put('/lineBreak', loginRequiredStrictly, adminRequired, csrf, validator.lineBreak, ApiV3FormValidator, async(req, res) => {
 
 
-    const lineBreakParams = {
+    const requestLineBreakParams = {
       'markdown:isEnabledLinebreaks': req.body.isEnabledLinebreaks,
       'markdown:isEnabledLinebreaks': req.body.isEnabledLinebreaks,
       'markdown:isEnabledLinebreaksInComments': req.body.isEnabledLinebreaksInComments,
       'markdown:isEnabledLinebreaksInComments': req.body.isEnabledLinebreaksInComments,
     };
     };
 
 
     try {
     try {
-      await crowi.configManager.updateConfigsInTheSameNamespace('markdown', lineBreakParams);
-      return res.apiv3({ lineBreakParams });
+      await crowi.configManager.updateConfigsInTheSameNamespace('markdown', requestLineBreakParams);
+      const lineBreaksParams = {
+        isEnabledLinebreaks: await crowi.configManager.getConfig('markdown', 'markdown:isEnabledLinebreaks'),
+        isEnabledLinebreaksInComments: await crowi.configManager.getConfig('markdown', 'markdown:isEnabledLinebreaksInComments') || '',
+      };
+      return res.apiv3({ lineBreaksParams });
     }
     }
     catch (err) {
     catch (err) {
       const msg = 'Error occurred in updating lineBreak';
       const msg = 'Error occurred in updating lineBreak';
@@ -84,34 +139,35 @@ module.exports = (crowi) => {
 
 
   });
   });
 
 
-  validator.presentationSetting = [
-    body('pageBreakSeparator').isInt().not().isEmpty(),
-  ];
-
   /**
   /**
    * @swagger
    * @swagger
    *
    *
-   *  paths:
-   *    /_api/v3/markdown-setting/presentation:
+   *    /markdown-setting/presentation:
    *      put:
    *      put:
-   *        tags: [Users]
+   *        tags: [MarkDownSetting]
    *        description: Update presentation
    *        description: Update presentation
-   *        parameters:
-   *          - name: markdown:presentation:pageBreakSeparator
-   *            in: query
-   *            description: pageBreakSeparator
-   *            schema:
-   *              type: number
+   *        requestBody:
+   *          required: true
+   *          content:
+   *            application/json:
+   *              schema:
+   *                type: object
+   *                properties:
+   *                  pageBreakSeparator:
+   *                    description: number of pageBreakSeparator
+   *                    type: number
+   *                  pageBreakCustomSeparator:
+   *                    description: string of pageBreakCustomSeparator
+   *                    type: string
    *        responses:
    *        responses:
    *          200:
    *          200:
-   *            description: Updating presentation success
+   *            description: Succeeded to update presentation setting
    *            content:
    *            content:
    *              application/json:
    *              application/json:
    *                schema:
    *                schema:
    *                  properties:
    *                  properties:
-   *                    presentationParams:
-   *                      type: object
-   *                      description: new presentation params
+   *                    status:
+   *                      $ref: '#/components/schemas/presentationParams'
    */
    */
   router.put('/presentation', loginRequiredStrictly, adminRequired, csrf, validator.presentationSetting, ApiV3FormValidator, async(req, res) => {
   router.put('/presentation', loginRequiredStrictly, adminRequired, csrf, validator.presentationSetting, ApiV3FormValidator, async(req, res) => {
     if (req.body.pageBreakSeparator === 3 && req.body.pageBreakCustomSeparator === '') {
     if (req.body.pageBreakSeparator === 3 && req.body.pageBreakCustomSeparator === '') {
@@ -139,64 +195,54 @@ module.exports = (crowi) => {
 
 
   });
   });
 
 
-  validator.xssSetting = [
-    body('isEnabledXss').isBoolean(),
-    body('tagWhiteList').isArray(),
-    body('attrWhiteList').isArray(),
-  ];
-
   /**
   /**
    * @swagger
    * @swagger
    *
    *
-   *  paths:
-   *    /_api/v3/markdown-setting/xss:
+   *    /markdown-setting/xss:
    *      put:
    *      put:
    *        tags: [MarkDownSetting]
    *        tags: [MarkDownSetting]
    *        description: Update xss
    *        description: Update xss
-   *        parameters:
-   *          - name: isEnabledPrevention
-   *            in: query
-   *            description: enable xss
-   *            schema:
-   *              type: boolean
-   *          - name: option
-   *            in: query
-   *            description: xss option
-   *            schema:
-   *              type: number
-   *          - name: tagWhiteList
-   *            in: query
-   *            description: custom tag whitelist
-   *            schema:
-   *              type: array
-   *              items:
-   *                type: string
-   *                description: tag whitelist
-   *          - name: attrWhiteList
-   *            in: query
-   *            description: custom attr whitelist
-   *            schema:
-   *              type: array
-   *              items:
-   *                type: string
-   *                description: tag whitelist
+   *        requestBody:
+   *          required: true
+   *          content:
+   *            application/json:
+   *              schema:
+   *                type: object
+   *                properties:
+   *                  isEnabledPrevention:
+   *                    description: enable xss
+   *                    type: boolean
+   *                  xssOption:
+   *                    description: number of xss option
+   *                    type: number
+   *                  tagWhiteList:
+   *                    description: array of tag whiteList
+   *                    type: array
+   *                    items:
+   *                      type: string
+   *                      description: tag whitelist
+   *                  attrWhiteList:
+   *                    description: array of attr whiteList
+   *                    type: array
+   *                    items:
+   *                      type: string
+   *                      description: attr whitelist
    *        responses:
    *        responses:
    *          200:
    *          200:
-   *            description: Updating xss success
+   *            description: Succeeded to update xss setting
    *            content:
    *            content:
    *              application/json:
    *              application/json:
    *                schema:
    *                schema:
    *                  properties:
    *                  properties:
-   *                    xssParams:
-   *                      type: object
-   *                      description: new xss params
+   *                    status:
+   *                      $ref: '#/components/schemas/xssParams'
    */
    */
   router.put('/xss', loginRequiredStrictly, adminRequired, csrf, validator.xssSetting, ApiV3FormValidator, async(req, res) => {
   router.put('/xss', loginRequiredStrictly, adminRequired, csrf, validator.xssSetting, ApiV3FormValidator, async(req, res) => {
     if (req.body.isEnabledXss && req.body.xssOption == null) {
     if (req.body.isEnabledXss && req.body.xssOption == null) {
       return res.apiv3Err(new ErrorV3('xss option is required'));
       return res.apiv3Err(new ErrorV3('xss option is required'));
     }
     }
 
 
-    const xssParams = {
+    const reqestXssParams = {
       'markdown:xss:isEnabledPrevention': req.body.isEnabledXss,
       'markdown:xss:isEnabledPrevention': req.body.isEnabledXss,
       'markdown:xss:option': req.body.xssOption,
       'markdown:xss:option': req.body.xssOption,
       'markdown:xss:tagWhiteList': req.body.tagWhiteList,
       'markdown:xss:tagWhiteList': req.body.tagWhiteList,
@@ -204,7 +250,13 @@ module.exports = (crowi) => {
     };
     };
 
 
     try {
     try {
-      await crowi.configManager.updateConfigsInTheSameNamespace('markdown', xssParams);
+      await crowi.configManager.updateConfigsInTheSameNamespace('markdown', reqestXssParams);
+      const xssParams = {
+        isEnabledXss: await crowi.configManager.getConfig('markdown', 'markdown:xss:isEnabledPrevention'),
+        xssOption: await crowi.configManager.getConfig('markdown', 'markdown:xss:option'),
+        tagWhiteList: await crowi.configManager.getConfig('markdown', 'markdown:xss:tagWhiteList'),
+        attrWhiteList: await crowi.configManager.getConfig('markdown', 'markdown:xss:attrWhiteList'),
+      };
       return res.apiv3({ xssParams });
       return res.apiv3({ xssParams });
     }
     }
     catch (err) {
     catch (err) {

+ 0 - 27
src/server/views/admin/markdown.html

@@ -22,33 +22,6 @@
   </div>
   </div>
 
 
 </div>
 </div>
-
-<script>
-  // give a space between items in textarea(',' => ', ')
-  for (var i = 0; i < $('textarea.xss-list').length; i++) {
-    $($('textarea.xss-list')[i]).val($($('textarea.xss-list')[i]).val().replace(/,/g, ', '));
-  };
-
-  $('input[name="markdownSetting[markdown:xss:isEnabledPrevention]"]').change(function() {
-    if ($(this).val() === 'true') {
-      $('#xss-hide-when-disabled').slideDown();
-    }
-    else {
-      $('#xss-hide-when-disabled').slideUp();
-    }
-  });
-
-  $('#btn-import-tags').on('click', () => {
-    var $tagWhiteList = $('textarea[name="markdownSetting[markdown:xss:tagWhiteList]"]');
-    var $recommendedTagList = $('textarea[name="recommendedTags"]');
-    $tagWhiteList.val($recommendedTagList.val());
-  });
-  $('#btn-import-attrs').on('click', () => {
-    var $attrWhiteList = $('textarea[name="markdownSetting[markdown:xss:attrWhiteList]"]');
-    var $recommendedAttrList = $('textarea[name="recommendedAttrs"]');
-    $attrWhiteList.val($recommendedAttrList.val());
-  });
-</script>
 {% endblock content_main %}
 {% endblock content_main %}
 
 
 {% block content_footer %}
 {% block content_footer %}