Yuki Takei 6 лет назад
Родитель
Сommit
e3574e63d0

+ 15 - 2
src/server/crowi/index.js

@@ -78,6 +78,8 @@ Crowi.prototype.init = async function() {
   await this.setupModels();
   await this.setupSessionConfig();
   await this.setupConfigManager();
+  await this.setUpApp();
+  await this.setUpXss();
 
   await Promise.all([
     this.scanRuntimeVersions(),
@@ -88,9 +90,8 @@ Crowi.prototype.init = async function() {
     this.setupCsrf(),
     this.setUpGlobalNotification(),
     this.setUpSlacklNotification(),
-    this.setUpXss(),
     this.setUpAcl(),
-    this.setUpApp(),
+    this.setUpCustomize(), // depends on AppService and XssService
     this.setUpRestQiitaAPI(),
   ]);
 };
@@ -473,6 +474,18 @@ Crowi.prototype.setUpAcl = function() {
   }
 };
 
+/**
+ * setup CustomizeService
+ */
+Crowi.prototype.setUpCustomize = function() {
+  const CustomizeService = require('../service/customize');
+  if (this.customizeService == null) {
+    this.customizeService = new CustomizeService(this.configManager, this.appService, this.xssService, this.model('Config'));
+    this.customizeService.initCustomCss();
+    this.customizeService.initCustomTitle();
+  }
+};
+
 /**
  * setup AppService
  */

+ 0 - 44
src/server/models/config.js

@@ -6,7 +6,6 @@
 module.exports = function(crowi) {
   const mongoose = require('mongoose');
   const debug = require('debug')('growi:models:config');
-  const uglifycss = require('uglifycss');
   const recommendedWhitelist = require('@commons/service/xss/recommended-whitelist');
 
   const SECURITY_RESTRICT_GUEST_MODE_DENY = 'Deny';
@@ -430,49 +429,6 @@ module.exports = function(crowi) {
     }
   };
 
-  /**
-   * initialize custom css strings
-   */
-  configSchema.statics.initCustomCss = function(config) {
-    const key = 'customize:css';
-    const rawCss = getValueForCrowiNS(config, key);
-    // uglify and store
-    this._customCss = uglifycss.processString(rawCss);
-  };
-
-  configSchema.statics.customCss = function(config) {
-    return this._customCss;
-  };
-
-  configSchema.statics.initCustomScript = function(config) {
-    const key = 'customize:script';
-    const rawScript = getValueForCrowiNS(config, key);
-    // store as is
-    this._customScript = rawScript;
-  };
-
-  configSchema.statics.customScript = function(config) {
-    return this._customScript;
-  };
-
-  configSchema.statics.customTitle = function(config, page) {
-    validateCrowi();
-
-    const key = 'customize:title';
-    let customTitle = getValueForCrowiNS(config, key);
-
-    if (customTitle == null || customTitle.trim().length === 0) {
-      customTitle = '{{page}} - {{sitename}}';
-    }
-
-    // replace
-    customTitle = customTitle
-      .replace('{{sitename}}', this.appTitle(config))
-      .replace('{{page}}', page);
-
-    return crowi.xss.process(customTitle);
-  };
-
   configSchema.statics.fileUploadEnabled = function(config) {
     const Config = this;
 

+ 61 - 0
src/server/service/customize.js

@@ -0,0 +1,61 @@
+const logger = require('@alias/logger')('growi:service:CustomizeService'); // eslint-disable-line no-unused-vars
+
+/**
+ * the service class of CustomizeService
+ */
+class CustomizeService {
+
+  constructor(configManager, appService, xssService, Config) {
+    this.configManager = configManager;
+    this.appService = appService;
+    this.xssService = xssService;
+
+    // === TODO remove after GC-2004
+    this.Config = Config;
+  }
+
+  /**
+   * initialize custom css strings
+   */
+  initCustomCss() {
+    const uglifycss = require('uglifycss');
+
+    const rawCss = this.configManager.getConfig('crowi', 'customize:css') || '';
+
+    // uglify and store
+    this.customCss = uglifycss.processString(rawCss);
+  }
+
+  getCustomCss() {
+    return this.customCss;
+  }
+
+  getCustomScript() {
+    return this.configManager.getConfig('crowi', 'customize:script') || '';
+  }
+
+  initCustomTitle() {
+    let configValue = this.configManager.getConfig('crowi', 'customize:title');
+
+    if (configValue == null || configValue.trim().length === 0) {
+      configValue = '{{page}} - {{sitename}}';
+    }
+
+    this.customTitleTemplate = configValue;
+  }
+
+  generateCustomTitle(page) {
+    // replace
+    const customTitle = this.customTitleTemplate
+      .replace('{{sitename}}', this.Config.appTitle())
+      // === TODO fix after refactoring AppService (GC-2004)
+      // .replace('{{sitename}}', this.appService.getAppTitle())
+      .replace('{{page}}', page);
+
+    return this.xssService.process(customTitle);
+  }
+
+
+}
+
+module.exports = CustomizeService;

+ 8 - 0
src/server/service/xss.js

@@ -1,4 +1,6 @@
 const logger = require('@alias/logger')('growi:service:XssSerivce'); // eslint-disable-line no-unused-vars
+
+const Xss = require('@commons/service/xss');
 const { tags, attrs } = require('@commons/service/xss/recommended-whitelist');
 
 /**
@@ -8,6 +10,12 @@ class XssSerivce {
 
   constructor(configManager) {
     this.configManager = configManager;
+
+    this.xss = new Xss();
+  }
+
+  process(value) {
+    return this.xss.process(value);
   }
 
   getTagWhiteList() {

+ 9 - 7
src/server/util/swigFunctions.js

@@ -202,10 +202,6 @@ module.exports = function(crowi, app, req, locals) {
     return Config.isEnabledLinebreaksInComments(config);
   };
 
-  locals.customCss = function() {
-    return Config.customCss();
-  };
-
   locals.pageBreakSeparator = function() {
     const config = crowi.getConfig();
     return Config.pageBreakSeparator(config);
@@ -216,8 +212,14 @@ module.exports = function(crowi, app, req, locals) {
     return Config.pageBreakCustomSeparator(config);
   };
 
+  locals.customCss = function() {
+    const customizeService = crowi.customizeService;
+    return customizeService.getCustomCss();
+  };
+
   locals.customScript = function() {
-    return Config.customScript();
+    const customizeService = crowi.customizeService;
+    return customizeService.getCustomScript();
   };
 
   locals.customHeader = function() {
@@ -225,8 +227,8 @@ module.exports = function(crowi, app, req, locals) {
   };
 
   locals.customTitle = function(page) {
-    const config = crowi.getConfig();
-    return Config.customTitle(config, page);
+    const customizeService = crowi.customizeService;
+    return customizeService.generateCustomTitle(page);
   };
 
   locals.behaviorType = function() {