Просмотр исходного кода

Merge pull request #7646 from weseek/feat/use-only-env-vars-for-app-site-url

feat: Fix APP_SITE_URL with an environment variable
Yuki Takei 2 лет назад
Родитель
Сommit
0be1f0f890

+ 2 - 1
apps/app/public/static/locales/en_US/admin.json

@@ -338,7 +338,8 @@
       "title": "Site URL settings",
       "desc": "This is for the site URL setting.",
       "warn": "Some features don't work because the site URL is not set.",
-      "help": "Site full URL beginning from <code>http://</code> or <code>https://</code>."
+      "help": "Site full URL beginning from <code>http://</code> or <code>https://</code>.",
+      "note_for_the_only_env_option": "The Site URL is fixed to the value of the environment variable.<br>To change this setting, please change to false or delete the value of the environment variable <code>{{env}}</code> ."
     },
     "confidential_name": "Confidential name",
     "confidential_example": "ex): internal use only",

+ 2 - 1
apps/app/public/static/locales/ja_JP/admin.json

@@ -346,7 +346,8 @@
       "title": "サイトURL設定",
       "desc": "サイトURLを設定します。",
       "warn": "サイトURLが設定されていないため、一部機能が動作しない状態になっています。",
-      "help": "<code>http://</code> または <code>https://</code> から始まるサイトのURL"
+      "help": "<code>http://</code> または <code>https://</code> から始まるサイトのURL",
+      "note_for_the_only_env_option": "現在サイトURLは環境変数の値によって固定されています<br>この設定を変更する場合は環境変数 <code>{{env}}</code> の値をfalseに変更もしくは削除してください"
     },
     "confidential_name": "コンフィデンシャル表示",
     "confidential_example": "例: 社外秘",

+ 2 - 1
apps/app/public/static/locales/zh_CN/admin.json

@@ -346,7 +346,8 @@
       "title": "主页URL设置",
       "desc": "用于网站URL设置。",
       "warn": "某些功能不起作用,因为未设置网站URL。",
-      "help": "网站完整URL起始于 <code>http://</code> or <code>https://</code>."
+      "help": "网站完整URL起始于 <code>http://</code> or <code>https://</code>.",
+      "note_for_the_only_env_option": "站点 URL 固定为环境变量的值。<br>要更改此设置,请更改为 false 或删除环境变量 <code>{{env}}</code> 的值。"
     },
     "confidential_name": "内部名称",
     "confidential_example": "ex):仅供内部使用",

+ 2 - 0
apps/app/src/client/services/AdminAppContainer.js

