security-setting.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* eslint-disable max-len */
  2. /* eslint-disable no-unused-vars */
  3. const loggerFactory = require('@alias/logger');
  4. const logger = loggerFactory('growi:routes:apiv3:security-setting');
  5. const express = require('express');
  6. const router = express.Router();
  7. const { body } = require('express-validator/check');
  8. const ErrorV3 = require('../../models/vo/error-apiv3');
  9. const validator = {
  10. // TODO correct validator
  11. guestMode: [
  12. body('restrictGuestMode').isString(),
  13. ],
  14. pageDeletion: [
  15. body('pageCompleteDeletionAuthority').isString(),
  16. ],
  17. function: [
  18. body('hideRestrictedByOwner').isBoolean(),
  19. body('hideRestrictedByGroup').isBoolean(),
  20. ],
  21. };
  22. /**
  23. * @swagger
  24. * tags:
  25. * name: SecuritySetting
  26. */
  27. /**
  28. * @swagger
  29. *
  30. * components:
  31. * schemas:
  32. * GuestModeParams:
  33. * type: object
  34. * properties:
  35. * restrictGuestMode:
  36. * type: string
  37. * description: type of restrictGuestMode
  38. * PageDeletionParams:
  39. * type: object
  40. * properties:
  41. * pageCompleteDeletionAuthority:
  42. * type: string
  43. * description: type of pageDeletionAuthority
  44. * HideParams:
  45. * type: object
  46. * properties:
  47. * hideRestrictedByOwner:
  48. * type: boolean
  49. * description: enable hide by owner
  50. * hideRestrictedByGroup:
  51. * type: boolean
  52. * description: enable hide by group
  53. */
  54. module.exports = (crowi) => {
  55. const loginRequiredStrictly = require('../../middleware/login-required')(crowi);
  56. const adminRequired = require('../../middleware/admin-required')(crowi);
  57. const csrf = require('../../middleware/csrf')(crowi);
  58. const { ApiV3FormValidator } = crowi.middlewares;
  59. /**
  60. * @swagger
  61. *
  62. * /security-setting/:
  63. * get:
  64. * tags: [SecuritySetting]
  65. * description: Get security paramators
  66. * responses:
  67. * 200:
  68. * description: params of security
  69. * content:
  70. * application/json:
  71. * schema:
  72. * properties:
  73. * securityParams:
  74. * $ref: '#/components/schemas/SecurityParams'
  75. */
  76. router.get('/', loginRequiredStrictly, adminRequired, async(req, res) => {
  77. const securityParams = {
  78. general: {
  79. isTwitterOAuthEnabled: await crowi.configManager.getConfig('crowi', 'security:passport-twitter:isEnabled'),
  80. },
  81. twitterOAuth: {
  82. twitterConsumerId: await crowi.configManager.getConfig('crowi', 'settingForm[security:passport-twitter:consumerKey') || '',
  83. twitterConsumerSecret: await crowi.configManager.getConfig('crowi', 'settingForm[security:passport-twitter:consumerSecret') || '',
  84. isSameUsernameTreatedAsIdenticalUser: await crowi.configManager.getConfig('crowi', 'settingForm[security:passport-twitter:isSameUsernameTreatedAsIdenticalUser') || false,
  85. },
  86. };
  87. return res.apiv3({ securityParams });
  88. });
  89. /**
  90. * @swagger
  91. *
  92. * /security-setting/guest-mode:
  93. * put:
  94. * tags: [SecuritySetting]
  95. * description: Update restrictGuestMode
  96. * requestBody:
  97. * required: true
  98. * content:
  99. * application/json:
  100. * schema:
  101. * type: object
  102. * properties:
  103. * restrictGuestMode:
  104. * description: type of restrictGuestMode
  105. * type: string
  106. * responses:
  107. * 200:
  108. * description: Succeeded to update restrictGuestMode
  109. * content:
  110. * application/json:
  111. * schema:
  112. * properties:
  113. * status:
  114. * $ref: '#/components/schemas/GuestModeParams'
  115. */
  116. router.put('/guest-mode', loginRequiredStrictly, adminRequired, csrf, validator.guestMode, ApiV3FormValidator, async(req, res) => {
  117. const requestParams = {
  118. 'security:restrictGuestMode': req.body.restrictGuestMode,
  119. };
  120. try {
  121. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  122. const securitySettingParams = {
  123. restrictGuestMode: await crowi.configManager.getConfig('crowi', 'security:restrictGuestMode'),
  124. };
  125. return res.apiv3({ securitySettingParams });
  126. }
  127. catch (err) {
  128. const msg = 'Error occurred in updating restrict guest mode';
  129. logger.error('Error', err);
  130. return res.apiv3Err(new ErrorV3(msg, 'update-restrictGuestMode-failed'));
  131. }
  132. });
  133. /**
  134. * @swagger
  135. *
  136. * /security-setting/page-deletion:
  137. * put:
  138. * tags: [SecuritySetting]
  139. * description: Update pageDeletion Setting
  140. * requestBody:
  141. * required: true
  142. * content:
  143. * application/json:
  144. * schema:
  145. * type: object
  146. * properties:
  147. * pageCompleteDeletionAuthority:
  148. * description: type of pageCompleteDeletionAuthority
  149. * type: string
  150. * responses:
  151. * 200:
  152. * description: Succeeded to update pageDeletion
  153. * content:
  154. * application/json:
  155. * schema:
  156. * properties:
  157. * status:
  158. * $ref: '#/components/schemas/PageDeletionParams'
  159. */
  160. router.put('/page-deletion', loginRequiredStrictly, adminRequired, csrf, validator.pageDeletion, ApiV3FormValidator, async(req, res) => {
  161. const requestParams = {
  162. 'security:pageCompleteDeletionAuthority': req.body.pageCompleteDeletionAuthority,
  163. };
  164. try {
  165. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  166. const securitySettingParams = {
  167. pageCompleteDeletionAuthority: await crowi.configManager.getConfig('crowi', 'security:pageCompleteDeletionAuthority'),
  168. };
  169. return res.apiv3({ securitySettingParams });
  170. }
  171. catch (err) {
  172. const msg = 'Error occurred in updating page-deletion-setting';
  173. logger.error('Error', err);
  174. return res.apiv3Err(new ErrorV3(msg, 'update-page-deletion-setting-failed'));
  175. }
  176. });
  177. /**
  178. * @swagger
  179. *
  180. * /security-setting/function:
  181. * put:
  182. * tags: [SecuritySetting]
  183. * description: Update function
  184. * requestBody:
  185. * required: true
  186. * content:
  187. * application/json:
  188. * schema:
  189. * type: object
  190. * properties:
  191. * hideRestrictedByOwner:
  192. * description: is enabled hideRestrictedByOwner
  193. * type: boolean
  194. * ihideRestrictedByGroup:
  195. * description: is enabled hideRestrictedBygroup
  196. * type: boolean
  197. * responses:
  198. * 200:
  199. * description: Succeeded to update function
  200. * content:
  201. * application/json:
  202. * schema:
  203. * properties:
  204. * status:
  205. * $ref: '#/components/schemas/HideParams'
  206. */
  207. router.put('/function', loginRequiredStrictly, adminRequired, csrf, validator.function, ApiV3FormValidator, async(req, res) => {
  208. const requestParams = {
  209. 'security:list-policy:hideRestrictedByOwner': req.body.hideRestrictedByOwner,
  210. 'security:list-policy:hideRestrictedByGroup': req.body.hideRestrictedByGroup,
  211. };
  212. try {
  213. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  214. const securitySettingParams = {
  215. hideRestrictedByOwner: await crowi.configManager.getConfig('crowi', 'security:list-policy:hideRestrictedByOwner'),
  216. hideRestrictedByGroup: await crowi.configManager.getConfig('crowi', 'customize:security:list-policy:hideRestrictedByGroup'),
  217. };
  218. return res.apiv3({ securitySettingParams });
  219. }
  220. catch (err) {
  221. const msg = 'Error occurred in updating function';
  222. logger.error('Error', err);
  223. return res.apiv3Err(new ErrorV3(msg, 'update-function-failed'));
  224. }
  225. });
  226. return router;
  227. };