AdminAppContainer.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. import { isServer } from '@growi/core/dist/utils';
  2. import { Container } from 'unstated';
  3. import { apiv3Get, apiv3Post, apiv3Put } from '../util/apiv3-client';
  4. /**
  5. * Service container for admin app setting page (AppSettings.jsx)
  6. * @extends {Container} unstated Container
  7. */
  8. export default class AdminAppContainer extends Container {
  9. constructor() {
  10. super();
  11. if (isServer()) {
  12. return;
  13. }
  14. this.state = {
  15. retrieveError: null,
  16. title: '',
  17. confidential: '',
  18. globalLang: '',
  19. isEmailPublishedForNewUser: true,
  20. fileUpload: '',
  21. isV5Compatible: null,
  22. siteUrl: '',
  23. siteUrlUseOnlyEnvVars: null,
  24. envSiteUrl: '',
  25. isSetSiteUrl: true,
  26. isMailerSetup: false,
  27. fromAddress: '',
  28. transmissionMethod: '',
  29. smtpHost: '',
  30. smtpPort: '',
  31. smtpUser: '',
  32. smtpPassword: '',
  33. sesAccessKeyId: '',
  34. sesSecretAccessKey: '',
  35. fileUploadType: '',
  36. envFileUploadType: '',
  37. isFixedFileUploadByEnvVar: false,
  38. gcsUseOnlyEnvVars: false,
  39. gcsApiKeyJsonPath: '',
  40. envGcsApiKeyJsonPath: '',
  41. gcsBucket: '',
  42. envGcsBucket: '',
  43. gcsUploadNamespace: '',
  44. envGcsUploadNamespace: '',
  45. gcsReferenceFileWithRelayMode: false,
  46. s3Region: '',
  47. s3CustomEndpoint: '',
  48. s3Bucket: '',
  49. s3AccessKeyId: '',
  50. s3SecretAccessKey: '',
  51. s3ReferenceFileWithRelayMode: false,
  52. azureReferenceFileWithRelayMode: false,
  53. azureUseOnlyEnvVars: false,
  54. azureTenantId: '',
  55. azureClientId: '',
  56. azureClientSecret: '',
  57. azureStorageAccountName: '',
  58. azureStorageContainerName: '',
  59. envAzureTenantId: '',
  60. envAzureClientId: '',
  61. envAzureClientSecret: '',
  62. envAzureStorageAccountName: '',
  63. envAzureStorageContainerName: '',
  64. isEnabledPlugins: true,
  65. isMaintenanceMode: false,
  66. // TODO: remove this property when bulk export can be relased for cloud (https://redmine.weseek.co.jp/issues/163220)
  67. isBulkExportDisabledForCloud: false,
  68. };
  69. }
  70. /**
  71. * Workaround for the mangling in production build to break constructor.name
  72. */
  73. static getClassName() {
  74. return 'AdminAppContainer';
  75. }
  76. /**
  77. * retrieve app sttings data
  78. */
  79. async retrieveAppSettingsData() {
  80. const response = await apiv3Get('/app-settings/');
  81. const { appSettingsParams } = response.data;
  82. this.setState({
  83. title: appSettingsParams.title,
  84. confidential: appSettingsParams.confidential,
  85. globalLang: appSettingsParams.globalLang,
  86. isEmailPublishedForNewUser: appSettingsParams.isEmailPublishedForNewUser,
  87. fileUpload: appSettingsParams.fileUpload,
  88. isV5Compatible: appSettingsParams.isV5Compatible,
  89. siteUrl: appSettingsParams.siteUrl,
  90. siteUrlUseOnlyEnvVars: appSettingsParams.siteUrlUseOnlyEnvVars,
  91. envSiteUrl: appSettingsParams.envSiteUrl,
  92. isSetSiteUrl: !!appSettingsParams.siteUrl,
  93. isMailerSetup: appSettingsParams.isMailerSetup,
  94. fromAddress: appSettingsParams.fromAddress,
  95. transmissionMethod: appSettingsParams.transmissionMethod,
  96. smtpHost: appSettingsParams.smtpHost,
  97. smtpPort: appSettingsParams.smtpPort,
  98. smtpUser: appSettingsParams.smtpUser,
  99. smtpPassword: appSettingsParams.smtpPassword,
  100. sesAccessKeyId: appSettingsParams.sesAccessKeyId,
  101. sesSecretAccessKey: appSettingsParams.sesSecretAccessKey,
  102. fileUploadType: appSettingsParams.fileUploadType,
  103. envFileUploadType: appSettingsParams.envFileUploadType,
  104. useOnlyEnvVarForFileUploadType: appSettingsParams.useOnlyEnvVarForFileUploadType,
  105. s3Region: appSettingsParams.s3Region,
  106. s3CustomEndpoint: appSettingsParams.s3CustomEndpoint,
  107. s3Bucket: appSettingsParams.s3Bucket,
  108. s3AccessKeyId: appSettingsParams.s3AccessKeyId,
  109. s3ReferenceFileWithRelayMode: appSettingsParams.s3ReferenceFileWithRelayMode,
  110. gcsUseOnlyEnvVars: appSettingsParams.gcsUseOnlyEnvVars,
  111. gcsApiKeyJsonPath: appSettingsParams.gcsApiKeyJsonPath,
  112. gcsBucket: appSettingsParams.gcsBucket,
  113. gcsUploadNamespace: appSettingsParams.gcsUploadNamespace,
  114. gcsReferenceFileWithRelayMode: appSettingsParams.gcsReferenceFileWithRelayMode,
  115. envGcsApiKeyJsonPath: appSettingsParams.envGcsApiKeyJsonPath,
  116. envGcsBucket: appSettingsParams.envGcsBucket,
  117. envGcsUploadNamespace: appSettingsParams.envGcsUploadNamespace,
  118. azureUseOnlyEnvVars: appSettingsParams.azureUseOnlyEnvVars,
  119. azureTenantId: appSettingsParams.azureTenantId,
  120. azureClientId: appSettingsParams.azureClientId,
  121. azureClientSecret: appSettingsParams.azureClientSecret,
  122. azureStorageAccountName: appSettingsParams.azureStorageAccountName,
  123. azureStorageContainerName: appSettingsParams.azureStorageContainerName,
  124. azureReferenceFileWithRelayMode: appSettingsParams.azureReferenceFileWithRelayMode,
  125. envAzureTenantId: appSettingsParams.envAzureTenantId,
  126. envAzureClientId: appSettingsParams.envAzureClientId,
  127. envAzureClientSecret: appSettingsParams.envAzureClientSecret,
  128. envAzureStorageAccountName: appSettingsParams.envAzureStorageAccountName,
  129. envAzureStorageContainerName: appSettingsParams.envAzureStorageContainerName,
  130. isEnabledPlugins: appSettingsParams.isEnabledPlugins,
  131. isMaintenanceMode: appSettingsParams.isMaintenanceMode,
  132. // TODO: remove this property when bulk export can be relased for cloud (https://redmine.weseek.co.jp/issues/163220)
  133. isBulkExportDisabledForCloud: appSettingsParams.isBulkExportDisabledForCloud,
  134. });
  135. // if useOnlyEnvVarForFileUploadType is true, get fileUploadType from only env var and make the forms fixed.
  136. // and if env var 'FILE_UPLOAD' is null, envFileUploadType is 'aws' that is default value of 'FILE_UPLOAD'.
  137. if (appSettingsParams.useOnlyEnvVarForFileUploadType) {
  138. this.setState({ fileUploadType: appSettingsParams.envFileUploadType });
  139. this.setState({ isFixedFileUploadByEnvVar: true });
  140. }
  141. }
  142. /**
  143. * Change title
  144. */
  145. changeTitle(title) {
  146. this.setState({ title });
  147. }
  148. /**
  149. * Change confidential
  150. */
  151. changeConfidential(confidential) {
  152. this.setState({ confidential });
  153. }
  154. /**
  155. * Change globalLang
  156. */
  157. changeGlobalLang(globalLang) {
  158. this.setState({ globalLang });
  159. }
  160. /**
  161. * Change isEmailPublishedForNewUser
  162. */
  163. changeIsEmailPublishedForNewUserShow(isEmailPublishedForNewUser) {
  164. this.setState({ isEmailPublishedForNewUser });
  165. }
  166. /**
  167. * Change fileUpload
  168. */
  169. changeFileUpload(fileUpload) {
  170. this.setState({ fileUpload });
  171. }
  172. /**
  173. * Change site url
  174. */
  175. changeIsV5Compatible(isV5Compatible) {
  176. this.setState({ isV5Compatible });
  177. }
  178. /**
  179. * Change site url
  180. */
  181. changeSiteUrl(siteUrl) {
  182. this.setState({ siteUrl });
  183. }
  184. /**
  185. * Change from address
  186. */
  187. changeFromAddress(fromAddress) {
  188. this.setState({ fromAddress });
  189. }
  190. /**
  191. * Change from transmission method
  192. */
  193. changeTransmissionMethod(transmissionMethod) {
  194. this.setState({ transmissionMethod });
  195. }
  196. /**
  197. * Change smtp host
  198. */
  199. changeSmtpHost(smtpHost) {
  200. this.setState({ smtpHost });
  201. }
  202. /**
  203. * Change smtp port
  204. */
  205. changeSmtpPort(smtpPort) {
  206. this.setState({ smtpPort });
  207. }
  208. /**
  209. * Change smtp user
  210. */
  211. changeSmtpUser(smtpUser) {
  212. this.setState({ smtpUser });
  213. }
  214. /**
  215. * Change smtp password
  216. */
  217. changeSmtpPassword(smtpPassword) {
  218. this.setState({ smtpPassword });
  219. }
  220. /**
  221. * Change sesAccessKeyId
  222. */
  223. changeSesAccessKeyId(sesAccessKeyId) {
  224. this.setState({ sesAccessKeyId });
  225. }
  226. /**
  227. * Change sesSecretAccessKey
  228. */
  229. changeSesSecretAccessKey(sesSecretAccessKey) {
  230. this.setState({ sesSecretAccessKey });
  231. }
  232. /**
  233. * Change s3Region
  234. */
  235. changeS3Region(s3Region) {
  236. this.setState({ s3Region });
  237. }
  238. /**
  239. * Change s3CustomEndpoint
  240. */
  241. changeS3CustomEndpoint(s3CustomEndpoint) {
  242. this.setState({ s3CustomEndpoint });
  243. }
  244. /**
  245. * Change fileUploadType
  246. */
  247. changeFileUploadType(fileUploadType) {
  248. this.setState({ fileUploadType });
  249. }
  250. /**
  251. * Change region
  252. */
  253. changeS3Bucket(s3Bucket) {
  254. this.setState({ s3Bucket });
  255. }
  256. /**
  257. * Change access key id
  258. */
  259. changeS3AccessKeyId(s3AccessKeyId) {
  260. this.setState({ s3AccessKeyId });
  261. }
  262. /**
  263. * Change secret access key
  264. */
  265. changeS3SecretAccessKey(s3SecretAccessKey) {
  266. this.setState({ s3SecretAccessKey });
  267. }
  268. /**
  269. * Change s3ReferenceFileWithRelayMode
  270. */
  271. changeS3ReferenceFileWithRelayMode(s3ReferenceFileWithRelayMode) {
  272. this.setState({ s3ReferenceFileWithRelayMode });
  273. }
  274. /**
  275. * Change gcsApiKeyJsonPath
  276. */
  277. changeGcsApiKeyJsonPath(gcsApiKeyJsonPath) {
  278. this.setState({ gcsApiKeyJsonPath });
  279. }
  280. /**
  281. * Change gcsBucket
  282. */
  283. changeGcsBucket(gcsBucket) {
  284. this.setState({ gcsBucket });
  285. }
  286. /**
  287. * Change gcsUploadNamespace
  288. */
  289. changeGcsUploadNamespace(gcsUploadNamespace) {
  290. this.setState({ gcsUploadNamespace });
  291. }
  292. /**
  293. * Change gcsReferenceFileWithRelayMode
  294. */
  295. changeGcsReferenceFileWithRelayMode(gcsReferenceFileWithRelayMode) {
  296. this.setState({ gcsReferenceFileWithRelayMode });
  297. }
  298. /**
  299. * Change azureReferenceFileWithRelayMode
  300. */
  301. changeAzureReferenceFileWithRelayMode(azureReferenceFileWithRelayMode) {
  302. this.setState({ azureReferenceFileWithRelayMode });
  303. }
  304. /**
  305. * Change azureTenantId
  306. */
  307. changeAzureTenantId(azureTenantId) {
  308. this.setState({ azureTenantId });
  309. }
  310. /**
  311. * Change azureClientId
  312. */
  313. changeAzureClientId(azureClientId) {
  314. this.setState({ azureClientId });
  315. }
  316. /**
  317. * Change azureClientSecret
  318. */
  319. changeAzureClientSecret(azureClientSecret) {
  320. this.setState({ azureClientSecret });
  321. }
  322. /**
  323. * Change azureStorageAccountName
  324. */
  325. changeAzureStorageAccountName(azureStorageAccountName) {
  326. this.setState({ azureStorageAccountName });
  327. }
  328. /**
  329. * Change azureStorageContainerName
  330. */
  331. changeAzureStorageContainerName(azureStorageContainerName) {
  332. this.setState({ azureStorageContainerName });
  333. }
  334. /**
  335. * Update app setting
  336. * @memberOf AdminAppContainer
  337. * @return {Array} Appearance
  338. */
  339. async updateAppSettingHandler() {
  340. const response = await apiv3Put('/app-settings/app-setting', {
  341. title: this.state.title,
  342. confidential: this.state.confidential,
  343. globalLang: this.state.globalLang,
  344. isEmailPublishedForNewUser: this.state.isEmailPublishedForNewUser,
  345. fileUpload: this.state.fileUpload,
  346. });
  347. const { appSettingParams } = response.data;
  348. return appSettingParams;
  349. }
  350. /**
  351. * Update site url setting
  352. * @memberOf AdminAppContainer
  353. * @return {Array} Appearance
  354. */
  355. async updateSiteUrlSettingHandler() {
  356. const response = await apiv3Put('/app-settings/site-url-setting', {
  357. siteUrl: this.state.siteUrl,
  358. });
  359. const { siteUrlSettingParams } = response.data;
  360. return siteUrlSettingParams;
  361. }
  362. /**
  363. * Update mail setting
  364. * @memberOf AdminAppContainer
  365. * @return {Array} Appearance
  366. */
  367. updateMailSettingHandler() {
  368. if (this.state.transmissionMethod === 'smtp') {
  369. return this.updateSmtpSetting();
  370. }
  371. return this.updateSesSetting();
  372. }
  373. /**
  374. * Update smtp setting
  375. * @memberOf AdminAppContainer
  376. * @return {Array} Appearance
  377. */
  378. async updateSmtpSetting() {
  379. const response = await apiv3Put('/app-settings/smtp-setting', {
  380. fromAddress: this.state.fromAddress,
  381. transmissionMethod: this.state.transmissionMethod,
  382. smtpHost: this.state.smtpHost,
  383. smtpPort: this.state.smtpPort,
  384. smtpUser: this.state.smtpUser,
  385. smtpPassword: this.state.smtpPassword,
  386. });
  387. const { mailSettingParams } = response.data;
  388. this.setState({ isMailerSetup: mailSettingParams.isMailerSetup });
  389. return mailSettingParams;
  390. }
  391. /**
  392. * Update ses setting
  393. * @memberOf AdminAppContainer
  394. * @return {Array} Appearance
  395. */
  396. async updateSesSetting() {
  397. const response = await apiv3Put('/app-settings/ses-setting', {
  398. fromAddress: this.state.fromAddress,
  399. transmissionMethod: this.state.transmissionMethod,
  400. sesAccessKeyId: this.state.sesAccessKeyId,
  401. sesSecretAccessKey: this.state.sesSecretAccessKey,
  402. });
  403. const { mailSettingParams } = response.data;
  404. this.setState({ isMailerSetup: mailSettingParams.isMailerSetup });
  405. return mailSettingParams;
  406. }
  407. /**
  408. * send test e-mail
  409. * @memberOf AdminAppContainer
  410. */
  411. async sendTestEmail() {
  412. return apiv3Post('/app-settings/smtp-test');
  413. }
  414. /**
  415. * Update updateFileUploadSettingHandler
  416. * @memberOf AdminAppContainer
  417. */
  418. async updateFileUploadSettingHandler() {
  419. const { fileUploadType } = this.state;
  420. const requestParams = {
  421. fileUploadType,
  422. };
  423. if (fileUploadType === 'gcs') {
  424. requestParams.gcsApiKeyJsonPath = this.state.gcsApiKeyJsonPath;
  425. requestParams.gcsBucket = this.state.gcsBucket;
  426. requestParams.gcsUploadNamespace = this.state.gcsUploadNamespace;
  427. requestParams.gcsReferenceFileWithRelayMode = this.state.gcsReferenceFileWithRelayMode;
  428. }
  429. if (fileUploadType === 'aws') {
  430. requestParams.s3Region = this.state.s3Region;
  431. requestParams.s3CustomEndpoint = this.state.s3CustomEndpoint;
  432. requestParams.s3Bucket = this.state.s3Bucket;
  433. requestParams.s3AccessKeyId = this.state.s3AccessKeyId;
  434. requestParams.s3SecretAccessKey = this.state.s3SecretAccessKey;
  435. requestParams.s3ReferenceFileWithRelayMode = this.state.s3ReferenceFileWithRelayMode;
  436. }
  437. if (fileUploadType === 'azure') {
  438. requestParams.azureTenantId = this.state.azureTenantId;
  439. requestParams.azureClientId = this.state.azureClientId;
  440. requestParams.azureClientSecret = this.state.azureClientSecret;
  441. requestParams.azureStorageAccountName = this.state.azureStorageAccountName;
  442. requestParams.azureStorageContainerName = this.state.azureStorageContainerName;
  443. requestParams.azureReferenceFileWithRelayMode = this.state.azureReferenceFileWithRelayMode;
  444. }
  445. const response = await apiv3Put('/app-settings/file-upload-setting', requestParams);
  446. const { responseParams } = response.data;
  447. return this.setState(responseParams);
  448. }
  449. /**
  450. * Start v5 page migration
  451. * @memberOf AdminAppContainer
  452. */
  453. async v5PageMigrationHandler() {
  454. const response = await apiv3Post('/app-settings/v5-schema-migration');
  455. const { isV5Compatible } = response.data;
  456. return { isV5Compatible };
  457. }
  458. async startMaintenanceMode() {
  459. await apiv3Post('/app-settings/maintenance-mode', { flag: true });
  460. }
  461. async endMaintenanceMode() {
  462. await apiv3Post('/app-settings/maintenance-mode', { flag: false });
  463. }
  464. }