app-settings.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. const loggerFactory = require('@alias/logger');
  2. const logger = loggerFactory('growi:routes:apiv3:app-settings');
  3. const debug = require('debug')('growi:routes:admin');
  4. const express = require('express');
  5. const { listLocaleIds } = require('@commons/util/locale-utils');
  6. const router = express.Router();
  7. const { body } = require('express-validator');
  8. const ErrorV3 = require('../../models/vo/error-apiv3');
  9. /**
  10. * @swagger
  11. * tags:
  12. * name: AppSettings
  13. */
  14. /**
  15. * @swagger
  16. *
  17. * components:
  18. * schemas:
  19. * AppSettingParams:
  20. * description: AppSettingParams
  21. * type: object
  22. * properties:
  23. * title:
  24. * type: string
  25. * description: site name show on page header and tilte of HTML
  26. * confidential:
  27. * type: string
  28. * description: confidential show on page header
  29. * globalLang:
  30. * type: string
  31. * description: language set when create user
  32. * fileUpload:
  33. * type: boolean
  34. * description: enable upload file except image file
  35. * SiteUrlSettingParams:
  36. * description: SiteUrlSettingParams
  37. * type: object
  38. * properties:
  39. * siteUrl:
  40. * type: string
  41. * description: Site URL. e.g. https://example.com, https://example.com:8080
  42. * envSiteUrl:
  43. * type: string
  44. * description: environment variable 'APP_SITE_URL'
  45. * MailSetting:
  46. * description: MailSettingParams
  47. * type: object
  48. * properties:
  49. * fromAddress:
  50. * type: string
  51. * description: e-mail address used as from address of mail which sent from GROWI app
  52. * transmissionMethod:
  53. * type: string
  54. * description: transmission method
  55. * SmtpSettingParams:
  56. * description: SmtpSettingParams
  57. * type: object
  58. * properties:
  59. * smtpHost:
  60. * type: string
  61. * description: host name of client's smtp server
  62. * smtpPort:
  63. * type: string
  64. * description: port of client's smtp server
  65. * smtpUser:
  66. * type: string
  67. * description: user name of client's smtp server
  68. * smtpPassword:
  69. * type: string
  70. * description: password of client's smtp server
  71. * SesSettingParams:
  72. * description: SesSettingParams
  73. * type: object
  74. * properties:
  75. * accessKeyId:
  76. * type: string
  77. * description: accesskey id for authentification of AWS
  78. * secretAccessKey:
  79. * type: string
  80. * description: secret key for authentification of AWS
  81. * AwsSettingParams:
  82. * description: AwsSettingParams
  83. * type: object
  84. * properties:
  85. * region:
  86. * type: string
  87. * description: region of AWS S3
  88. * customEndpoint:
  89. * type: string
  90. * description: custom endpoint of AWS S3
  91. * bucket:
  92. * type: string
  93. * description: AWS S3 bucket name
  94. * accessKeyId:
  95. * type: string
  96. * description: accesskey id for authentification of AWS
  97. * secretAccessKey:
  98. * type: string
  99. * description: secret key for authentification of AWS
  100. * GcpSettingParams:
  101. * description: GcpSettingParams
  102. * type: object
  103. * properties:
  104. * gcsApiKeyJsonPath:
  105. * type: string
  106. * description: apiKeyJsonPath of gcp
  107. * gcsBucket:
  108. * type: string
  109. * description: bucket name of gcs
  110. * gcsUploadNamespace:
  111. * type: string
  112. * description: name space of gcs
  113. * envGcsApiKeyJsonPath:
  114. * type: string
  115. * description: Path of the JSON file that contains service account key to authenticate to GCP API
  116. * envGcsBucket:
  117. * type: string
  118. * description: Name of the GCS bucket
  119. * envGcsUploadNamespace:
  120. * type: string
  121. * description: Directory name to create in the bucket
  122. * PluginSettingParams:
  123. * description: PluginSettingParams
  124. * type: object
  125. * properties:
  126. * isEnabledPlugins:
  127. * type: string
  128. * description: enable use plugins
  129. */
  130. module.exports = (crowi) => {
  131. const accessTokenParser = require('../../middlewares/access-token-parser')(crowi);
  132. const loginRequired = require('../../middlewares/login-required')(crowi);
  133. const loginRequiredStrictly = require('../../middlewares/login-required')(crowi);
  134. const adminRequired = require('../../middlewares/admin-required')(crowi);
  135. const csrf = require('../../middlewares/csrf')(crowi);
  136. const apiV3FormValidator = require('../../middlewares/apiv3-form-validator')(crowi);
  137. const validator = {
  138. appSetting: [
  139. body('title').trim(),
  140. body('confidential'),
  141. body('globalLang').isIn(listLocaleIds()),
  142. body('fileUpload').isBoolean(),
  143. ],
  144. siteUrlSetting: [
  145. body('siteUrl').trim().matches(/^(https?:\/\/[^/]+|)$/).isURL({ require_tld: false }),
  146. ],
  147. mailSetting: [
  148. body('fromAddress').trim().if(value => value !== '').isEmail(),
  149. body('transmissionMethod').isIn(['smtp', 'ses']),
  150. ],
  151. smtpSetting: [
  152. body('smtpHost').trim(),
  153. body('smtpPort').trim().if(value => value !== '').isPort(),
  154. body('smtpUser').trim(),
  155. body('smtpPassword').trim(),
  156. ],
  157. sesSetting: [
  158. body('sesAccessKeyId').trim().if(value => value !== '').matches(/^[\da-zA-Z]+$/),
  159. body('sesSecretAccessKey').trim(),
  160. ],
  161. awsSetting: [
  162. body('s3Region').trim().matches(/^[a-z]+-[a-z]+-\d+$/).withMessage((value, { req }) => req.t('validation.aws_region')),
  163. body('s3CustomEndpoint').trim().matches(/^(https?:\/\/[^/]+|)$/).withMessage((value, { req }) => req.t('validation.aws_custom_endpoint')),
  164. body('s3Bucket').trim(),
  165. body('s3AccessKeyId').trim().if(value => value !== '').matches(/^[\da-zA-Z]+$/),
  166. body('s3SecretAccessKey').trim(),
  167. ],
  168. gcpSetting: [
  169. body('gcsApiKeyJsonPath').trim(),
  170. body('gcsBucket').trim(),
  171. body('gcsUploadNamespace').trim(),
  172. ],
  173. pluginSetting: [
  174. body('isEnabledPlugins').isBoolean(),
  175. ],
  176. };
  177. /**
  178. * @swagger
  179. *
  180. * /app-settings:
  181. * get:
  182. * tags: [AppSettings]
  183. * operationId: getAppSettings
  184. * summary: /app-settings
  185. * description: get app setting params
  186. * responses:
  187. * 200:
  188. * description: Resources are available
  189. * content:
  190. * application/json:
  191. * schema:
  192. * properties:
  193. * appSettingsParams:
  194. * type: object
  195. * description: app settings params
  196. */
  197. router.get('/', accessTokenParser, loginRequired, adminRequired, async(req, res) => {
  198. const appSettingsParams = {
  199. title: crowi.configManager.getConfig('crowi', 'app:title'),
  200. confidential: crowi.configManager.getConfig('crowi', 'app:confidential'),
  201. globalLang: crowi.configManager.getConfig('crowi', 'app:globalLang'),
  202. fileUpload: crowi.configManager.getConfig('crowi', 'app:fileUpload'),
  203. siteUrl: crowi.configManager.getConfig('crowi', 'app:siteUrl'),
  204. envSiteUrl: crowi.configManager.getConfigFromEnvVars('crowi', 'app:siteUrl'),
  205. isMailerSetup: crowi.mailService.isMailerSetup,
  206. fromAddress: crowi.configManager.getConfig('crowi', 'mail:from'),
  207. transmissionMethod: crowi.configManager.getConfig('crowi', 'mail:transmissionMethod'),
  208. smtpHost: crowi.configManager.getConfig('crowi', 'mail:smtpHost'),
  209. smtpPort: crowi.configManager.getConfig('crowi', 'mail:smtpPort'),
  210. smtpUser: crowi.configManager.getConfig('crowi', 'mail:smtpUser'),
  211. smtpPassword: crowi.configManager.getConfig('crowi', 'mail:smtpPassword'),
  212. sesAccessKeyId: crowi.configManager.getConfig('crowi', 'mail:sesAccessKeyId'),
  213. sesSecretAccessKey: crowi.configManager.getConfig('crowi', 'mail:sesSecretAccessKey'),
  214. fileUploadType: crowi.configManager.getConfig('crowi', 'app:fileUploadType'),
  215. envFileUploadType: crowi.configManager.getConfigFromEnvVars('crowi', 'app:fileUploadType'),
  216. s3Region: crowi.configManager.getConfig('crowi', 'aws:s3Region'),
  217. s3CustomEndpoint: crowi.configManager.getConfig('crowi', 'aws:s3CustomEndpoint'),
  218. s3Bucket: crowi.configManager.getConfig('crowi', 'aws:s3Bucket'),
  219. s3AccessKeyId: crowi.configManager.getConfig('crowi', 'aws:s3AccessKeyId'),
  220. s3SecretAccessKey: crowi.configManager.getConfig('crowi', 'aws:s3SecretAccessKey'),
  221. gcsApiKeyJsonPath: crowi.configManager.getConfig('crowi', 'gcs:apiKeyJsonPath'),
  222. gcsBucket: crowi.configManager.getConfig('crowi', 'gcs:bucket'),
  223. gcsUploadNamespace: crowi.configManager.getConfig('crowi', 'gcs:uploadNamespace'),
  224. envGcsApiKeyJsonPath: crowi.configManager.getConfigFromEnvVars('crowi', 'gcs:apiKeyJsonPath'),
  225. envGcsBucket: crowi.configManager.getConfigFromEnvVars('crowi', 'gcs:bucket'),
  226. envGcsUploadNamespace: crowi.configManager.getConfigFromEnvVars('crowi', 'gcs:uploadNamespace'),
  227. isEnabledPlugins: crowi.configManager.getConfig('crowi', 'plugin:isEnabledPlugins'),
  228. };
  229. return res.apiv3({ appSettingsParams });
  230. });
  231. /**
  232. * @swagger
  233. *
  234. * /app-settings/app-setting:
  235. * put:
  236. * tags: [AppSettings]
  237. * summary: /app-settings/app-setting
  238. * operationId: updateAppSettings
  239. * description: Update app setting
  240. * requestBody:
  241. * required: true
  242. * content:
  243. * application/json:
  244. * schema:
  245. * $ref: '#/components/schemas/AppSettingParams'
  246. * responses:
  247. * 200:
  248. * description: Succeeded to update app setting
  249. * content:
  250. * application/json:
  251. * schema:
  252. * $ref: '#/components/schemas/AppSettingParams'
  253. */
  254. router.put('/app-setting', loginRequiredStrictly, adminRequired, csrf, validator.appSetting, apiV3FormValidator, async(req, res) => {
  255. const requestAppSettingParams = {
  256. 'app:title': req.body.title,
  257. 'app:confidential': req.body.confidential,
  258. 'app:globalLang': req.body.globalLang,
  259. 'app:fileUpload': req.body.fileUpload,
  260. };
  261. try {
  262. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestAppSettingParams);
  263. const appSettingParams = {
  264. title: crowi.configManager.getConfig('crowi', 'app:title'),
  265. confidential: crowi.configManager.getConfig('crowi', 'app:confidential'),
  266. globalLang: crowi.configManager.getConfig('crowi', 'app:globalLang'),
  267. fileUpload: crowi.configManager.getConfig('crowi', 'app:fileUpload'),
  268. };
  269. return res.apiv3({ appSettingParams });
  270. }
  271. catch (err) {
  272. const msg = 'Error occurred in updating app setting';
  273. logger.error('Error', err);
  274. return res.apiv3Err(new ErrorV3(msg, 'update-appSetting-failed'));
  275. }
  276. });
  277. /**
  278. * @swagger
  279. *
  280. * /app-settings/site-url-setting:
  281. * put:
  282. * tags: [AppSettings]
  283. * operationId: updateAppSettingSiteUrlSetting
  284. * summary: /app-settings/site-url-setting
  285. * description: Update site url setting
  286. * requestBody:
  287. * required: true
  288. * content:
  289. * application/json:
  290. * schema:
  291. * $ref: '#/components/schemas/SiteUrlSettingParams'
  292. * responses:
  293. * 200:
  294. * description: Succeeded to update site url setting
  295. * content:
  296. * application/json:
  297. * schema:
  298. * $ref: '#/components/schemas/SiteUrlSettingParams'
  299. */
  300. router.put('/site-url-setting', loginRequiredStrictly, adminRequired, csrf, validator.siteUrlSetting, apiV3FormValidator, async(req, res) => {
  301. const requestSiteUrlSettingParams = {
  302. 'app:siteUrl': req.body.siteUrl,
  303. };
  304. try {
  305. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestSiteUrlSettingParams);
  306. const siteUrlSettingParams = {
  307. siteUrl: crowi.configManager.getConfig('crowi', 'app:siteUrl'),
  308. };
  309. return res.apiv3({ siteUrlSettingParams });
  310. }
  311. catch (err) {
  312. const msg = 'Error occurred in updating site url setting';
  313. logger.error('Error', err);
  314. return res.apiv3Err(new ErrorV3(msg, 'update-siteUrlSetting-failed'));
  315. }
  316. });
  317. /**
  318. * send mail (Promise wrapper)
  319. */
  320. async function sendMailPromiseWrapper(smtpClient, options) {
  321. return new Promise((resolve, reject) => {
  322. smtpClient.sendMail(options, (err, res) => {
  323. if (err) {
  324. reject(err);
  325. }
  326. else {
  327. resolve(res);
  328. }
  329. });
  330. });
  331. }
  332. /**
  333. * validate mail setting send test mail
  334. */
  335. async function sendTestEmail(destinationAddress) {
  336. const { configManager, mailService } = crowi;
  337. if (!mailService.isMailerSetup) {
  338. throw Error('mailService is not setup');
  339. }
  340. const fromAddress = configManager.getConfig('crowi', 'mail:from');
  341. if (fromAddress == null) {
  342. throw Error('fromAddress is not setup');
  343. }
  344. const smtpHost = configManager.getConfig('crowi', 'mail:smtpHost');
  345. const smtpPort = configManager.getConfig('crowi', 'mail:smtpPort');
  346. const smtpUser = configManager.getConfig('crowi', 'mail:smtpUser');
  347. const smtpPassword = configManager.getConfig('crowi', 'mail:smtpPassword');
  348. const option = {
  349. host: smtpHost,
  350. port: smtpPort,
  351. };
  352. if (smtpUser && smtpPassword) {
  353. option.auth = {
  354. user: smtpUser,
  355. pass: smtpPassword,
  356. };
  357. }
  358. if (option.port === 465) {
  359. option.secure = true;
  360. }
  361. const smtpClient = mailService.createSMTPClient(option);
  362. debug('mailer setup for validate SMTP setting', smtpClient);
  363. const mailOptions = {
  364. from: fromAddress,
  365. to: destinationAddress,
  366. subject: 'Wiki管理設定のアップデートによるメール通知',
  367. text: 'このメールは、WikiのSMTP設定のアップデートにより送信されています。',
  368. };
  369. await sendMailPromiseWrapper(smtpClient, mailOptions);
  370. }
  371. const updateMailSettinConfig = async function(requestMailSettingParams) {
  372. const {
  373. configManager,
  374. mailService,
  375. } = crowi;
  376. // update config without publishing S2sMessage
  377. await configManager.updateConfigsInTheSameNamespace('crowi', requestMailSettingParams, true);
  378. await mailService.initialize();
  379. mailService.publishUpdatedMessage();
  380. return {
  381. isMailerSetup: mailService.isMailerSetup,
  382. fromAddress: configManager.getConfig('crowi', 'mail:from'),
  383. smtpHost: configManager.getConfig('crowi', 'mail:smtpHost'),
  384. smtpPort: configManager.getConfig('crowi', 'mail:smtpPort'),
  385. smtpUser: configManager.getConfig('crowi', 'mail:smtpUser'),
  386. smtpPassword: configManager.getConfig('crowi', 'mail:smtpPassword'),
  387. sesAccessKeyId: configManager.getConfig('crowi', 'mail:sesAccessKeyId'),
  388. sesSecretAccessKey: configManager.getConfig('crowi', 'mail:sesSecretAccessKey'),
  389. };
  390. };
  391. /**
  392. * @swagger
  393. *
  394. * /app-settings/smtp-setting:
  395. * put:
  396. * tags: [AppSettings]
  397. * operationId: updateAppSettingSmtpSetting
  398. * summary: /app-settings/smtp-setting
  399. * description: Update smtp setting
  400. * requestBody:
  401. * required: true
  402. * content:
  403. * application/json:
  404. * schema:
  405. * $ref: '#/components/schemas/SmtpSettingParams'
  406. * responses:
  407. * 200:
  408. * description: Succeeded to update smtp setting
  409. * content:
  410. * application/json:
  411. * schema:
  412. * $ref: '#/components/schemas/SmtpSettingParams'
  413. */
  414. router.put('/smtp-setting', loginRequiredStrictly, adminRequired, csrf, validator.smtpSetting, apiV3FormValidator, async(req, res) => {
  415. const requestMailSettingParams = {
  416. 'mail:from': req.body.fromAddress,
  417. 'mail:transmissionMethod': req.body.transmissionMethod,
  418. 'mail:smtpHost': req.body.smtpHost,
  419. 'mail:smtpPort': req.body.smtpPort,
  420. 'mail:smtpUser': req.body.smtpUser,
  421. 'mail:smtpPassword': req.body.smtpPassword,
  422. };
  423. try {
  424. const mailSettingParams = await updateMailSettinConfig(requestMailSettingParams);
  425. return res.apiv3({ mailSettingParams });
  426. }
  427. catch (err) {
  428. const msg = 'Error occurred in updating smtp setting';
  429. logger.error('Error', err);
  430. return res.apiv3Err(new ErrorV3(msg, 'update-smtpSetting-failed'));
  431. }
  432. });
  433. /**
  434. * @swagger
  435. *
  436. * /app-settings/smtp-test:
  437. * post:
  438. * tags: [AppSettings]
  439. * operationId: postSmtpTest
  440. * summary: /app-settings/smtp-setting
  441. * description: Send test mail for smtp
  442. * responses:
  443. * 200:
  444. * description: Succeeded to send test mail for smtp
  445. */
  446. router.post('/smtp-test', loginRequiredStrictly, adminRequired, async(req, res) => {
  447. try {
  448. await sendTestEmail(req.user.email);
  449. return res.apiv3({});
  450. }
  451. catch (err) {
  452. const msg = req.t('validation.failed_to_send_a_test_email');
  453. logger.error('Error', err);
  454. debug('Error validate mail setting: ', err);
  455. return res.apiv3Err(new ErrorV3(msg, 'send-email-with-smtp-failed'));
  456. }
  457. });
  458. /**
  459. * @swagger
  460. *
  461. * /app-settings/ses-setting:
  462. * put:
  463. * tags: [AppSettings]
  464. * operationId: updateAppSettingSesSetting
  465. * summary: /app-settings/ses-setting
  466. * description: Update ses setting
  467. * requestBody:
  468. * required: true
  469. * content:
  470. * application/json:
  471. * schema:
  472. * $ref: '#/components/schemas/SesSettingParams'
  473. * responses:
  474. * 200:
  475. * description: Succeeded to update ses setting
  476. * content:
  477. * application/json:
  478. * schema:
  479. * $ref: '#/components/schemas/SesSettingParams'
  480. */
  481. router.put('/ses-setting', loginRequiredStrictly, adminRequired, csrf, validator.sesSetting, apiV3FormValidator, async(req, res) => {
  482. const { mailService } = crowi;
  483. const requestSesSettingParams = {
  484. 'mail:from': req.body.fromAddress,
  485. 'mail:transmissionMethod': req.body.transmissionMethod,
  486. 'mail:sesAccessKeyId': req.body.sesAccessKeyId,
  487. 'mail:sesSecretAccessKey': req.body.sesSecretAccessKey,
  488. };
  489. let mailSettingParams;
  490. try {
  491. mailSettingParams = await updateMailSettinConfig(requestSesSettingParams);
  492. }
  493. catch (err) {
  494. const msg = 'Error occurred in updating ses setting';
  495. logger.error('Error', err);
  496. return res.apiv3Err(new ErrorV3(msg, 'update-ses-setting-failed'));
  497. }
  498. await mailService.initialize();
  499. mailService.publishUpdatedMessage();
  500. return res.apiv3({ mailSettingParams });
  501. });
  502. /**
  503. * @swagger
  504. *
  505. * /app-settings/aws-setting:
  506. * put:
  507. * tags: [AppSettings]
  508. * operationId: updateAppSettingAwsSetting
  509. * summary: /app-settings/aws-setting
  510. * description: Update aws setting
  511. * requestBody:
  512. * required: true
  513. * content:
  514. * application/json:
  515. * schema:
  516. * $ref: '#/components/schemas/AwsSettingParams'
  517. * responses:
  518. * 200:
  519. * description: Succeeded to update aws setting
  520. * content:
  521. * application/json:
  522. * schema:
  523. * $ref: '#/components/schemas/AwsSettingParams'
  524. */
  525. router.put('/aws-setting', loginRequiredStrictly, adminRequired, csrf, validator.awsSetting, apiV3FormValidator, async(req, res) => {
  526. const requestAwsSettingParams = {
  527. 'app:fileUploadType': req.body.fileUploadType,
  528. 'aws:s3Region': req.body.s3Region,
  529. 'aws:s3CustomEndpoint': req.body.s3CustomEndpoint,
  530. 'aws:s3Bucket': req.body.s3Bucket,
  531. 'aws:s3AccessKeyId': req.body.s3AccessKeyId,
  532. 'aws:s3SecretAccessKey': req.body.s3SecretAccessKey,
  533. };
  534. try {
  535. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestAwsSettingParams, true);
  536. crowi.fileUploadService.publishUpdatedMessage();
  537. const awsSettingParams = {
  538. s3Region: crowi.configManager.getConfig('crowi', 'aws:s3Region'),
  539. s3CustomEndpoint: crowi.configManager.getConfig('crowi', 'aws:s3CustomEndpoint'),
  540. s3Bucket: crowi.configManager.getConfig('crowi', 'aws:s3Bucket'),
  541. s3AccessKeyId: crowi.configManager.getConfig('crowi', 'aws:s3AccessKeyId'),
  542. s3SecretAccessKey: crowi.configManager.getConfig('crowi', 'aws:s3SecretAccessKey'),
  543. };
  544. return res.apiv3({ awsSettingParams });
  545. }
  546. catch (err) {
  547. const msg = 'Error occurred in updating aws setting';
  548. logger.error('Error', err);
  549. return res.apiv3Err(new ErrorV3(msg, 'update-awsSetting-failed'));
  550. }
  551. });
  552. /**
  553. * @swagger
  554. *
  555. * /app-settings/gcp-setting:
  556. * put:
  557. * tags: [AppSettings]
  558. * operationId: updateAppSettingGcpSetting
  559. * summary: /app-settings/gcp-setting
  560. * description: Update gcp setting
  561. * requestBody:
  562. * required: true
  563. * content:
  564. * application/json:
  565. * schema:
  566. * $ref: '#/components/schemas/GcpSettingParams'
  567. * responses:
  568. * 200:
  569. * description: Succeeded to update gcp setting
  570. * content:
  571. * application/json:
  572. * schema:
  573. * $ref: '#/components/schemas/GcpSettingParams'
  574. */
  575. router.put('/gcp-setting', loginRequiredStrictly, adminRequired, csrf, validator.gcpSetting, apiV3FormValidator, async(req, res) => {
  576. const requestGcpSettingParams = {
  577. 'app:fileUploadType': req.body.fileUploadType,
  578. 'gcs:apiKeyJsonPath': req.body.gcsApiKeyJsonPath,
  579. 'gcs:bucket': req.body.gcsBucket,
  580. 'gcs:uploadNamespace': req.body.gcsUploadNamespace,
  581. };
  582. try {
  583. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestGcpSettingParams, true);
  584. crowi.fileUploadService.publishUpdatedMessage();
  585. const gcpSettingParams = {
  586. gcsApiKeyJsonPath: crowi.configManager.getConfig('crowi', 'gcs:apiKeyJsonPath'),
  587. gcsBucket: crowi.configManager.getConfig('crowi', 'gcs:bucket'),
  588. gcsUploadNamespace: crowi.configManager.getConfig('crowi', 'gcs:uploadNamespace'),
  589. };
  590. return res.apiv3({ gcpSettingParams });
  591. }
  592. catch (err) {
  593. const msg = 'Error occurred in updating aws setting';
  594. logger.error('Error', err);
  595. return res.apiv3Err(new ErrorV3(msg, 'update-awsSetting-failed'));
  596. }
  597. });
  598. /**
  599. * @swagger
  600. *
  601. * /app-settings/plugin-setting:
  602. * put:
  603. * tags: [AppSettings]
  604. * operationId: updateAppSettingPluginSetting
  605. * summary: /app-settings/plugin-setting
  606. * description: Update plugin setting
  607. * requestBody:
  608. * required: true
  609. * content:
  610. * application/json:
  611. * schema:
  612. * $ref: '#/components/schemas/PluginSettingParams'
  613. * responses:
  614. * 200:
  615. * description: Succeeded to update plugin setting
  616. * content:
  617. * application/json:
  618. * schema:
  619. * $ref: '#/components/schemas/PluginSettingParams'
  620. */
  621. router.put('/plugin-setting', loginRequiredStrictly, adminRequired, csrf, validator.pluginSetting, apiV3FormValidator, async(req, res) => {
  622. const requestPluginSettingParams = {
  623. 'plugin:isEnabledPlugins': req.body.isEnabledPlugins,
  624. };
  625. try {
  626. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestPluginSettingParams);
  627. const pluginSettingParams = {
  628. isEnabledPlugins: crowi.configManager.getConfig('crowi', 'plugin:isEnabledPlugins'),
  629. };
  630. return res.apiv3({ pluginSettingParams });
  631. }
  632. catch (err) {
  633. const msg = 'Error occurred in updating plugin setting';
  634. logger.error('Error', err);
  635. return res.apiv3Err(new ErrorV3(msg, 'update-pluginSetting-failed'));
  636. }
  637. });
  638. return router;
  639. };