security-setting.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. generalSetting: [
  12. body('restrictGuestMode').isString(),
  13. body('pageCompleteDeletionAuthority').isString(),
  14. body('hideRestrictedByOwner').isBoolean(),
  15. body('hideRestrictedByGroup').isBoolean(),
  16. ],
  17. twitterOAuth: [
  18. body('twitterConsumerKey').isString(),
  19. body('twitterConsumerSecret').isString(),
  20. body('isSameUsernameTreatedAsIdenticalUser').isBoolean(),
  21. ],
  22. };
  23. /**
  24. * @swagger
  25. * tags:
  26. * name: SecuritySetting
  27. */
  28. /**
  29. * @swagger
  30. *
  31. * components:
  32. * schemas:
  33. * SecurityParams:
  34. * type: object
  35. * GeneralSetting:
  36. * type:object
  37. * GuestModeParams:
  38. * type: object
  39. * properties:
  40. * restrictGuestMode:
  41. * type: string
  42. * description: type of restrictGuestMode
  43. * PageDeletionParams:
  44. * type: object
  45. * properties:
  46. * pageCompleteDeletionAuthority:
  47. * type: string
  48. * description: type of pageDeletionAuthority
  49. * Function:
  50. * type: object
  51. * properties:
  52. * hideRestrictedByOwner:
  53. * type: boolean
  54. * description: enable hide by owner
  55. * hideRestrictedByGroup:
  56. * type: boolean
  57. * description: enable hide by group
  58. * TwitterOAuthSetting:
  59. * type:object
  60. * consumerKey:
  61. * type: string
  62. * description: key of comsumer
  63. * consumerSecret:
  64. * type: string
  65. * description: password of comsumer
  66. * isSameUsernameTreatedAsIdenticalUser
  67. * type: boolean
  68. * description: local account automatically linked the email matched
  69. */
  70. module.exports = (crowi) => {
  71. const loginRequiredStrictly = require('../../middleware/login-required')(crowi);
  72. const adminRequired = require('../../middleware/admin-required')(crowi);
  73. const csrf = require('../../middleware/csrf')(crowi);
  74. const { ApiV3FormValidator } = crowi.middlewares;
  75. /**
  76. * @swagger
  77. *
  78. * /security-setting/:
  79. * get:
  80. * tags: [SecuritySetting]
  81. * description: Get security paramators
  82. * responses:
  83. * 200:
  84. * description: params of security
  85. * content:
  86. * application/json:
  87. * schema:
  88. * properties:
  89. * securityParams:
  90. * $ref: '#/components/schemas/SecurityParams'
  91. */
  92. router.get('/', loginRequiredStrictly, adminRequired, async(req, res) => {
  93. const securityParams = {
  94. generalAuth: {
  95. isTwitterOAuthEnabled: await crowi.configManager.getConfig('crowi', 'security:passport-twitter:isEnabled'),
  96. },
  97. twitterOAuth: {
  98. twitterConsumerKey: await crowi.configManager.getConfig('crowi', 'security:passport-twitter:consumerKey'),
  99. twitterConsumerSecret: await crowi.configManager.getConfig('crowi', 'security:passport-twitter:consumerSecret'),
  100. isSameUsernameTreatedAsIdenticalUser: await crowi.configManager.getConfig('crowi', 'security:passport-twitter:isSameUsernameTreatedAsIdenticalUser'),
  101. },
  102. };
  103. return res.apiv3({ securityParams });
  104. });
  105. /**
  106. * @swagger
  107. *
  108. * /security-setting/general-setting:
  109. * put:
  110. * tags: [SecuritySetting]
  111. * description: Update GeneralSetting
  112. * requestBody:
  113. * required: true
  114. * content:
  115. * application/json:
  116. * schema:
  117. * type: object
  118. * properties:
  119. * restrictGuestMode:
  120. * description: type of restrictGuestMode
  121. * type: string
  122. * pageCompleteDeletionAuthority:
  123. * type: string
  124. * description: type of pageDeletionAuthority
  125. * hideRestrictedByOwner:
  126. * type: boolean
  127. * description: enable hide by owner
  128. * hideRestrictedByGroup:
  129. * type: boolean
  130. * description: enable hide by group
  131. * responses:
  132. * 200:
  133. * description: Succeeded to update general Setting
  134. * content:
  135. * application/json:
  136. * schema:
  137. * properties:
  138. * status:
  139. * $ref: '#/components/schemas/SecurityParams/GeneralSetting'
  140. */
  141. router.put('/general-setting', loginRequiredStrictly, adminRequired, csrf, validator.generalSetting, ApiV3FormValidator, async(req, res) => {
  142. const requestParams = {
  143. 'security:restrictGuestMode': req.body.restrictGuestMode,
  144. 'security:pageCompleteDeletionAuthority': req.body.pageCompleteDeletionAuthority,
  145. 'security:list-policy:hideRestrictedByOwner': req.body.hideRestrictedByOwner,
  146. 'security:list-policy:hideRestrictedByGroup': req.body.hideRestrictedByGroup,
  147. };
  148. try {
  149. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  150. const securitySettingParams = {
  151. restrictGuestMode: await crowi.configManager.getConfig('crowi', 'security:restrictGuestMode'),
  152. pageCompleteDeletionAuthority: await crowi.configManager.getConfig('crowi', 'security:pageCompleteDeletionAuthority'),
  153. hideRestrictedByOwner: await crowi.configManager.getConfig('crowi', 'security:list-policy:hideRestrictedByOwner'),
  154. hideRestrictedByGroup: await crowi.configManager.getConfig('crowi', 'security:list-policy:hideRestrictedByGroup'),
  155. };
  156. return res.apiv3({ securitySettingParams });
  157. }
  158. catch (err) {
  159. const msg = 'Error occurred in updating security setting';
  160. logger.error('Error', err);
  161. return res.apiv3Err(new ErrorV3(msg, 'update-secuirty-setting failed'));
  162. }
  163. });
  164. /**
  165. * @swagger
  166. *
  167. * /security-setting/twitter-oauth:
  168. * put:
  169. * tags: [SecuritySetting]
  170. * description: Update twitter OAuth
  171. * requestBody:
  172. * required: true
  173. * content:
  174. * application/json:
  175. * schema:
  176. * $ref: '#/components/schemas/SecurityParams/TwitterOAuthSetting'
  177. * responses:
  178. * 200:
  179. * description: Succeeded to update function
  180. * content:
  181. * application/json:
  182. * schema:
  183. * $ref: '#/components/schemas/SecurityParams/TwitterOAuthSetting'
  184. */
  185. router.put('/twitter-oauth', loginRequiredStrictly, adminRequired, csrf, validator.twitterOAuth, ApiV3FormValidator, async(req, res) => {
  186. const requestParams = {
  187. 'security:passport-twitter:consumerKey': req.body.twitterConsumerKey,
  188. 'security:passport-twitter:consumerSecret': req.body.twitterConsumerSecret,
  189. 'security:passport-twitter:isSameUsernameTreatedAsIdenticalUser': req.body.isSameUsernameTreatedAsIdenticalUser,
  190. };
  191. try {
  192. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  193. const securitySettingParams = {
  194. twitterConsumerId: await crowi.configManager.getConfig('crowi', 'security:passport-twitter:consumerKey'),
  195. twitterConsumerSecret: await crowi.configManager.getConfig('crowi', 'security:passport-twitter:consumerSecret'),
  196. isSameUsernameTreatedAsIdenticalUser: await crowi.configManager.getConfig('crowi', 'security:passport-twitter:isSameUsernameTreatedAsIdenticalUser'),
  197. };
  198. return res.apiv3({ securitySettingParams });
  199. }
  200. catch (err) {
  201. const msg = 'Error occurred in updating twitterOAuth';
  202. logger.error('Error', err);
  203. return res.apiv3Err(new ErrorV3(msg, 'update-twitterOAuth-failed'));
  204. }
  205. });
  206. return router;
  207. };