AdminAppContainer.js 14 KB

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