customize-setting.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. /* eslint-disable no-unused-vars */
  2. import { ErrorV3 } from '@growi/core';
  3. import express from 'express';
  4. import { body } from 'express-validator';
  5. import mongoose from 'mongoose';
  6. import multer from 'multer';
  7. import { GrowiPluginResourceType } from '~/features/growi-plugin/interfaces';
  8. import { GrowiPlugin } from '~/features/growi-plugin/models';
  9. import { SupportedAction } from '~/interfaces/activity';
  10. import { AttachmentType } from '~/server/interfaces/attachment';
  11. import loggerFactory from '~/utils/logger';
  12. import { generateAddActivityMiddleware } from '../../middlewares/add-activity';
  13. import { apiV3FormValidator } from '../../middlewares/apiv3-form-validator';
  14. const logger = loggerFactory('growi:routes:apiv3:customize-setting');
  15. const router = express.Router();
  16. /**
  17. * @swagger
  18. * tags:
  19. * name: CustomizeSetting
  20. */
  21. /**
  22. * @swagger
  23. *
  24. * components:
  25. * schemas:
  26. * CustomizeLayout:
  27. * description: CustomizeLayout
  28. * type: object
  29. * properties:
  30. * isContainerFluid:
  31. * type: boolean
  32. * CustomizeTheme:
  33. * description: CustomizeTheme
  34. * type: object
  35. * properties:
  36. * themeType:
  37. * type: string
  38. * CustomizeFunction:
  39. * description: CustomizeFunction
  40. * type: object
  41. * properties:
  42. * isEnabledTimeline:
  43. * type: boolean
  44. * isEnabledAttachTitleHeader:
  45. * type: boolean
  46. * pageLimitationS:
  47. * type: number
  48. * pageLimitationM:
  49. * type: number
  50. * isEnabledStaleNotification:
  51. * type: boolean
  52. * isAllReplyShown:
  53. * type: boolean
  54. * isSearchScopeChildrenAsDefault:
  55. * type: boolean
  56. * CustomizeHighlight:
  57. * description: CustomizeHighlight
  58. * type: object
  59. * properties:
  60. * styleName:
  61. * type: string
  62. * styleBorder:
  63. * type: boolean
  64. * CustomizeTitle:
  65. * description: CustomizeTitle
  66. * type: object
  67. * properties:
  68. * customizeTitle:
  69. * type: string
  70. * CustomizeNoscript:
  71. * description: CustomizeNoscript
  72. * type: object
  73. * properties:
  74. * customizeNoscript:
  75. * type: string
  76. * CustomizeCss:
  77. * description: CustomizeCss
  78. * type: object
  79. * properties:
  80. * customizeCss:
  81. * type: string
  82. * CustomizeScript:
  83. * description: CustomizeScript
  84. * type: object
  85. * properties:
  86. * customizeScript:
  87. * type: string
  88. */
  89. module.exports = (crowi) => {
  90. const loginRequiredStrictly = require('../../middlewares/login-required')(crowi);
  91. const adminRequired = require('../../middlewares/admin-required')(crowi);
  92. const addActivity = generateAddActivityMiddleware(crowi);
  93. const activityEvent = crowi.event('activity');
  94. const { customizeService, attachmentService } = crowi;
  95. const Attachment = crowi.model('Attachment');
  96. const uploads = multer({ dest: `${crowi.tmpDir}uploads` });
  97. const validator = {
  98. layout: [
  99. body('isContainerFluid').isBoolean(),
  100. ],
  101. theme: [
  102. body('theme').isString(),
  103. ],
  104. sidebar: [
  105. body('isSidebarDrawerMode').isBoolean(),
  106. body('isSidebarClosedAtDockMode').isBoolean(),
  107. ],
  108. function: [
  109. body('isEnabledTimeline').isBoolean(),
  110. body('isEnabledAttachTitleHeader').isBoolean(),
  111. body('pageLimitationS').isInt().isInt({ min: 1, max: 1000 }),
  112. body('pageLimitationM').isInt().isInt({ min: 1, max: 1000 }),
  113. body('pageLimitationL').isInt().isInt({ min: 1, max: 1000 }),
  114. body('pageLimitationXL').isInt().isInt({ min: 1, max: 1000 }),
  115. body('isEnabledStaleNotification').isBoolean(),
  116. body('isAllReplyShown').isBoolean(),
  117. body('isSearchScopeChildrenAsDefault').isBoolean(),
  118. ],
  119. customizeTitle: [
  120. body('customizeTitle').isString(),
  121. ],
  122. highlight: [
  123. body('highlightJsStyle').isString().isIn([
  124. 'github', 'github-gist', 'atom-one-light', 'xcode', 'vs', 'atom-one-dark', 'hybrid', 'monokai', 'tomorrow-night', 'vs2015',
  125. ]),
  126. body('highlightJsStyleBorder').isBoolean(),
  127. ],
  128. customizeScript: [
  129. body('customizeScript').isString(),
  130. ],
  131. customizeCss: [
  132. body('customizeCss').isString(),
  133. ],
  134. customizeNoscript: [
  135. body('customizeNoscript').isString(),
  136. ],
  137. logo: [
  138. body('isDefaultLogo').isBoolean().optional({ nullable: true }),
  139. body('customizedLogoSrc').isString().optional({ nullable: true }),
  140. ],
  141. };
  142. /**
  143. * @swagger
  144. *
  145. * /customize-setting:
  146. * get:
  147. * tags: [CustomizeSetting]
  148. * operationId: getCustomizeSetting
  149. * summary: /customize-setting
  150. * description: Get customize parameters
  151. * responses:
  152. * 200:
  153. * description: params of customize
  154. * content:
  155. * application/json:
  156. * schema:
  157. * properties:
  158. * customizeParams:
  159. * type: object
  160. * description: customize params
  161. */
  162. router.get('/', loginRequiredStrictly, adminRequired, async(req, res) => {
  163. const customizeParams = {
  164. isEnabledTimeline: await crowi.configManager.getConfig('crowi', 'customize:isEnabledTimeline'),
  165. isEnabledAttachTitleHeader: await crowi.configManager.getConfig('crowi', 'customize:isEnabledAttachTitleHeader'),
  166. pageLimitationS: await crowi.configManager.getConfig('crowi', 'customize:showPageLimitationS'),
  167. pageLimitationM: await crowi.configManager.getConfig('crowi', 'customize:showPageLimitationM'),
  168. pageLimitationL: await crowi.configManager.getConfig('crowi', 'customize:showPageLimitationL'),
  169. pageLimitationXL: await crowi.configManager.getConfig('crowi', 'customize:showPageLimitationXL'),
  170. isEnabledStaleNotification: await crowi.configManager.getConfig('crowi', 'customize:isEnabledStaleNotification'),
  171. isAllReplyShown: await crowi.configManager.getConfig('crowi', 'customize:isAllReplyShown'),
  172. isSearchScopeChildrenAsDefault: await crowi.configManager.getConfig('crowi', 'customize:isSearchScopeChildrenAsDefault'),
  173. styleName: await crowi.configManager.getConfig('crowi', 'customize:highlightJsStyle'),
  174. styleBorder: await crowi.configManager.getConfig('crowi', 'customize:highlightJsStyleBorder'),
  175. customizeTitle: await crowi.configManager.getConfig('crowi', 'customize:title'),
  176. customizeScript: await crowi.configManager.getConfig('crowi', 'customize:script'),
  177. customizeCss: await crowi.configManager.getConfig('crowi', 'customize:css'),
  178. customizeNoscript: await crowi.configManager.getConfig('crowi', 'customize:noscript'),
  179. };
  180. return res.apiv3({ customizeParams });
  181. });
  182. /**
  183. * @swagger
  184. *
  185. * /customize-setting/layout:
  186. * get:
  187. * tags: [CustomizeSetting]
  188. * operationId: getLayoutCustomizeSetting
  189. * summary: /customize-setting/layout
  190. * description: Get layout
  191. * responses:
  192. * 200:
  193. * description: Succeeded to get layout
  194. * content:
  195. * application/json:
  196. * schema:
  197. * $ref: '#/components/schemas/CustomizeLayout'
  198. */
  199. router.get('/layout', loginRequiredStrictly, adminRequired, async(req, res) => {
  200. try {
  201. const isContainerFluid = await crowi.configManager.getConfig('crowi', 'customize:isContainerFluid');
  202. return res.apiv3({ isContainerFluid });
  203. }
  204. catch (err) {
  205. const msg = 'Error occurred in getting layout';
  206. logger.error('Error', err);
  207. return res.apiv3Err(new ErrorV3(msg, 'get-layout-failed'));
  208. }
  209. });
  210. /**
  211. * @swagger
  212. *
  213. * /customize-setting/layout:
  214. * put:
  215. * tags: [CustomizeSetting]
  216. * operationId: updateLayoutCustomizeSetting
  217. * summary: /customize-setting/layout
  218. * description: Update layout
  219. * requestBody:
  220. * required: true
  221. * content:
  222. * application/json:
  223. * schema:
  224. * $ref: '#/components/schemas/CustomizeLayout'
  225. * responses:
  226. * 200:
  227. * description: Succeeded to update layout
  228. * content:
  229. * application/json:
  230. * schema:
  231. * $ref: '#/components/schemas/CustomizeLayout'
  232. */
  233. router.put('/layout', loginRequiredStrictly, adminRequired, addActivity, validator.layout, apiV3FormValidator, async(req, res) => {
  234. const requestParams = {
  235. 'customize:isContainerFluid': req.body.isContainerFluid,
  236. };
  237. try {
  238. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  239. const customizedParams = {
  240. isContainerFluid: await crowi.configManager.getConfig('crowi', 'customize:isContainerFluid'),
  241. };
  242. const parameters = { action: SupportedAction.ACTION_ADMIN_LAYOUT_UPDATE };
  243. activityEvent.emit('update', res.locals.activity._id, parameters);
  244. return res.apiv3({ customizedParams });
  245. }
  246. catch (err) {
  247. const msg = 'Error occurred in updating layout';
  248. logger.error('Error', err);
  249. return res.apiv3Err(new ErrorV3(msg, 'update-layout-failed'));
  250. }
  251. });
  252. router.get('/theme', loginRequiredStrictly, adminRequired, async(req, res) => {
  253. try {
  254. const currentTheme = await crowi.configManager.getConfig('crowi', 'customize:theme');
  255. // retrieve plugin manifests
  256. const themePlugins = await GrowiPlugin.findEnabledPluginsIncludingAnyTypes([GrowiPluginResourceType.Theme]);
  257. const pluginThemesMetadatas = themePlugins
  258. .map(themePlugin => themePlugin.meta.themes)
  259. .flat();
  260. return res.apiv3({ currentTheme, pluginThemesMetadatas });
  261. }
  262. catch (err) {
  263. const msg = 'Error occurred in getting theme';
  264. logger.error('Error', err);
  265. return res.apiv3Err(new ErrorV3(msg, 'get-theme-failed'));
  266. }
  267. });
  268. /**
  269. * @swagger
  270. *
  271. * /customize-setting/theme:
  272. * put:
  273. * tags: [CustomizeSetting]
  274. * operationId: updateThemeCustomizeSetting
  275. * summary: /customize-setting/theme
  276. * description: Update theme
  277. * requestBody:
  278. * required: true
  279. * content:
  280. * application/json:
  281. * schema:
  282. * $ref: '#/components/schemas/CustomizeTheme'
  283. * responses:
  284. * 200:
  285. * description: Succeeded to update theme
  286. * content:
  287. * application/json:
  288. * schema:
  289. * $ref: '#/components/schemas/CustomizeTheme'
  290. */
  291. router.put('/theme', loginRequiredStrictly, adminRequired, addActivity, validator.theme, apiV3FormValidator, async(req, res) => {
  292. const requestParams = {
  293. 'customize:theme': req.body.theme,
  294. };
  295. try {
  296. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  297. const customizedParams = {
  298. theme: await crowi.configManager.getConfig('crowi', 'customize:theme'),
  299. };
  300. customizeService.initGrowiTheme();
  301. const parameters = { action: SupportedAction.ACTION_ADMIN_THEME_UPDATE };
  302. activityEvent.emit('update', res.locals.activity._id, parameters);
  303. return res.apiv3({ customizedParams });
  304. }
  305. catch (err) {
  306. const msg = 'Error occurred in updating theme';
  307. logger.error('Error', err);
  308. return res.apiv3Err(new ErrorV3(msg, 'update-theme-failed'));
  309. }
  310. });
  311. // sidebar
  312. router.get('/sidebar', loginRequiredStrictly, adminRequired, async(req, res) => {
  313. try {
  314. const isSidebarDrawerMode = await crowi.configManager.getConfig('crowi', 'customize:isSidebarDrawerMode');
  315. const isSidebarClosedAtDockMode = await crowi.configManager.getConfig('crowi', 'customize:isSidebarClosedAtDockMode');
  316. return res.apiv3({ isSidebarDrawerMode, isSidebarClosedAtDockMode });
  317. }
  318. catch (err) {
  319. const msg = 'Error occurred in getting sidebar';
  320. logger.error('Error', err);
  321. return res.apiv3Err(new ErrorV3(msg, 'get-sidebar-failed'));
  322. }
  323. });
  324. router.put('/sidebar', loginRequiredStrictly, adminRequired, validator.sidebar, apiV3FormValidator, addActivity, async(req, res) => {
  325. const requestParams = {
  326. 'customize:isSidebarDrawerMode': req.body.isSidebarDrawerMode,
  327. 'customize:isSidebarClosedAtDockMode': req.body.isSidebarClosedAtDockMode,
  328. };
  329. try {
  330. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  331. const customizedParams = {
  332. isSidebarDrawerMode: await crowi.configManager.getConfig('crowi', 'customize:isSidebarDrawerMode'),
  333. isSidebarClosedAtDockMode: await crowi.configManager.getConfig('crowi', 'customize:isSidebarClosedAtDockMode'),
  334. };
  335. activityEvent.emit('update', res.locals.activity._id, { action: SupportedAction.ACTION_ADMIN_SIDEBAR_UPDATE });
  336. return res.apiv3({ customizedParams });
  337. }
  338. catch (err) {
  339. const msg = 'Error occurred in updating sidebar';
  340. logger.error('Error', err);
  341. return res.apiv3Err(new ErrorV3(msg, 'update-sidebar-failed'));
  342. }
  343. });
  344. /**
  345. * @swagger
  346. *
  347. * /customize-setting/function:
  348. * put:
  349. * tags: [CustomizeSetting]
  350. * operationId: updateFunctionCustomizeSetting
  351. * summary: /customize-setting/function
  352. * description: Update function
  353. * requestBody:
  354. * required: true
  355. * content:
  356. * application/json:
  357. * schema:
  358. * $ref: '#/components/schemas/CustomizeFunction'
  359. * responses:
  360. * 200:
  361. * description: Succeeded to update function
  362. * content:
  363. * application/json:
  364. * schema:
  365. * $ref: '#/components/schemas/CustomizeFunction'
  366. */
  367. router.put('/function', loginRequiredStrictly, adminRequired, addActivity, validator.function, apiV3FormValidator, async(req, res) => {
  368. const requestParams = {
  369. 'customize:isEnabledTimeline': req.body.isEnabledTimeline,
  370. 'customize:isEnabledAttachTitleHeader': req.body.isEnabledAttachTitleHeader,
  371. 'customize:showPageLimitationS': req.body.pageLimitationS,
  372. 'customize:showPageLimitationM': req.body.pageLimitationM,
  373. 'customize:showPageLimitationL': req.body.pageLimitationL,
  374. 'customize:showPageLimitationXL': req.body.pageLimitationXL,
  375. 'customize:isEnabledStaleNotification': req.body.isEnabledStaleNotification,
  376. 'customize:isAllReplyShown': req.body.isAllReplyShown,
  377. 'customize:isSearchScopeChildrenAsDefault': req.body.isSearchScopeChildrenAsDefault,
  378. };
  379. try {
  380. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  381. const customizedParams = {
  382. isEnabledTimeline: await crowi.configManager.getConfig('crowi', 'customize:isEnabledTimeline'),
  383. isEnabledAttachTitleHeader: await crowi.configManager.getConfig('crowi', 'customize:isEnabledAttachTitleHeader'),
  384. pageLimitationS: await crowi.configManager.getConfig('crowi', 'customize:showPageLimitationS'),
  385. pageLimitationM: await crowi.configManager.getConfig('crowi', 'customize:showPageLimitationM'),
  386. pageLimitationL: await crowi.configManager.getConfig('crowi', 'customize:showPageLimitationL'),
  387. pageLimitationXL: await crowi.configManager.getConfig('crowi', 'customize:showPageLimitationXL'),
  388. isEnabledStaleNotification: await crowi.configManager.getConfig('crowi', 'customize:isEnabledStaleNotification'),
  389. isAllReplyShown: await crowi.configManager.getConfig('crowi', 'customize:isAllReplyShown'),
  390. isSearchScopeChildrenAsDefault: await crowi.configManager.getConfig('crowi', 'customize:isSearchScopeChildrenAsDefault'),
  391. };
  392. const parameters = { action: SupportedAction.ACTION_ADMIN_FUNCTION_UPDATE };
  393. activityEvent.emit('update', res.locals.activity._id, parameters);
  394. return res.apiv3({ customizedParams });
  395. }
  396. catch (err) {
  397. const msg = 'Error occurred in updating function';
  398. logger.error('Error', err);
  399. return res.apiv3Err(new ErrorV3(msg, 'update-function-failed'));
  400. }
  401. });
  402. /**
  403. * @swagger
  404. *
  405. * /customize-setting/highlight:
  406. * put:
  407. * tags: [CustomizeSetting]
  408. * operationId: updateHighlightCustomizeSetting
  409. * summary: /customize-setting/highlight
  410. * description: Update highlight
  411. * requestBody:
  412. * required: true
  413. * content:
  414. * application/json:
  415. * schema:
  416. * $ref: '#/components/schemas/CustomizeHighlight'
  417. * responses:
  418. * 200:
  419. * description: Succeeded to update highlight
  420. * content:
  421. * application/json:
  422. * schema:
  423. * $ref: '#/components/schemas/CustomizeHighlight'
  424. */
  425. router.put('/highlight', loginRequiredStrictly, adminRequired, addActivity, validator.highlight, apiV3FormValidator, async(req, res) => {
  426. const requestParams = {
  427. 'customize:highlightJsStyle': req.body.highlightJsStyle,
  428. 'customize:highlightJsStyleBorder': req.body.highlightJsStyleBorder,
  429. };
  430. try {
  431. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  432. const customizedParams = {
  433. styleName: await crowi.configManager.getConfig('crowi', 'customize:highlightJsStyle'),
  434. styleBorder: await crowi.configManager.getConfig('crowi', 'customize:highlightJsStyleBorder'),
  435. };
  436. const parameters = { action: SupportedAction.ACTION_ADMIN_CODE_HIGHLIGHT_UPDATE };
  437. activityEvent.emit('update', res.locals.activity._id, parameters);
  438. return res.apiv3({ customizedParams });
  439. }
  440. catch (err) {
  441. const msg = 'Error occurred in updating highlight';
  442. logger.error('Error', err);
  443. return res.apiv3Err(new ErrorV3(msg, 'update-highlight-failed'));
  444. }
  445. });
  446. /**
  447. * @swagger
  448. *
  449. * /customize-setting/customizeTitle:
  450. * put:
  451. * tags: [CustomizeSetting]
  452. * operationId: updateCustomizeTitleCustomizeSetting
  453. * summary: /customize-setting/customizeTitle
  454. * description: Update customizeTitle
  455. * requestBody:
  456. * required: true
  457. * content:
  458. * application/json:
  459. * schema:
  460. * $ref: '#/components/schemas/CustomizeTitle'
  461. * responses:
  462. * 200:
  463. * description: Succeeded to update customizeTitle
  464. * content:
  465. * application/json:
  466. * schema:
  467. * $ref: '#/components/schemas/CustomizeTitle'
  468. */
  469. router.put('/customize-title', loginRequiredStrictly, adminRequired, addActivity, validator.customizeTitle, apiV3FormValidator, async(req, res) => {
  470. const requestParams = {
  471. 'customize:title': req.body.customizeTitle,
  472. };
  473. try {
  474. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams, true);
  475. crowi.customizeService.publishUpdatedMessage();
  476. const customizedParams = {
  477. customizeTitle: await crowi.configManager.getConfig('crowi', 'customize:title'),
  478. };
  479. customizeService.initCustomTitle();
  480. const parameters = { action: SupportedAction.ACTION_ADMIN_CUSTOM_TITLE_UPDATE };
  481. activityEvent.emit('update', res.locals.activity._id, parameters);
  482. return res.apiv3({ customizedParams });
  483. }
  484. catch (err) {
  485. const msg = 'Error occurred in updating customizeTitle';
  486. logger.error('Error', err);
  487. return res.apiv3Err(new ErrorV3(msg, 'update-customizeTitle-failed'));
  488. }
  489. });
  490. /**
  491. * @swagger
  492. *
  493. * /customize-setting/customize-noscript:
  494. * put:
  495. * tags: [CustomizeSetting]
  496. * operationId: updateCustomizeNoscriptCustomizeSetting
  497. * summary: /customize-setting/customize-noscript
  498. * description: Update customizeNoscript
  499. * requestBody:
  500. * required: true
  501. * content:
  502. * application/json:
  503. * schema:
  504. * $ref: '#/components/schemas/CustomizeNoscript'
  505. * responses:
  506. * 200:
  507. * description: Succeeded to update customize header
  508. * content:
  509. * application/json:
  510. * schema:
  511. * $ref: '#/components/schemas/CustomizeNoscript'
  512. */
  513. router.put('/customize-noscript', loginRequiredStrictly, adminRequired, addActivity, validator.customizeNoscript, apiV3FormValidator, async(req, res) => {
  514. const requestParams = {
  515. 'customize:noscript': req.body.customizeNoscript,
  516. };
  517. try {
  518. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  519. const customizedParams = {
  520. customizeNoscript: await crowi.configManager.getConfig('crowi', 'customize:noscript'),
  521. };
  522. const parameters = { action: SupportedAction.ACTION_ADMIN_CUSTOM_NOSCRIPT_UPDATE };
  523. activityEvent.emit('update', res.locals.activity._id, parameters);
  524. return res.apiv3({ customizedParams });
  525. }
  526. catch (err) {
  527. const msg = 'Error occurred in updating customizeNoscript';
  528. logger.error('Error', err);
  529. return res.apiv3Err(new ErrorV3(msg, 'update-customizeNoscript-failed'));
  530. }
  531. });
  532. /**
  533. * @swagger
  534. *
  535. * /customize-setting/customizeCss:
  536. * put:
  537. * tags: [CustomizeSetting]
  538. * operationId: updateCustomizeCssCustomizeSetting
  539. * summary: /customize-setting/customizeCss
  540. * description: Update customizeCss
  541. * requestBody:
  542. * required: true
  543. * content:
  544. * application/json:
  545. * schema:
  546. * $ref: '#/components/schemas/CustomizeCss'
  547. * responses:
  548. * 200:
  549. * description: Succeeded to update customize css
  550. * content:
  551. * application/json:
  552. * schema:
  553. * $ref: '#/components/schemas/CustomizeCss'
  554. */
  555. router.put('/customize-css', loginRequiredStrictly, adminRequired, addActivity, validator.customizeCss, apiV3FormValidator, async(req, res) => {
  556. const requestParams = {
  557. 'customize:css': req.body.customizeCss,
  558. };
  559. try {
  560. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams, true);
  561. crowi.customizeService.publishUpdatedMessage();
  562. const customizedParams = {
  563. customizeCss: await crowi.configManager.getConfig('crowi', 'customize:css'),
  564. };
  565. customizeService.initCustomCss();
  566. const parameters = { action: SupportedAction.ACTION_ADMIN_CUSTOM_CSS_UPDATE };
  567. activityEvent.emit('update', res.locals.activity._id, parameters);
  568. return res.apiv3({ customizedParams });
  569. }
  570. catch (err) {
  571. const msg = 'Error occurred in updating customizeCss';
  572. logger.error('Error', err);
  573. return res.apiv3Err(new ErrorV3(msg, 'update-customizeCss-failed'));
  574. }
  575. });
  576. /**
  577. * @swagger
  578. *
  579. * /customize-setting/customizeScript:
  580. * put:
  581. * tags: [CustomizeSetting]
  582. * operationId: updateCustomizeScriptCustomizeSetting
  583. * summary: /customize-setting/customizeScript
  584. * description: Update customizeScript
  585. * requestBody:
  586. * required: true
  587. * content:
  588. * application/json:
  589. * schema:
  590. * $ref: '#/components/schemas/CustomizeScript'
  591. * responses:
  592. * 200:
  593. * description: Succeeded to update customize script
  594. * content:
  595. * application/json:
  596. * schema:
  597. * $ref: '#/components/schemas/CustomizeScript'
  598. */
  599. router.put('/customize-script', loginRequiredStrictly, adminRequired, addActivity, validator.customizeScript, apiV3FormValidator, async(req, res) => {
  600. const requestParams = {
  601. 'customize:script': req.body.customizeScript,
  602. };
  603. try {
  604. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  605. const customizedParams = {
  606. customizeScript: await crowi.configManager.getConfig('crowi', 'customize:script'),
  607. };
  608. const parameters = { action: SupportedAction.ACTION_ADMIN_CUSTOM_SCRIPT_UPDATE };
  609. activityEvent.emit('update', res.locals.activity._id, parameters);
  610. return res.apiv3({ customizedParams });
  611. }
  612. catch (err) {
  613. const msg = 'Error occurred in updating customizeScript';
  614. logger.error('Error', err);
  615. return res.apiv3Err(new ErrorV3(msg, 'update-customizeScript-failed'));
  616. }
  617. });
  618. router.put('/customize-logo', loginRequiredStrictly, adminRequired, validator.logo, apiV3FormValidator, async(req, res) => {
  619. const {
  620. isDefaultLogo,
  621. } = req.body;
  622. const requestParams = {
  623. 'customize:isDefaultLogo': isDefaultLogo,
  624. };
  625. try {
  626. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  627. const customizedParams = {
  628. isDefaultLogo: await crowi.configManager.getConfig('crowi', 'customize:isDefaultLogo'),
  629. };
  630. return res.apiv3({ customizedParams });
  631. }
  632. catch (err) {
  633. const msg = 'Error occurred in updating customizeLogo';
  634. logger.error('Error', err);
  635. return res.apiv3Err(new ErrorV3(msg, 'update-customizeLogo-failed'));
  636. }
  637. });
  638. router.post('/upload-brand-logo', uploads.single('file'), loginRequiredStrictly,
  639. adminRequired, validator.logo, apiV3FormValidator, async(req, res) => {
  640. if (req.file == null) {
  641. return res.apiv3Err(new ErrorV3('File error.', 'upload-brand-logo-failed'));
  642. }
  643. if (req.user == null) {
  644. return res.apiv3Err(new ErrorV3('param "user" must be set.', 'upload-brand-logo-failed'));
  645. }
  646. const file = req.file;
  647. // check type
  648. const acceptableFileType = /image\/.+/;
  649. if (!file.mimetype.match(acceptableFileType)) {
  650. const msg = 'File type error. Only image files is allowed to set as user picture.';
  651. return res.apiv3Err(new ErrorV3(msg, 'upload-brand-logo-failed'));
  652. }
  653. // Check if previous attachment exists and remove it
  654. const attachments = await Attachment.find({ attachmentType: AttachmentType.BRAND_LOGO });
  655. if (attachments != null) {
  656. await attachmentService.removeAllAttachments(attachments);
  657. }
  658. let attachment;
  659. try {
  660. attachment = await attachmentService.createAttachment(file, req.user, null, AttachmentType.BRAND_LOGO);
  661. }
  662. catch (err) {
  663. logger.error(err);
  664. return res.apiv3Err(new ErrorV3(err.message, 'upload-brand-logo-failed'));
  665. }
  666. attachment.toObject({ virtuals: true });
  667. return res.apiv3({ attachment });
  668. });
  669. router.delete('/delete-brand-logo', loginRequiredStrictly, adminRequired, async(req, res) => {
  670. const attachments = await Attachment.find({ attachmentType: AttachmentType.BRAND_LOGO });
  671. if (attachments == null) {
  672. return res.apiv3Err(new ErrorV3('attachment not found', 'delete-brand-logo-failed'));
  673. }
  674. try {
  675. await attachmentService.removeAllAttachments(attachments);
  676. }
  677. catch (err) {
  678. logger.error(err);
  679. return res.status(500).apiv3Err(new ErrorV3('Error while deleting logo', 'delete-brand-logo-failed'));
  680. }
  681. return res.apiv3({});
  682. });
  683. return router;
  684. };