customize-setting.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /* eslint-disable no-unused-vars */
  2. const loggerFactory = require('@alias/logger');
  3. const logger = loggerFactory('growi:routes:apiv3:customize-setting');
  4. const express = require('express');
  5. const router = express.Router();
  6. const { body } = require('express-validator/check');
  7. const ErrorV3 = require('../../models/vo/error-apiv3');
  8. /**
  9. * @swagger
  10. * tags:
  11. * name: CustomizeSetting
  12. */
  13. /**
  14. * @swagger
  15. *
  16. * components:
  17. * schemas:
  18. * CustomizeStatus:
  19. * type: object
  20. * properties:
  21. * layoutType:
  22. * type: string
  23. * themeType:
  24. * type: string
  25. * behaviorType
  26. * type: string
  27. * isEnabledTimeline:
  28. * type: boolean
  29. * isSavedStatesOfTabChanges:
  30. * type: boolean
  31. * isEnabledAttachTitleHeader:
  32. * type: boolean
  33. * recentCreatedLimit:
  34. * type: number
  35. * customizeHeader:
  36. * type: string
  37. * customizeCss:
  38. * type: string
  39. * customizeScript:
  40. * type: string
  41. * customizeScript:
  42. * type: string
  43. */
  44. module.exports = (crowi) => {
  45. const loginRequiredStrictly = require('../../middleware/login-required')(crowi);
  46. const adminRequired = require('../../middleware/admin-required')(crowi);
  47. const csrf = require('../../middleware/csrf')(crowi);
  48. const { ApiV3FormValidator } = crowi.middlewares;
  49. // TODO GW-533 implement accurate validation
  50. const validator = {
  51. layoutTheme: [
  52. body('layoutType').isString(),
  53. body('themeType').isString(),
  54. ],
  55. behavior: [
  56. body('behaviorType').isString(),
  57. ],
  58. function: [
  59. body('isEnabledTimeline').isBoolean(),
  60. body('isSavedStatesOfTabChanges').isBoolean(),
  61. body('isEnabledAttachTitleHeader').isBoolean(),
  62. body('recentCreatedLimit').isInt(),
  63. ],
  64. customizeHeader: [
  65. body('customizeHeader').isString(),
  66. ],
  67. customizeCss: [
  68. body('customizeCss').isString(),
  69. ],
  70. customizeScript: [
  71. body('customizeScript').isString(),
  72. ],
  73. };
  74. // TODO GW-575 writte swagger
  75. router.get('/', loginRequiredStrictly, adminRequired, async(req, res) => {
  76. // TODO GW-575 return others customize settings
  77. return res.apiv3();
  78. });
  79. /**
  80. * @swagger
  81. *
  82. * /customize-setting/layoutTheme:
  83. * put:
  84. * tags: [CustomizeSetting]
  85. * description: Update layout and theme
  86. * requestBody:
  87. * required: true
  88. * content:
  89. * application/json:
  90. * schama:
  91. * type: object
  92. * properties:
  93. * layoutType:
  94. * description: type of layout
  95. * type: string
  96. * themeType:
  97. * description: type of theme
  98. * type: string
  99. * responses:
  100. * 200:
  101. * description: Succeeded to update layout and theme
  102. * content:
  103. * application/json:
  104. * schema:
  105. * properties:
  106. * customizedParams:
  107. * $ref: '#/components/schemas/CustomizeStatus'
  108. */
  109. router.put('/layoutTheme', loginRequiredStrictly, adminRequired, csrf, validator.layoutTheme, ApiV3FormValidator, async(req, res) => {
  110. const requestParams = {
  111. 'customize:layout': req.body.layoutType,
  112. 'customize:theme': req.body.themeType,
  113. };
  114. try {
  115. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  116. const customizedParams = {
  117. layoutType: await crowi.configManager.getConfig('crowi', 'customize:layout'),
  118. themeType: await crowi.configManager.getConfig('crowi', 'customize:theme'),
  119. };
  120. return res.apiv3({ customizedParams });
  121. }
  122. catch (err) {
  123. const msg = 'Error occurred in updating layout and theme';
  124. logger.error('Error', err);
  125. return res.apiv3Err(new ErrorV3(msg, 'update-layoutTheme-failed'));
  126. }
  127. });
  128. /**
  129. * @swagger
  130. *
  131. * /customize-setting/behavior:
  132. * put:
  133. * tags: [CustomizeSetting]
  134. * description: Update behavior
  135. * requestBody:
  136. * required: true
  137. * content:
  138. * application/json:
  139. * schama:
  140. * type: object
  141. * properties:
  142. * behaviorType:
  143. * description: type of behavior
  144. * type: string
  145. * responses:
  146. * 200:
  147. * description: Succeeded to update behavior
  148. * content:
  149. * application/json:
  150. * schema:
  151. * properties:
  152. * customizedParams:
  153. * $ref: '#/components/schemas/CustomizeStatus'
  154. */
  155. router.put('/behavior', loginRequiredStrictly, adminRequired, csrf, validator.behavior, ApiV3FormValidator, async(req, res) => {
  156. const requestParams = {
  157. 'customize:behavior': req.body.behaviorType,
  158. };
  159. try {
  160. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  161. const customizedParams = {
  162. behaviorType: await crowi.configManager.getConfig('crowi', 'customize:behavior'),
  163. };
  164. return res.apiv3({ customizedParams });
  165. }
  166. catch (err) {
  167. const msg = 'Error occurred in updating behavior';
  168. logger.error('Error', err);
  169. return res.apiv3Err(new ErrorV3(msg, 'update-behavior-failed'));
  170. }
  171. });
  172. /**
  173. * @swagger
  174. *
  175. * /customize-setting/function:
  176. * put:
  177. * tags: [CustomizeSetting]
  178. * description: Update function
  179. * requestBody:
  180. * required: true
  181. * content:
  182. * application/json:
  183. * schama:
  184. * type: object
  185. * properties:
  186. * isEnabledTimeline:
  187. * description: is enabled timeline
  188. * type: boolean
  189. * isSavedStatesOfTabChanges:
  190. * description: is saved states of tabChanges
  191. * type: boolean
  192. * isEnabledAttachTitleHeader:
  193. * description: is enabled attach titleHeader
  194. * type: boolean
  195. * recentCreatedLimit:
  196. * description: limit of recent created
  197. * type: number
  198. * responses:
  199. * 200:
  200. * description: Succeeded to update function
  201. * content:
  202. * application/json:
  203. * schema:
  204. * properties:
  205. * customizedParams:
  206. * $ref: '#/components/schemas/CustomizeStatus'
  207. */
  208. router.put('/function', loginRequiredStrictly, adminRequired, csrf, validator.function, ApiV3FormValidator, async(req, res) => {
  209. const requestParams = {
  210. 'customize:isEnabledTimeline': req.body.isEnabledTimeline,
  211. 'customize:isSavedStatesOfTabChanges': req.body.isSavedStatesOfTabChanges,
  212. 'customize:isEnabledAttachTitleHeader': req.body.isEnabledAttachTitleHeader,
  213. 'customize:showRecentCreatedNumber': req.body.recentCreatedLimit,
  214. };
  215. try {
  216. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  217. const customizedParams = {
  218. isEnabledTimeline: await crowi.configManager.getConfig('crowi', 'customize:isEnabledTimeline'),
  219. isSavedStatesOfTabChanges: await crowi.configManager.getConfig('crowi', 'customize:isSavedStatesOfTabChanges'),
  220. isEnabledAttachTitleHeader: await crowi.configManager.getConfig('crowi', 'customize:isEnabledAttachTitleHeader'),
  221. recentCreatedLimit: await crowi.configManager.getConfig('crowi', 'customize:showRecentCreatedNumber'),
  222. };
  223. return res.apiv3({ customizedParams });
  224. }
  225. catch (err) {
  226. const msg = 'Error occurred in updating function';
  227. logger.error('Error', err);
  228. return res.apiv3Err(new ErrorV3(msg, 'update-function-failed'));
  229. }
  230. });
  231. /**
  232. * @swagger
  233. *
  234. * /customize-setting/customizeHeader:
  235. * put:
  236. * tags: [CustomizeSetting]
  237. * description: Update customizeHeader
  238. * requestBody:
  239. * required: true
  240. * content:
  241. * application/json:
  242. * schama:
  243. * type: object
  244. * properties:
  245. * customizeHeader:
  246. * description: customize header
  247. * type: string
  248. * responses:
  249. * 200:
  250. * description: Succeeded to update customize header
  251. * content:
  252. * application/json:
  253. * schema:
  254. * properties:
  255. * customizedParams:
  256. * $ref: '#/components/schemas/CustomizeStatus'
  257. */
  258. router.put('/customize-header', loginRequiredStrictly, adminRequired, csrf, validator.customizeHeader, ApiV3FormValidator, async(req, res) => {
  259. const requestParams = {
  260. 'customize:header': req.body.customizeHeader,
  261. };
  262. try {
  263. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  264. const customizedParams = {
  265. customizeCss: await crowi.configManager.getConfig('crowi', 'customize:header'),
  266. };
  267. return res.apiv3({ customizedParams });
  268. }
  269. catch (err) {
  270. const msg = 'Error occurred in updating customizeHeader';
  271. logger.error('Error', err);
  272. return res.apiv3Err(new ErrorV3(msg, 'update-customizeHeader-failed'));
  273. }
  274. });
  275. /**
  276. * @swagger
  277. *
  278. * /customize-setting/customizeCss:
  279. * put:
  280. * tags: [CustomizeSetting]
  281. * description: Update customizeCss
  282. * requestBody:
  283. * required: true
  284. * content:
  285. * application/json:
  286. * schama:
  287. * type: object
  288. * properties:
  289. * customizeCss:
  290. * description: customize css
  291. * type: string
  292. * responses:
  293. * 200:
  294. * description: Succeeded to update customize css
  295. * content:
  296. * application/json:
  297. * schema:
  298. * properties:
  299. * customizedParams:
  300. * $ref: '#/components/schemas/CustomizeStatus'
  301. */
  302. router.put('/customize-css', loginRequiredStrictly, adminRequired, csrf, validator.customizeCss, ApiV3FormValidator, async(req, res) => {
  303. const requestParams = {
  304. 'customize:css': req.body.customizeCss,
  305. };
  306. try {
  307. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  308. const customizedParams = {
  309. customizeCss: await crowi.configManager.getConfig('crowi', 'customize:css'),
  310. };
  311. return res.apiv3({ customizedParams });
  312. }
  313. catch (err) {
  314. const msg = 'Error occurred in updating customizeCss';
  315. logger.error('Error', err);
  316. return res.apiv3Err(new ErrorV3(msg, 'update-customizeCss-failed'));
  317. }
  318. });
  319. /**
  320. * @swagger
  321. *
  322. * /customize-setting/customizeScript:
  323. * put:
  324. * tags: [CustomizeSetting]
  325. * description: Update customizeScript
  326. * requestBody:
  327. * required: true
  328. * content:
  329. * application/json:
  330. * schama:
  331. * type: object
  332. * properties:
  333. * customizeScript:
  334. * description: customize script
  335. * type: string
  336. * responses:
  337. * 200:
  338. * description: Succeeded to update customize script
  339. * content:
  340. * application/json:
  341. * schema:
  342. * properties:
  343. * customizedParams:
  344. * $ref: '#/components/schemas/CustomizeStatus'
  345. */
  346. router.put('/customize-script', loginRequiredStrictly, adminRequired, csrf, validator.customizeScript, ApiV3FormValidator, async(req, res) => {
  347. const requestParams = {
  348. 'customize:script': req.body.customizeScript,
  349. };
  350. try {
  351. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  352. const customizedParams = {
  353. customizeScript: await crowi.configManager.getConfig('crowi', 'customize:script'),
  354. };
  355. return res.apiv3({ customizedParams });
  356. }
  357. catch (err) {
  358. const msg = 'Error occurred in updating customizeScript';
  359. logger.error('Error', err);
  360. return res.apiv3Err(new ErrorV3(msg, 'update-customizeScript-failed'));
  361. }
  362. });
  363. return router;
  364. };