app-settings.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. const loggerFactory = require('@alias/logger');
  2. const logger = loggerFactory('growi:routes:apiv3:app-settings');
  3. const express = require('express');
  4. const router = express.Router();
  5. const { body } = require('express-validator/check');
  6. const ErrorV3 = require('../../models/vo/error-apiv3');
  7. const validator = {
  8. appSetting: [
  9. body('title').trim(),
  10. body('confidential'),
  11. body('globalLang').isIn(['en-US', 'ja']),
  12. body('fileUpload').isBoolean(),
  13. ],
  14. siteUrlSetting: [
  15. body('siteUrl').trim(),
  16. ],
  17. };
  18. /**
  19. * @swagger
  20. * tags:
  21. * name: AppSettings
  22. */
  23. /**
  24. * @swagger
  25. *
  26. * components:
  27. * schemas:
  28. * AppSettingParams:
  29. * type: object
  30. * properties:
  31. * title:
  32. * type: String
  33. * description: site name show on page header and tilte of HTML
  34. * confidential:
  35. * type: String
  36. * description: confidential show on page header
  37. * globalLang:
  38. * type: String
  39. * description: language set when create user
  40. * fileUpload:
  41. * type: boolean
  42. * description: enable upload file except image file
  43. * siteUrl:
  44. * type: String
  45. * description: Site URL. e.g. https://example.com, https://example.com:8080
  46. * envSiteUrl:
  47. * type: String
  48. * description: environment variable 'APP_SITE_URL'
  49. */
  50. module.exports = (crowi) => {
  51. const accessTokenParser = require('../../middleware/access-token-parser')(crowi);
  52. const loginRequired = require('../../middleware/login-required')(crowi);
  53. const loginRequiredStrictly = require('../../middleware/login-required')(crowi);
  54. const adminRequired = require('../../middleware/admin-required')(crowi);
  55. const csrf = require('../../middleware/csrf')(crowi);
  56. const { ApiV3FormValidator } = crowi.middlewares;
  57. /**
  58. * @swagger
  59. *
  60. * /app-settings/:
  61. * get:
  62. * tags: [AppSettings]
  63. * description: get app setting params
  64. * responses:
  65. * 200:
  66. * description: Resources are available
  67. * content:
  68. * application/json:
  69. * schema:
  70. * properties:
  71. * title:
  72. * type: String
  73. * description: site name show on page header and tilte of HTML
  74. * confidential:
  75. * type: String
  76. * description: confidential show on page header
  77. * globalLang:
  78. * type: String
  79. * description: language set when create user
  80. * fileUpload:
  81. * type: boolean
  82. * description: enable upload file except image file
  83. * siteUrl:
  84. * type: String
  85. * description: Site URL. e.g. https://example.com, https://example.com:8080
  86. * envSiteUrl:
  87. * type: String
  88. * description: environment variable 'APP_SITE_URL'
  89. */
  90. router.get('/', accessTokenParser, loginRequired, adminRequired, async(req, res) => {
  91. const appSettingParams = {
  92. title: crowi.configManager.getConfig('crowi', 'app:title'),
  93. confidential: crowi.configManager.getConfig('crowi', 'app:confidential'),
  94. globalLang: crowi.configManager.getConfig('crowi', 'app:globalLang'),
  95. fileUpload: crowi.configManager.getConfig('crowi', 'app:fileUpload'),
  96. siteUrl: crowi.configManager.getConfig('crowi', 'app:siteUrl'),
  97. envSiteUrl: crowi.configManager.getConfigFromEnvVars('crowi', 'app:siteUrl'),
  98. };
  99. return res.apiv3({ appSettingParams });
  100. });
  101. /**
  102. * @swagger
  103. *
  104. * /app-settings/app-setting:
  105. * put:
  106. * tags: [AppSettings]
  107. * description: Update app setting
  108. * requestBody:
  109. * required: true
  110. * content:
  111. * application/json:
  112. * schema:
  113. * type: object
  114. * properties:
  115. * title:
  116. * type: String
  117. * description: site name show on page header and tilte of HTML
  118. * confidential:
  119. * type: String
  120. * description: confidential show on page header
  121. * globalLang:
  122. * type: String
  123. * description: language set when create user
  124. * fileUpload:
  125. * type: boolean
  126. * description: enable upload file except image file
  127. * responses:
  128. * 200:
  129. * description: Succeeded to update app setting
  130. * content:
  131. * application/json:
  132. * schema:
  133. * properties:
  134. * status:
  135. * $ref: '#/components/schemas/appSettingParams'
  136. */
  137. router.put('/app-setting', loginRequiredStrictly, adminRequired, csrf, validator.appSetting, ApiV3FormValidator, async(req, res) => {
  138. const requestAppSettingParams = {
  139. 'app:title': req.body.title,
  140. 'app:confidential': req.body.confidential,
  141. 'app:globalLang': req.body.globalLang,
  142. 'app:fileUpload': req.body.fileUpload,
  143. };
  144. try {
  145. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestAppSettingParams);
  146. const appSettingParams = {
  147. title: crowi.configManager.getConfig('crowi', 'app:title'),
  148. confidential: crowi.configManager.getConfig('crowi', 'app:confidential'),
  149. globalLang: crowi.configManager.getConfig('crowi', 'app:globalLang'),
  150. fileUpload: crowi.configManager.getConfig('crowi', 'app:fileUpload'),
  151. };
  152. return res.apiv3({ appSettingParams });
  153. }
  154. catch (err) {
  155. const msg = 'Error occurred in updating app setting';
  156. logger.error('Error', err);
  157. return res.apiv3Err(new ErrorV3(msg, 'update-appSetting-failed'));
  158. }
  159. });
  160. /**
  161. * @swagger
  162. *
  163. * /app-settings/site-url-setting:
  164. * put:
  165. * tags: [AppSettings]
  166. * description: Update site url setting
  167. * requestBody:
  168. * required: true
  169. * content:
  170. * application/json:
  171. * schema:
  172. * type: object
  173. * properties:
  174. * siteUrl:
  175. * type: String
  176. * description: Site URL. e.g. https://example.com, https://example.com:8080
  177. * responses:
  178. * 200:
  179. * description: Succeeded to update site url setting
  180. * content:
  181. * application/json:
  182. * schema:
  183. * properties:
  184. * status:
  185. * $ref: '#/components/schemas/appSettingParams'
  186. */
  187. router.put('/site-url-setting', loginRequiredStrictly, adminRequired, csrf, validator.siteUrlSetting, ApiV3FormValidator, async(req, res) => {
  188. const requestSiteUrlSettingParams = {
  189. 'app:siteUrl': req.body.siteUrl,
  190. };
  191. try {
  192. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestSiteUrlSettingParams);
  193. const appSettingParams = {
  194. siteUrl: crowi.configManager.getConfig('crowi', 'app:siteUrl'),
  195. };
  196. return res.apiv3({ appSettingParams });
  197. }
  198. catch (err) {
  199. const msg = 'Error occurred in updating site url setting';
  200. logger.error('Error', err);
  201. return res.apiv3Err(new ErrorV3(msg, 'update-siteUrlSetting-failed'));
  202. }
  203. });
  204. return router;
  205. };