AdminAppContainer.js 11 KB

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