customize-setting.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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, query } = require('express-validator');
  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. * CustomizeLayoutTheme:
  19. * description: CustomizeLayoutTheme
  20. * type: object
  21. * properties:
  22. * layoutType:
  23. * type: string
  24. * themeType:
  25. * type: string
  26. * CustomizeFunction:
  27. * description: CustomizeFunction
  28. * type: object
  29. * properties:
  30. * isEnabledTimeline:
  31. * type: boolean
  32. * isSavedStatesOfTabChanges:
  33. * type: boolean
  34. * isEnabledAttachTitleHeader:
  35. * type: boolean
  36. * recentCreatedLimit:
  37. * type: number
  38. * isEnabledStaleNotification:
  39. * type: boolean
  40. * isAllReplyShown:
  41. * type: boolean
  42. * CustomizeHighlight:
  43. * description: CustomizeHighlight
  44. * type: object
  45. * properties:
  46. * styleName:
  47. * type: string
  48. * styleBorder:
  49. * type: boolean
  50. * CustomizeTitle:
  51. * description: CustomizeTitle
  52. * type: object
  53. * properties:
  54. * customizeTitle:
  55. * type: string
  56. * CustomizeHeader:
  57. * description: CustomizeHeader
  58. * type: object
  59. * properties:
  60. * customizeHeader:
  61. * type: string
  62. * CustomizeCss:
  63. * description: CustomizeCss
  64. * type: object
  65. * properties:
  66. * customizeCss:
  67. * type: string
  68. * CustomizeScript:
  69. * description: CustomizeScript
  70. * type: object
  71. * properties:
  72. * customizeScript:
  73. * type: string
  74. */
  75. module.exports = (crowi) => {
  76. const loginRequiredStrictly = require('../../middlewares/login-required')(crowi);
  77. const adminRequired = require('../../middlewares/admin-required')(crowi);
  78. const csrf = require('../../middlewares/csrf')(crowi);
  79. const apiV3FormValidator = require('../../middlewares/apiv3-form-validator')(crowi);
  80. const { customizeService } = crowi;
  81. const validator = {
  82. themeAssetPath: [
  83. query('themeName').isString().isIn([
  84. 'default', 'nature', 'mono-blue', 'wood', 'island', 'christmas', 'antarctic', 'future', 'halloween', 'spring',
  85. ]),
  86. ],
  87. layoutTheme: [
  88. body('layoutType').isString().isIn(['growi', 'kibela']),
  89. body('themeType').isString().isIn([
  90. 'default', 'nature', 'mono-blue', 'wood', 'island', 'christmas', 'antarctic', 'future', 'halloween', 'spring',
  91. ]),
  92. ],
  93. function: [
  94. body('isEnabledTimeline').isBoolean(),
  95. body('isSavedStatesOfTabChanges').isBoolean(),
  96. body('isEnabledAttachTitleHeader').isBoolean(),
  97. body('recentCreatedLimit').isInt().isInt({ min: 1, max: 1000 }),
  98. body('isEnabledStaleNotification').isBoolean(),
  99. body('isAllReplyShown').isBoolean(),
  100. ],
  101. customizeTitle: [
  102. body('customizeTitle').isString(),
  103. ],
  104. customizeHeader: [
  105. body('customizeHeader').isString(),
  106. ],
  107. highlight: [
  108. body('highlightJsStyle').isString().isIn([
  109. 'github', 'github-gist', 'atom-one-light', 'xcode', 'vs', 'atom-one-dark', 'hybrid', 'monokai', 'tomorrow-night', 'vs2015',
  110. ]),
  111. body('highlightJsStyleBorder').isBoolean(),
  112. ],
  113. customizeCss: [
  114. body('customizeCss').isString(),
  115. ],
  116. customizeScript: [
  117. body('customizeScript').isString(),
  118. ],
  119. };
  120. /**
  121. * @swagger
  122. *
  123. * /customize-setting:
  124. * get:
  125. * tags: [CustomizeSetting]
  126. * operationId: getCustomizeSetting
  127. * summary: /customize-setting
  128. * description: Get customize parameters
  129. * responses:
  130. * 200:
  131. * description: params of customize
  132. * content:
  133. * application/json:
  134. * schema:
  135. * properties:
  136. * customizeParams:
  137. * type: object
  138. * description: customize params
  139. */
  140. router.get('/', loginRequiredStrictly, adminRequired, async(req, res) => {
  141. const customizeParams = {
  142. layoutType: await crowi.configManager.getConfig('crowi', 'customize:layout'),
  143. themeType: await crowi.configManager.getConfig('crowi', 'customize:theme'),
  144. isEnabledTimeline: await crowi.configManager.getConfig('crowi', 'customize:isEnabledTimeline'),
  145. isSavedStatesOfTabChanges: await crowi.configManager.getConfig('crowi', 'customize:isSavedStatesOfTabChanges'),
  146. isEnabledAttachTitleHeader: await crowi.configManager.getConfig('crowi', 'customize:isEnabledAttachTitleHeader'),
  147. recentCreatedLimit: await crowi.configManager.getConfig('crowi', 'customize:showRecentCreatedNumber'),
  148. isEnabledStaleNotification: await crowi.configManager.getConfig('crowi', 'customize:isEnabledStaleNotification'),
  149. isAllReplyShown: await crowi.configManager.getConfig('crowi', 'customize:isAllReplyShown'),
  150. styleName: await crowi.configManager.getConfig('crowi', 'customize:highlightJsStyle'),
  151. styleBorder: await crowi.configManager.getConfig('crowi', 'customize:highlightJsStyleBorder'),
  152. customizeTitle: await crowi.configManager.getConfig('crowi', 'customize:title'),
  153. customizeHeader: await crowi.configManager.getConfig('crowi', 'customize:header'),
  154. customizeCss: await crowi.configManager.getConfig('crowi', 'customize:css'),
  155. customizeScript: await crowi.configManager.getConfig('crowi', 'customize:script'),
  156. };
  157. return res.apiv3({ customizeParams });
  158. });
  159. /**
  160. * @swagger
  161. *
  162. * /customize-setting/layout-theme/asset-path:
  163. * put:
  164. * tags: [CustomizeSetting]
  165. * operationId: getLayoutThemeAssetPath
  166. * summary: /customize-setting/layout-theme/asset-path
  167. * description: Get layout theme asset path
  168. * parameters:
  169. * - name: themeName
  170. * in: query
  171. * required: true
  172. * schema:
  173. * type: string
  174. * responses:
  175. * 200:
  176. * description: Succeeded to update layout and theme
  177. * content:
  178. * application/json:
  179. * schema:
  180. * properties:
  181. * assetPath:
  182. * type: string
  183. */
  184. router.get('/layout-theme/asset-path', loginRequiredStrictly, adminRequired, validator.themeAssetPath, apiV3FormValidator, async(req, res) => {
  185. const themeName = req.query.themeName;
  186. const webpackAssetKey = `styles/theme-${themeName}.css`;
  187. const assetPath = res.locals.webpack_asset(webpackAssetKey);
  188. if (assetPath == null) {
  189. return res.apiv3Err(new ErrorV3(`The asset for '${webpackAssetKey}' is undefined.`, 'invalid-asset'));
  190. }
  191. return res.apiv3({ assetPath });
  192. });
  193. /**
  194. * @swagger
  195. *
  196. * /customize-setting/layout-theme:
  197. * put:
  198. * tags: [CustomizeSetting]
  199. * operationId: updateLayoutThemeCustomizeSetting
  200. * summary: /customize-setting/layout-theme
  201. * description: Update layout and theme
  202. * requestBody:
  203. * required: true
  204. * content:
  205. * application/json:
  206. * schema:
  207. * $ref: '#/components/schemas/CustomizeLayoutTheme'
  208. * responses:
  209. * 200:
  210. * description: Succeeded to update layout and theme
  211. * content:
  212. * application/json:
  213. * schema:
  214. * $ref: '#/components/schemas/CustomizeLayoutTheme'
  215. */
  216. router.put('/layout-theme', loginRequiredStrictly, adminRequired, csrf, validator.layoutTheme, apiV3FormValidator, async(req, res) => {
  217. const requestParams = {
  218. 'customize:layout': req.body.layoutType,
  219. 'customize:theme': req.body.themeType,
  220. };
  221. try {
  222. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  223. const customizedParams = {
  224. layoutType: await crowi.configManager.getConfig('crowi', 'customize:layout'),
  225. themeType: await crowi.configManager.getConfig('crowi', 'customize:theme'),
  226. };
  227. return res.apiv3({ customizedParams });
  228. }
  229. catch (err) {
  230. const msg = 'Error occurred in updating layout and theme';
  231. logger.error('Error', err);
  232. return res.apiv3Err(new ErrorV3(msg, 'update-layoutTheme-failed'));
  233. }
  234. });
  235. /**
  236. * @swagger
  237. *
  238. * /customize-setting/function:
  239. * put:
  240. * tags: [CustomizeSetting]
  241. * operationId: updateFunctionCustomizeSetting
  242. * summary: /customize-setting/function
  243. * description: Update function
  244. * requestBody:
  245. * required: true
  246. * content:
  247. * application/json:
  248. * schema:
  249. * $ref: '#/components/schemas/CustomizeFunction'
  250. * responses:
  251. * 200:
  252. * description: Succeeded to update function
  253. * content:
  254. * application/json:
  255. * schema:
  256. * $ref: '#/components/schemas/CustomizeFunction'
  257. */
  258. router.put('/function', loginRequiredStrictly, adminRequired, csrf, validator.function, apiV3FormValidator, async(req, res) => {
  259. const requestParams = {
  260. 'customize:isEnabledTimeline': req.body.isEnabledTimeline,
  261. 'customize:isSavedStatesOfTabChanges': req.body.isSavedStatesOfTabChanges,
  262. 'customize:isEnabledAttachTitleHeader': req.body.isEnabledAttachTitleHeader,
  263. 'customize:showRecentCreatedNumber': req.body.recentCreatedLimit,
  264. 'customize:isEnabledStaleNotification': req.body.isEnabledStaleNotification,
  265. 'customize:isAllReplyShown': req.body.isAllReplyShown,
  266. };
  267. try {
  268. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  269. const customizedParams = {
  270. isEnabledTimeline: await crowi.configManager.getConfig('crowi', 'customize:isEnabledTimeline'),
  271. isSavedStatesOfTabChanges: await crowi.configManager.getConfig('crowi', 'customize:isSavedStatesOfTabChanges'),
  272. isEnabledAttachTitleHeader: await crowi.configManager.getConfig('crowi', 'customize:isEnabledAttachTitleHeader'),
  273. recentCreatedLimit: await crowi.configManager.getConfig('crowi', 'customize:showRecentCreatedNumber'),
  274. isEnabledStaleNotification: await crowi.configManager.getConfig('crowi', 'customize:isEnabledStaleNotification'),
  275. isAllReplyShown: await crowi.configManager.getConfig('crowi', 'customize:isAllReplyShown'),
  276. };
  277. return res.apiv3({ customizedParams });
  278. }
  279. catch (err) {
  280. const msg = 'Error occurred in updating function';
  281. logger.error('Error', err);
  282. return res.apiv3Err(new ErrorV3(msg, 'update-function-failed'));
  283. }
  284. });
  285. /**
  286. * @swagger
  287. *
  288. * /customize-setting/highlight:
  289. * put:
  290. * tags: [CustomizeSetting]
  291. * operationId: updateHighlightCustomizeSetting
  292. * summary: /customize-setting/highlight
  293. * description: Update highlight
  294. * requestBody:
  295. * required: true
  296. * content:
  297. * application/json:
  298. * schema:
  299. * $ref: '#/components/schemas/CustomizeHighlight'
  300. * responses:
  301. * 200:
  302. * description: Succeeded to update highlight
  303. * content:
  304. * application/json:
  305. * schema:
  306. * $ref: '#/components/schemas/CustomizeHighlight'
  307. */
  308. router.put('/highlight', loginRequiredStrictly, adminRequired, csrf, validator.highlight, apiV3FormValidator, async(req, res) => {
  309. const requestParams = {
  310. 'customize:highlightJsStyle': req.body.highlightJsStyle,
  311. 'customize:highlightJsStyleBorder': req.body.highlightJsStyleBorder,
  312. };
  313. try {
  314. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  315. const customizedParams = {
  316. styleName: await crowi.configManager.getConfig('crowi', 'customize:highlightJsStyle'),
  317. styleBorder: await crowi.configManager.getConfig('crowi', 'customize:highlightJsStyleBorder'),
  318. };
  319. return res.apiv3({ customizedParams });
  320. }
  321. catch (err) {
  322. const msg = 'Error occurred in updating highlight';
  323. logger.error('Error', err);
  324. return res.apiv3Err(new ErrorV3(msg, 'update-highlight-failed'));
  325. }
  326. });
  327. /**
  328. * @swagger
  329. *
  330. * /customize-setting/customizeTitle:
  331. * put:
  332. * tags: [CustomizeSetting]
  333. * operationId: updateCustomizeTitleCustomizeSetting
  334. * summary: /customize-setting/customizeTitle
  335. * description: Update customizeTitle
  336. * requestBody:
  337. * required: true
  338. * content:
  339. * application/json:
  340. * schema:
  341. * $ref: '#/components/schemas/CustomizeTitle'
  342. * responses:
  343. * 200:
  344. * description: Succeeded to update customizeTitle
  345. * content:
  346. * application/json:
  347. * schema:
  348. * $ref: '#/components/schemas/CustomizeTitle'
  349. */
  350. router.put('/customize-title', loginRequiredStrictly, adminRequired, csrf, validator.customizeTitle, apiV3FormValidator, async(req, res) => {
  351. const requestParams = {
  352. 'customize:title': req.body.customizeTitle,
  353. };
  354. try {
  355. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  356. const customizedParams = {
  357. customizeTitle: await crowi.configManager.getConfig('crowi', 'customize:title'),
  358. };
  359. customizeService.initCustomTitle();
  360. return res.apiv3({ customizedParams });
  361. }
  362. catch (err) {
  363. const msg = 'Error occurred in updating customizeTitle';
  364. logger.error('Error', err);
  365. return res.apiv3Err(new ErrorV3(msg, 'update-customizeTitle-failed'));
  366. }
  367. });
  368. /**
  369. * @swagger
  370. *
  371. * /customize-setting/customizeHeader:
  372. * put:
  373. * tags: [CustomizeSetting]
  374. * operationId: updateCustomizeHeaderCustomizeSetting
  375. * summary: /customize-setting/customizeHeader
  376. * description: Update customizeHeader
  377. * requestBody:
  378. * required: true
  379. * content:
  380. * application/json:
  381. * schema:
  382. * $ref: '#/components/schemas/CustomizeHeader'
  383. * responses:
  384. * 200:
  385. * description: Succeeded to update customize header
  386. * content:
  387. * application/json:
  388. * schema:
  389. * $ref: '#/components/schemas/CustomizeHeader'
  390. */
  391. router.put('/customize-header', loginRequiredStrictly, adminRequired, csrf, validator.customizeHeader, apiV3FormValidator, async(req, res) => {
  392. const requestParams = {
  393. 'customize:header': req.body.customizeHeader,
  394. };
  395. try {
  396. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  397. const customizedParams = {
  398. customizeHeader: await crowi.configManager.getConfig('crowi', 'customize:header'),
  399. };
  400. return res.apiv3({ customizedParams });
  401. }
  402. catch (err) {
  403. const msg = 'Error occurred in updating customizeHeader';
  404. logger.error('Error', err);
  405. return res.apiv3Err(new ErrorV3(msg, 'update-customizeHeader-failed'));
  406. }
  407. });
  408. /**
  409. * @swagger
  410. *
  411. * /customize-setting/customizeCss:
  412. * put:
  413. * tags: [CustomizeSetting]
  414. * operationId: updateCustomizeCssCustomizeSetting
  415. * summary: /customize-setting/customizeCss
  416. * description: Update customizeCss
  417. * requestBody:
  418. * required: true
  419. * content:
  420. * application/json:
  421. * schema:
  422. * $ref: '#/components/schemas/CustomizeCss'
  423. * responses:
  424. * 200:
  425. * description: Succeeded to update customize css
  426. * content:
  427. * application/json:
  428. * schema:
  429. * $ref: '#/components/schemas/CustomizeCss'
  430. */
  431. router.put('/customize-css', loginRequiredStrictly, adminRequired, csrf, validator.customizeCss, apiV3FormValidator, async(req, res) => {
  432. const requestParams = {
  433. 'customize:css': req.body.customizeCss,
  434. };
  435. try {
  436. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  437. const customizedParams = {
  438. customizeCss: await crowi.configManager.getConfig('crowi', 'customize:css'),
  439. };
  440. customizeService.initCustomCss();
  441. return res.apiv3({ customizedParams });
  442. }
  443. catch (err) {
  444. const msg = 'Error occurred in updating customizeCss';
  445. logger.error('Error', err);
  446. return res.apiv3Err(new ErrorV3(msg, 'update-customizeCss-failed'));
  447. }
  448. });
  449. /**
  450. * @swagger
  451. *
  452. * /customize-setting/customizeScript:
  453. * put:
  454. * tags: [CustomizeSetting]
  455. * operationId: updateCustomizeScriptCustomizeSetting
  456. * summary: /customize-setting/customizeScript
  457. * description: Update customizeScript
  458. * requestBody:
  459. * required: true
  460. * content:
  461. * application/json:
  462. * schema:
  463. * $ref: '#/components/schemas/CustomizeScript'
  464. * responses:
  465. * 200:
  466. * description: Succeeded to update customize script
  467. * content:
  468. * application/json:
  469. * schema:
  470. * $ref: '#/components/schemas/CustomizeScript'
  471. */
  472. router.put('/customize-script', loginRequiredStrictly, adminRequired, csrf, validator.customizeScript, apiV3FormValidator, async(req, res) => {
  473. const requestParams = {
  474. 'customize:script': req.body.customizeScript,
  475. };
  476. try {
  477. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  478. const customizedParams = {
  479. customizeScript: await crowi.configManager.getConfig('crowi', 'customize:script'),
  480. };
  481. return res.apiv3({ customizedParams });
  482. }
  483. catch (err) {
  484. const msg = 'Error occurred in updating customizeScript';
  485. logger.error('Error', err);
  486. return res.apiv3Err(new ErrorV3(msg, 'update-customizeScript-failed'));
  487. }
  488. });
  489. return router;
  490. };