AdminAppContainer.js 15 KB

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