@@ -26,6 +26,7 @@ export default class AdminAppContainer extends Container {
 
       isV5Compatible: null,
       siteUrl: '',
+      siteUrlUseOnlyEnvVars: null,
       envSiteUrl: '',
       isSetSiteUrl: true,
       isMailerSetup: false,
@@ -88,6 +89,7 @@ export default class AdminAppContainer extends Container {
       fileUpload: appSettingsParams.fileUpload,
       isV5Compatible: appSettingsParams.isV5Compatible,
       siteUrl: appSettingsParams.siteUrl,
+      siteUrlUseOnlyEnvVars: appSettingsParams.siteUrlUseOnlyEnvVars,
       envSiteUrl: appSettingsParams.envSiteUrl,
       isSetSiteUrl: !!appSettingsParams.siteUrl,
       isMailerSetup: appSettingsParams.isMailerSetup,

+ 15 - 0
apps/app/src/components/Admin/App/SiteUrlSetting.tsx

@@ -39,6 +39,20 @@ const SiteUrlSetting = (props: Props) => {
       {!adminAppContainer.state.isSetSiteUrl
           && (<p className="alert alert-danger"><i className="icon-exclamation"></i> {t('site_url.warn')}</p>)}
 
+      { adminAppContainer.state.siteUrlUseOnlyEnvVars && (
+        <div className="row">
+          <div className="col-md-9 offset-md-3">
+            <p
+              className="alert alert-info"
+              // eslint-disable-next-line react/no-danger
+              dangerouslySetInnerHTML={{
+                __html: t('site_url.note_for_the_only_env_option', { env: 'APP_SITE_URL_USES_ONLY_ENV_VARS' }),
+              }}
+            />
+          </div>
+        </div>
+      ) }
+
       <div className="row form-group">
         <div className="col-md-9 offset-md-3">
           <table className="table settings-table">
@@ -60,6 +74,7 @@ const SiteUrlSetting = (props: Props) => {
                     type="text"
                     name="settingForm[app:siteUrl]"
                     defaultValue={adminAppContainer.state.siteUrl || ''}
+                    disabled={adminAppContainer.state.siteUrlUseOnlyEnvVars ?? true}
                     onChange={(e) => { adminAppContainer.changeSiteUrl(e.target.value) }}
                     placeholder="e.g. https://my.growi.org"
                   />

+ 8 - 0
apps/app/src/server/routes/apiv3/app-settings.js

@@ -237,6 +237,7 @@ module.exports = (crowi) => {
       fileUpload: crowi.configManager.getConfig('crowi', 'app:fileUpload'),
       isV5Compatible: crowi.configManager.getConfig('crowi', 'app:isV5Compatible'),
       siteUrl: crowi.configManager.getConfig('crowi', 'app:siteUrl'),
+      siteUrlUseOnlyEnvVars: crowi.configManager.getConfig('crowi', 'app:siteUrl:useOnlyEnvVars'),
       envSiteUrl: crowi.configManager.getConfigFromEnvVars('crowi', 'app:siteUrl'),
       isMailerSetup: crowi.mailService.isMailerSetup,
       fromAddress: crowi.configManager.getConfig('crowi', 'mail:from'),
@@ -361,6 +362,13 @@ module.exports = (crowi) => {
    */
   router.put('/site-url-setting', loginRequiredStrictly, adminRequired, addActivity, validator.siteUrlSetting, apiV3FormValidator, async(req, res) => {
 
+    const useOnlyEnvVars = crowi.configManager.getConfig('crowi', 'app:siteUrl:useOnlyEnvVars');
+
+    if (useOnlyEnvVars) {
+      const msg = 'Updating the Site URL is prohibited on this system.';
+      return res.apiv3Err(new ErrorV3(msg, 'update-siteUrlSetting-prohibited'));
+    }
+
     const requestSiteUrlSettingParams = {
       'app:siteUrl': pathUtils.removeTrailingSlash(req.body.siteUrl),
     };

+ 6 - 0
apps/app/src/server/service/config-loader.ts

@@ -139,6 +139,12 @@ const ENV_VAR_NAME_TO_CONFIG_INFO = {
     type:    ValueType.STRING,
     default: null,
   },
+  APP_SITE_URL_USES_ONLY_ENV_VARS: {
+    ns:      'crowi',
+    key:     'app:siteUrl:useOnlyEnvVars',
+    type:    ValueType.BOOLEAN,
+    default: false,
+  },
   PUBLISH_OPEN_API: {
     ns:      'crowi',
     key:     'app:publishOpenAPI',

+ 10 - 1
apps/app/src/server/service/config-manager.ts

@@ -11,6 +11,10 @@ import { S2sMessageHandlable } from './s2s-messaging/handlable';
 
 const logger = loggerFactory('growi:service:ConfigManager');
 
+const KEYS_FOR_APP_SITE_URL_USES_ONLY_ENV_OPTION = [
+  'app:siteUrl',
+];
+
 const KEYS_FOR_LOCAL_STRATEGY_USE_ONLY_ENV_OPTION = [
   'security:passport-local:isEnabled',
 ];
@@ -233,8 +237,13 @@ export default class ConfigManager implements S2sMessageHandlable {
    */
   shouldSearchedFromEnvVarsOnly(namespace, key) {
     return (namespace === 'crowi' && (
-      // local strategy
+      // siteUrl
       (
+        KEYS_FOR_APP_SITE_URL_USES_ONLY_ENV_OPTION.includes(key)
+        && this.defaultSearch('crowi', 'app:siteUrl:useOnlyEnvVars')
+      )
+      // local strategy
+      || (
         KEYS_FOR_LOCAL_STRATEGY_USE_ONLY_ENV_OPTION.includes(key)
         && this.defaultSearch('crowi', 'security:passport-local:useOnlyEnvVarsForSomeOptions')
       )