Преглед изворни кода

setupConfigFormData => getConfigByPrefix, bug fix

mizozobu пре 6 година
родитељ
комит
d7f82bc30e

+ 5 - 1
src/server/crowi/express-init.js

@@ -28,6 +28,10 @@ module.exports = function(crowi, app) {
   const configManager = crowi.configManager;
   const getConfig = configManager.getConfig;
 
+  // Service classes
+  const { siteUrlService } = crowi;
+
+
   const User = crowi.model('User');
   const lngDetector = new i18nMiddleware.LanguageDetector();
   lngDetector.addDetector(i18nUserSettingDetector);
@@ -68,7 +72,7 @@ module.exports = function(crowi, app) {
     req.csrfToken = null;
 
     res.locals.req = req;
-    res.locals.baseUrl = configManager.getSiteUrl();
+    res.locals.baseUrl = siteUrlService.getSiteUrl();
     // res.locals.config = config;
     res.locals.env = env;
     res.locals.now = now;

+ 5 - 31
src/server/models/config.js

@@ -244,32 +244,6 @@ module.exports = function(crowi) {
   //   });
   // };
 
-  configSchema.statics.setupConfigFormData = function(ns, config) {
-    let defaultConfig = {};
-
-    // set Default Settings
-    if (ns === 'crowi') {
-      defaultConfig = getDefaultCrowiConfigs();
-    }
-    else if (ns === 'markdown') {
-      defaultConfig = getDefaultMarkdownConfigs();
-    }
-    else if (ns === 'notification') {
-      defaultConfig = getDefaultNotificationConfigs();
-    }
-
-    if (!defaultConfig[ns]) {
-      defaultConfig[ns] = {};
-    }
-    Object.keys(config[ns] || {}).forEach((key) => {
-      if (config[ns][key] !== undefined) {
-        defaultConfig[key] = config[ns][key];
-      }
-    });
-    return defaultConfig;
-  };
-
-
   configSchema.statics.updateNamespaceByArray = function(ns, configs, callback) {
     const Config = this;
     if (configs.length < 0) {
@@ -636,7 +610,7 @@ module.exports = function(crowi) {
     const localConfig = {
       crowi: {
         title: Config.appTitle(crowi),
-        url: crowi.configManager.getSiteUrl(),
+        url: crowi.siteUrlService.getSiteUrl(),
       },
       upload: {
         image: crowi.configManager.getIsUploadable(),
@@ -648,11 +622,11 @@ module.exports = function(crowi) {
       isEnabledLinebreaksInComments: crowi.configManager.getConfig('markdown', 'markdown:isEnabledLinebreaksInComments'),
       isEnabledXssPrevention: crowi.configManager.getConfig('markdown', 'markdown:xss:isEnabledPrevention'),
       xssOption: crowi.configManager.getConfig('markdown', 'markdown:xss:option'),
-      tagWhiteList: crowi.configManager.getTagWhiteList(),
-      attrWhiteList: crowi.configManager.getAttrWhiteList(),
+      tagWhiteList: crowi.xssService.getTagWhiteList(),
+      attrWhiteList: crowi.xssService.getAttrWhiteList(),
       highlightJsStyleBorder: crowi.configManager.getConfig('crowi', 'customize:highlightJsStyleBorder'),
       isSavedStatesOfTabChanges: crowi.configManager.getConfig('crowi', 'customize:isSavedStatesOfTabChanges'),
-      hasSlackConfig: crowi.configManager.getConfig('crowi', 'customize:behavior'), // change
+      hasSlackConfig: crowi.configManager.getConfig('crowi', 'customize:behavior'),
       env: {
         PLANTUML_URI: env.PLANTUML_URI || null,
         BLOCKDIAG_URI: env.BLOCKDIAG_URI || null,
@@ -661,7 +635,7 @@ module.exports = function(crowi) {
         NO_CDN: env.NO_CDN || null,
       },
       recentCreatedLimit: crowi.configManager.getConfig('crowi', 'customize:showRecentCreatedNumber'),
-      isAclEnabled: !crowi.configManager.getIsPublicWikiOnly(),
+      isAclEnabled: !crowi.aclService.getIsPublicWikiOnly(),
       globalLang: crowi.configManager.getConfig('crowi', 'app:globalLang'),
     };
 

+ 10 - 8
src/server/routes/admin.js

@@ -14,6 +14,8 @@ module.exports = function(crowi, app) {
   const GlobalNotificationMailSetting = models.GlobalNotificationMailSetting;
   const GlobalNotificationSlackSetting = models.GlobalNotificationSlackSetting; // eslint-disable-line no-unused-vars
 
+  const { configManager } = crowi;
+
   const recommendedWhitelist = require('@commons/service/xss/recommended-whitelist');
   const PluginUtils = require('../plugins/plugin-utils');
   const ApiResponse = require('../util/apiResponse');
@@ -90,7 +92,7 @@ module.exports = function(crowi, app) {
   // app.get('/admin/app'                  , admin.app.index);
   actions.app = {};
   actions.app.index = function(req, res) {
-    const settingForm = Config.setupConfigFormData('crowi', req.config);
+    const settingForm = configManager.getConfigByPrefix('crowi', 'app');
 
     return res.render('admin/app', {
       settingForm,
@@ -103,16 +105,16 @@ module.exports = function(crowi, app) {
   // app.get('/admin/security'                  , admin.security.index);
   actions.security = {};
   actions.security.index = function(req, res) {
-    const settingForm = Config.setupConfigFormData('crowi', req.config);
-    const isAclEnabled = !Config.isPublicWikiOnly(req.config);
+    const settingForm = configManager.getConfigByPrefix('crowi', 'security');
+    const isAclEnabled = crowi.aclService.getIsPublicWikiOnly();
+
     return res.render('admin/security', { settingForm, isAclEnabled });
   };
 
   // app.get('/admin/markdown'                  , admin.markdown.index);
   actions.markdown = {};
   actions.markdown.index = function(req, res) {
-    const config = crowi.getConfig();
-    const markdownSetting = Config.setupConfigFormData('markdown', config);
+    const markdownSetting = configManager.getConfigByPrefix('crowi', 'markdown');
 
     return res.render('admin/markdown', {
       markdownSetting,
@@ -188,7 +190,7 @@ module.exports = function(crowi, app) {
   // app.get('/admin/customize' , admin.customize.index);
   actions.customize = {};
   actions.customize.index = function(req, res) {
-    const settingForm = Config.setupConfigFormData('crowi', req.config);
+    const settingForm = configManager.getConfigByPrefix('crowi', 'customize');
 
     /* eslint-disable quote-props, no-multi-spaces */
     const highlightJsCssSelectorOptions = {
@@ -216,7 +218,7 @@ module.exports = function(crowi, app) {
   actions.notification.index = async(req, res) => {
     const config = crowi.getConfig();
     const UpdatePost = crowi.model('UpdatePost');
-    let slackSetting = Config.setupConfigFormData('notification', config);
+    let slackSetting = configManager.getConfigByPrefix('notification', 'slack');
     const hasSlackIwhUrl = Config.hasSlackIwhUrl(config);
     const hasSlackToken = Config.hasSlackToken(config);
 
@@ -851,7 +853,7 @@ module.exports = function(crowi, app) {
   // Importer management
   actions.importer = {};
   actions.importer.index = function(req, res) {
-    const settingForm = Config.setupConfigFormData('crowi', req.config);
+    const settingForm = configManager.getConfigByPrefix('crowi', 'importer');
 
     return res.render('admin/importer', {
       settingForm,

+ 55 - 3
src/server/service/config-manager.js

@@ -28,7 +28,6 @@ class ConfigManager {
    */
   async loadConfigs() {
     this.configObject = await this.configLoader.load();
-
     debug('ConfigManager#loadConfigs', this.configObject);
   }
 
@@ -52,6 +51,56 @@ class ConfigManager {
     return this.defaultSearch(namespace, key);
   }
 
+  /**
+   * get a config specified by namespace and prefix
+   */
+  getConfigByPrefix(namespace, prefix) {
+    const regexp = new RegExp(`^${prefix}:`);
+    const keys = this.getAllKeys();
+    const result = {};
+
+    for (const key of keys) {
+      if (regexp.test(key)) {
+        result[key] = this.getConfig(namespace, key);
+      }
+    }
+
+    return result;
+  }
+
+  /**
+   * generate an array of config keys from this.configObject
+   */
+  getAllKeys() {
+    // type: fromDB, fromEnvVars
+    const types = Object.keys(this.configObject);
+    let namespaces = [];
+    let keys = [];
+
+    for (const type of types) {
+      if (this.configObject[type] != null) {
+        // ns: crowi, markdown, notification
+        namespaces = [...namespaces, ...Object.keys(this.configObject[type])];
+      }
+    }
+
+    // remove duplicates
+    namespaces = [...new Set(namespaces)];
+
+    for (const type of types) {
+      for (const ns of namespaces) {
+        if (this.configObject[type][ns] != null) {
+          keys = [...keys, ...Object.keys(this.configObject[type][ns])];
+        }
+      }
+    }
+
+    // remove duplicates
+    keys = [...new Set(keys)];
+
+    return keys;
+  }
+
   /**
    * get a config specified by namespace & key from configs loaded from the database
    *
@@ -143,27 +192,30 @@ class ConfigManager {
    * and then from configs loaded from the environment variables
    */
   defaultSearch(namespace, key) {
+    // does not exist neither in db nor in env vars
     if (!this.configExistsInDB(namespace, key) && !this.configExistsInEnvVars(namespace, key)) {
       return undefined;
     }
 
+    // only exists in db
     if (this.configExistsInDB(namespace, key) && !this.configExistsInEnvVars(namespace, key)) {
       return this.configObject.fromDB[namespace][key];
     }
 
+    // only exists env vars
     if (!this.configExistsInDB(namespace, key) && this.configExistsInEnvVars(namespace, key)) {
       return this.configObject.fromEnvVars[namespace][key];
     }
 
+    // exists both in db and in env vars [db > env var]
     if (this.configExistsInDB(namespace, key) && this.configExistsInEnvVars(namespace, key)) {
-      /* eslint-disable no-else-return */
       if (this.configObject.fromDB[namespace][key] !== null) {
         return this.configObject.fromDB[namespace][key];
       }
+      /* eslint-disable-next-line no-else-return */
       else {
         return this.configObject.fromEnvVars[namespace][key];
       }
-      /* eslint-enable no-else-return */
     }
   }
 

+ 1 - 1
src/server/service/site-url.js

@@ -21,7 +21,7 @@ class SiteUrl {
    */
   /* eslint-disable no-else-return */
   getSiteUrl() {
-    const siteUrl = this.getConfig('crowi', 'app:siteUrl');
+    const siteUrl = this.crowi.configManager.getConfig('crowi', 'app:siteUrl');
     if (siteUrl != null) {
       return pathUtils.removeTrailingSlash(siteUrl);
     }