AdminAppContainer.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. import { isServer } from '@growi/core';
  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. envSiteUrl: '',
  24. isSetSiteUrl: true,
  25. isMailerSetup: false,
  26. fromAddress: '',
  27. transmissionMethod: '',
  28. smtpHost: '',
  29. smtpPort: '',
  30. smtpUser: '',
  31. smtpPassword: '',
  32. sesAccessKeyId: '',
  33. sesSecretAccessKey: '',
  34. fileUploadType: '',
  35. envFileUploadType: '',
  36. isFixedFileUploadByEnvVar: false,
  37. gcsUseOnlyEnvVars: false,
  38. gcsApiKeyJsonPath: '',
  39. envGcsApiKeyJsonPath: '',
  40. gcsBucket: '',
  41. envGcsBucket: '',
  42. gcsUploadNamespace: '',
  43. envGcsUploadNamespace: '',
  44. gcsReferenceFileWithRelayMode: false,
  45. s3Region: '',
  46. s3CustomEndpoint: '',
  47. s3Bucket: '',
  48. s3AccessKeyId: '',
  49. s3SecretAccessKey: '',
  50. s3ReferenceFileWithRelayMode: false,
  51. isEnabledPlugins: true,
  52. isMaintenanceMode: false,
  53. };
  54. }
  55. /**
  56. * Workaround for the mangling in production build to break constructor.name
  57. */
  58. static getClassName() {
  59. return 'AdminAppContainer';
  60. }
  61. /**
  62. * retrieve app sttings data
  63. */
  64. async retrieveAppSettingsData() {
  65. const response = await apiv3Get('/app-settings/');
  66. const { appSettingsParams } = response.data;
  67. this.setState({
  68. title: appSettingsParams.title,
  69. confidential: appSettingsParams.confidential,
  70. globalLang: appSettingsParams.globalLang,
  71. isEmailPublishedForNewUser: appSettingsParams.isEmailPublishedForNewUser,
  72. fileUpload: appSettingsParams.fileUpload,
  73. isV5Compatible: appSettingsParams.isV5Compatible,
  74. siteUrl: appSettingsParams.siteUrl,
  75. envSiteUrl: appSettingsParams.envSiteUrl,
  76. isSetSiteUrl: !!appSettingsParams.siteUrl,
  77. isMailerSetup: appSettingsParams.isMailerSetup,
  78. fromAddress: appSettingsParams.fromAddress,
  79. transmissionMethod: appSettingsParams.transmissionMethod,
  80. smtpHost: appSettingsParams.smtpHost,
  81. smtpPort: appSettingsParams.smtpPort,
  82. smtpUser: appSettingsParams.smtpUser,
  83. smtpPassword: appSettingsParams.smtpPassword,
  84. sesAccessKeyId: appSettingsParams.sesAccessKeyId,
  85. sesSecretAccessKey: appSettingsParams.sesSecretAccessKey,
  86. fileUploadType: appSettingsParams.fileUploadType,
  87. envFileUploadType: appSettingsParams.envFileUploadType,
  88. useOnlyEnvVarForFileUploadType: appSettingsParams.useOnlyEnvVarForFileUploadType,
  89. s3Region: appSettingsParams.s3Region,
  90. s3CustomEndpoint: appSettingsParams.s3CustomEndpoint,
  91. s3Bucket: appSettingsParams.s3Bucket,
  92. s3AccessKeyId: appSettingsParams.s3AccessKeyId,
  93. s3SecretAccessKey: appSettingsParams.s3SecretAccessKey,
  94. s3ReferenceFileWithRelayMode: appSettingsParams.s3ReferenceFileWithRelayMode,
  95. gcsUseOnlyEnvVars: appSettingsParams.gcsUseOnlyEnvVars,
  96. gcsApiKeyJsonPath: appSettingsParams.gcsApiKeyJsonPath,
  97. gcsBucket: appSettingsParams.gcsBucket,
  98. gcsUploadNamespace: appSettingsParams.gcsUploadNamespace,
  99. gcsReferenceFileWithRelayMode: appSettingsParams.gcsReferenceFileWithRelayMode,
  100. envGcsApiKeyJsonPath: appSettingsParams.envGcsApiKeyJsonPath,
  101. envGcsBucket: appSettingsParams.envGcsBucket,
  102. envGcsUploadNamespace: appSettingsParams.envGcsUploadNamespace,
  103. isEnabledPlugins: appSettingsParams.isEnabledPlugins,
  104. isMaintenanceMode: appSettingsParams.isMaintenanceMode,
  105. });
  106. // if useOnlyEnvVarForFileUploadType is true, get fileUploadType from only env var and make the forms fixed.
  107. // and if env var 'FILE_UPLOAD' is null, envFileUploadType is 'aws' that is default value of 'FILE_UPLOAD'.
  108. if (appSettingsParams.useOnlyEnvVarForFileUploadType) {
  109. this.setState({ fileUploadType: appSettingsParams.envFileUploadType });
  110. this.setState({ isFixedFileUploadByEnvVar: true });
  111. }
  112. }
  113. /**
  114. * Change title
  115. */
  116. changeTitle(title) {
  117. this.setState({ title });
  118. }
  119. /**
  120. * Change confidential
  121. */
  122. changeConfidential(confidential) {
  123. this.setState({ confidential });
  124. }
  125. /**
  126. * Change globalLang
  127. */
  128. changeGlobalLang(globalLang) {
  129. this.setState({ globalLang });
  130. }
  131. /**
  132. * Change isEmailPublishedForNewUser
  133. */
  134. changeIsEmailPublishedForNewUserShow(isEmailPublishedForNewUser) {
  135. this.setState({ isEmailPublishedForNewUser });
  136. }
  137. /**
  138. * Change fileUpload
  139. */
  140. changeFileUpload(fileUpload) {
  141. this.setState({ fileUpload });
  142. }
  143. /**
  144. * Change site url
  145. */
  146. changeIsV5Compatible(isV5Compatible) {
  147. this.setState({ isV5Compatible });
  148. }
  149. /**
  150. * Change site url
  151. */
  152. changeSiteUrl(siteUrl) {
  153. this.setState({ siteUrl });
  154. }
  155. /**
  156. * Change from address
  157. */
  158. changeFromAddress(fromAddress) {
  159. this.setState({ fromAddress });
  160. }
  161. /**
  162. * Change from transmission method
  163. */
  164. changeTransmissionMethod(transmissionMethod) {
  165. this.setState({ transmissionMethod });
  166. }
  167. /**
  168. * Change smtp host
  169. */
  170. changeSmtpHost(smtpHost) {
  171. this.setState({ smtpHost });
  172. }
  173. /**
  174. * Change smtp port
  175. */
  176. changeSmtpPort(smtpPort) {
  177. this.setState({ smtpPort });
  178. }
  179. /**
  180. * Change smtp user
  181. */
  182. changeSmtpUser(smtpUser) {
  183. this.setState({ smtpUser });
  184. }
  185. /**
  186. * Change smtp password
  187. */
  188. changeSmtpPassword(smtpPassword) {
  189. this.setState({ smtpPassword });
  190. }
  191. /**
  192. * Change sesAccessKeyId
  193. */
  194. changeSesAccessKeyId(sesAccessKeyId) {
  195. this.setState({ sesAccessKeyId });
  196. }
  197. /**
  198. * Change sesSecretAccessKey
  199. */
  200. changeSesSecretAccessKey(sesSecretAccessKey) {
  201. this.setState({ sesSecretAccessKey });
  202. }
  203. /**
  204. * Change s3Region
  205. */
  206. changeS3Region(s3Region) {
  207. this.setState({ s3Region });
  208. }
  209. /**
  210. * Change s3CustomEndpoint
  211. */
  212. changeS3CustomEndpoint(s3CustomEndpoint) {
  213. this.setState({ s3CustomEndpoint });
  214. }
  215. /**
  216. * Change fileUploadType
  217. */
  218. changeFileUploadType(fileUploadType) {
  219. this.setState({ fileUploadType });
  220. }
  221. /**
  222. * Change region
  223. */
  224. changeS3Bucket(s3Bucket) {
  225. this.setState({ s3Bucket });
  226. }
  227. /**
  228. * Change access key id
  229. */
  230. changeS3AccessKeyId(s3AccessKeyId) {
  231. this.setState({ s3AccessKeyId });
  232. }
  233. /**
  234. * Change secret access key
  235. */
  236. changeS3SecretAccessKey(s3SecretAccessKey) {
  237. this.setState({ s3SecretAccessKey });
  238. }
  239. /**
  240. * Change s3ReferenceFileWithRelayMode
  241. */
  242. changeS3ReferenceFileWithRelayMode(s3ReferenceFileWithRelayMode) {
  243. this.setState({ s3ReferenceFileWithRelayMode });
  244. }
  245. /**
  246. * Change gcsApiKeyJsonPath
  247. */
  248. changeGcsApiKeyJsonPath(gcsApiKeyJsonPath) {
  249. this.setState({ gcsApiKeyJsonPath });
  250. }
  251. /**
  252. * Change gcsBucket
  253. */
  254. changeGcsBucket(gcsBucket) {
  255. this.setState({ gcsBucket });
  256. }
  257. /**
  258. * Change gcsUploadNamespace
  259. */
  260. changeGcsUploadNamespace(gcsUploadNamespace) {
  261. this.setState({ gcsUploadNamespace });
  262. }
  263. /**
  264. * Change gcsReferenceFileWithRelayMode
  265. */
  266. changeGcsReferenceFileWithRelayMode(gcsReferenceFileWithRelayMode) {
  267. this.setState({ gcsReferenceFileWithRelayMode });
  268. }
  269. /**
  270. * Update app setting
  271. * @memberOf AdminAppContainer
  272. * @return {Array} Appearance
  273. */
  274. async updateAppSettingHandler() {
  275. const response = await apiv3Put('/app-settings/app-setting', {
  276. title: this.state.title,
  277. confidential: this.state.confidential,
  278. globalLang: this.state.globalLang,
  279. isEmailPublishedForNewUser: this.state.isEmailPublishedForNewUser,
  280. fileUpload: this.state.fileUpload,
  281. });
  282. const { appSettingParams } = response.data;
  283. return appSettingParams;
  284. }
  285. /**
  286. * Update site url setting
  287. * @memberOf AdminAppContainer
  288. * @return {Array} Appearance
  289. */
  290. async updateSiteUrlSettingHandler() {
  291. const response = await apiv3Put('/app-settings/site-url-setting', {
  292. siteUrl: this.state.siteUrl,
  293. });
  294. const { siteUrlSettingParams } = response.data;
  295. return siteUrlSettingParams;
  296. }
  297. /**
  298. * Update mail setting
  299. * @memberOf AdminAppContainer
  300. * @return {Array} Appearance
  301. */
  302. updateMailSettingHandler() {
  303. if (this.state.transmissionMethod === 'smtp') {
  304. return this.updateSmtpSetting();
  305. }
  306. return this.updateSesSetting();
  307. }
  308. /**
  309. * Update smtp setting
  310. * @memberOf AdminAppContainer
  311. * @return {Array} Appearance
  312. */
  313. async updateSmtpSetting() {
  314. const response = await apiv3Put('/app-settings/smtp-setting', {
  315. fromAddress: this.state.fromAddress,
  316. transmissionMethod: this.state.transmissionMethod,
  317. smtpHost: this.state.smtpHost,
  318. smtpPort: this.state.smtpPort,
  319. smtpUser: this.state.smtpUser,
  320. smtpPassword: this.state.smtpPassword,
  321. });
  322. const { mailSettingParams } = response.data;
  323. this.setState({ isMailerSetup: mailSettingParams.isMailerSetup });
  324. return mailSettingParams;
  325. }
  326. /**
  327. * Update ses setting
  328. * @memberOf AdminAppContainer
  329. * @return {Array} Appearance
  330. */
  331. async updateSesSetting() {
  332. const response = await apiv3Put('/app-settings/ses-setting', {
  333. fromAddress: this.state.fromAddress,
  334. transmissionMethod: this.state.transmissionMethod,
  335. sesAccessKeyId: this.state.sesAccessKeyId,
  336. sesSecretAccessKey: this.state.sesSecretAccessKey,
  337. });
  338. const { mailSettingParams } = response.data;
  339. this.setState({ isMailerSetup: mailSettingParams.isMailerSetup });
  340. return mailSettingParams;
  341. }
  342. /**
  343. * send test e-mail
  344. * @memberOf AdminAppContainer
  345. */
  346. async sendTestEmail() {
  347. return apiv3Post('/app-settings/smtp-test');
  348. }
  349. /**
  350. * Update updateFileUploadSettingHandler
  351. * @memberOf AdminAppContainer
  352. */
  353. async updateFileUploadSettingHandler() {
  354. const { fileUploadType } = this.state;
  355. const requestParams = {
  356. fileUploadType,
  357. };
  358. if (fileUploadType === 'gcs') {
  359. requestParams.gcsApiKeyJsonPath = this.state.gcsApiKeyJsonPath;
  360. requestParams.gcsBucket = this.state.gcsBucket;
  361. requestParams.gcsUploadNamespace = this.state.gcsUploadNamespace;
  362. requestParams.gcsReferenceFileWithRelayMode = this.state.gcsReferenceFileWithRelayMode;
  363. }
  364. if (fileUploadType === 'aws') {
  365. requestParams.s3Region = this.state.s3Region;
  366. requestParams.s3CustomEndpoint = this.state.s3CustomEndpoint;
  367. requestParams.s3Bucket = this.state.s3Bucket;
  368. requestParams.s3AccessKeyId = this.state.s3AccessKeyId;
  369. requestParams.s3SecretAccessKey = this.state.s3SecretAccessKey;
  370. requestParams.s3ReferenceFileWithRelayMode = this.state.s3ReferenceFileWithRelayMode;
  371. }
  372. const response = await apiv3Put('/app-settings/file-upload-setting', requestParams);
  373. const { responseParams } = response.data;
  374. return this.setState(responseParams);
  375. }
  376. /**
  377. * Start v5 page migration
  378. * @memberOf AdminAppContainer
  379. */
  380. async v5PageMigrationHandler() {
  381. const response = await apiv3Post('/app-settings/v5-schema-migration');
  382. const { isV5Compatible } = response.data;
  383. return { isV5Compatible };
  384. }
  385. async startMaintenanceMode() {
  386. await apiv3Post('/app-settings/maintenance-mode', { flag: true });
  387. }
  388. async endMaintenanceMode() {
  389. await apiv3Post('/app-settings/maintenance-mode', { flag: false });
  390. }
  391. }