AdminGeneralSecurityContainer.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. import { Container } from 'unstated';
  2. import { toastError } from '../util/apiNotification';
  3. import removeNullPropertyFromObject from '../../../lib/util/removeNullPropertyFromObject';
  4. /**
  5. * Service container for admin security page (SecuritySetting.jsx)
  6. * @extends {Container} unstated Container
  7. */
  8. export default class AdminGeneralSecurityContainer extends Container {
  9. constructor(appContainer) {
  10. super();
  11. this.appContainer = appContainer;
  12. this.dummyCurrentRestrictGuestMode = 0;
  13. this.dummyCurrentRestrictGuestModeForError = 1;
  14. this.state = {
  15. retrieveError: null,
  16. sessionMaxAge: null,
  17. wikiMode: '',
  18. // set dummy value tile for using suspense
  19. currentRestrictGuestMode: this.dummyCurrentRestrictGuestMode,
  20. currentPageCompleteDeletionAuthority: 'adminOnly',
  21. isShowRestrictedByOwner: false,
  22. isShowRestrictedByGroup: false,
  23. appSiteUrl: appContainer.config.crowi.url || '',
  24. isLocalEnabled: false,
  25. isLdapEnabled: false,
  26. isSamlEnabled: false,
  27. isOidcEnabled: false,
  28. isBasicEnabled: false,
  29. isGoogleEnabled: false,
  30. isGitHubEnabled: false,
  31. isTwitterEnabled: false,
  32. setupStrategies: [],
  33. disableLinkSharing: false,
  34. shareLinks: [],
  35. totalshareLinks: 0,
  36. shareLinksPagingLimit: Infinity,
  37. shareLinksActivePage: 1,
  38. };
  39. }
  40. async retrieveSecurityData() {
  41. await this.retrieveSetupStratedies();
  42. const response = await this.appContainer.apiv3.get('/security-setting/');
  43. const { generalSetting, generalAuth } = response.data.securityParams;
  44. this.setState({
  45. currentRestrictGuestMode: generalSetting.restrictGuestMode,
  46. currentPageCompleteDeletionAuthority: generalSetting.pageCompleteDeletionAuthority,
  47. isShowRestrictedByOwner: !generalSetting.hideRestrictedByOwner,
  48. isShowRestrictedByGroup: !generalSetting.hideRestrictedByGroup,
  49. sessionMaxAge: generalSetting.sessionMaxAge,
  50. wikiMode: generalSetting.wikiMode,
  51. disableLinkSharing: generalSetting.disableLinkSharing,
  52. isLocalEnabled: generalAuth.isLocalEnabled,
  53. isLdapEnabled: generalAuth.isLdapEnabled,
  54. isSamlEnabled: generalAuth.isSamlEnabled,
  55. isOidcEnabled: generalAuth.isOidcEnabled,
  56. isBasicEnabled: generalAuth.isBasicEnabled,
  57. isGoogleEnabled: generalAuth.isGoogleEnabled,
  58. isGitHubEnabled: generalAuth.isGitHubEnabled,
  59. isTwitterEnabled: generalAuth.isTwitterEnabled,
  60. });
  61. }
  62. /**
  63. * Workaround for the mangling in production build to break constructor.name
  64. */
  65. static getClassName() {
  66. return 'AdminGeneralSecurityContainer';
  67. }
  68. /**
  69. * get isWikiModeForced
  70. * @return {bool} isWikiModeForced
  71. */
  72. get isWikiModeForced() {
  73. return this.state.wikiMode === 'public' || this.state.wikiMode === 'private';
  74. }
  75. /**
  76. * setter for sessionMaxAge
  77. */
  78. setSessionMaxAge(sessionMaxAge) {
  79. this.setState({ sessionMaxAge });
  80. }
  81. /**
  82. * setter for disableLinkSharing
  83. */
  84. setDisableLinkSharing(disableLinkSharing) {
  85. this.setState({ disableLinkSharing });
  86. }
  87. /**
  88. * Change restrictGuestMode
  89. */
  90. changeRestrictGuestMode(restrictGuestModeLabel) {
  91. this.setState({ currentRestrictGuestMode: restrictGuestModeLabel });
  92. }
  93. /**
  94. * Change pageCompleteDeletionAuthority
  95. */
  96. changePageCompleteDeletionAuthority(pageCompleteDeletionAuthorityLabel) {
  97. this.setState({ currentPageCompleteDeletionAuthority: pageCompleteDeletionAuthorityLabel });
  98. }
  99. /**
  100. * Switch showRestrictedByOwner
  101. */
  102. switchIsShowRestrictedByOwner() {
  103. this.setState({ isShowRestrictedByOwner: !this.state.isShowRestrictedByOwner });
  104. }
  105. /**
  106. * Switch showRestrictedByGroup
  107. */
  108. switchIsShowRestrictedByGroup() {
  109. this.setState({ isShowRestrictedByGroup: !this.state.isShowRestrictedByGroup });
  110. }
  111. /**
  112. * Update restrictGuestMode
  113. * @memberOf AdminGeneralSecuritySContainer
  114. * @return {string} Appearance
  115. */
  116. async updateGeneralSecuritySetting() {
  117. let requestParams = {
  118. sessionMaxAge: this.state.sessionMaxAge,
  119. restrictGuestMode: this.state.currentRestrictGuestMode,
  120. pageCompleteDeletionAuthority: this.state.currentPageCompleteDeletionAuthority,
  121. hideRestrictedByGroup: !this.state.isShowRestrictedByGroup,
  122. hideRestrictedByOwner: !this.state.isShowRestrictedByOwner,
  123. };
  124. requestParams = await removeNullPropertyFromObject(requestParams);
  125. const response = await this.appContainer.apiv3.put('/security-setting/general-setting', requestParams);
  126. const { securitySettingParams } = response.data;
  127. return securitySettingParams;
  128. }
  129. /**
  130. * Update Share Link Settings
  131. */
  132. async updateShareLinkSetting() {
  133. let requestParams = {
  134. disableLinkSharing: this.state.disableLinkSharing,
  135. };
  136. requestParams = await removeNullPropertyFromObject(requestParams);
  137. const response = await this.appContainer.apiv3.put('/security-setting/general-setting', requestParams);
  138. const { securitySettingParams } = response.data;
  139. return securitySettingParams;
  140. }
  141. /**
  142. * Switch authentication
  143. */
  144. async switchAuthentication(stateVariableName, authId) {
  145. const isEnabled = !this.state[stateVariableName];
  146. try {
  147. await this.appContainer.apiv3.put('/security-setting/authentication/enabled', {
  148. isEnabled,
  149. authId,
  150. });
  151. await this.retrieveSetupStratedies();
  152. this.setState({ [stateVariableName]: isEnabled });
  153. }
  154. catch (err) {
  155. toastError(err);
  156. }
  157. }
  158. /**
  159. * Retrieve SetupStratedies
  160. */
  161. async retrieveSetupStratedies() {
  162. try {
  163. const response = await this.appContainer.apiv3.get('/security-setting/authentication');
  164. const { setupStrategies } = response.data;
  165. this.setState({ setupStrategies });
  166. }
  167. catch (err) {
  168. toastError(err);
  169. }
  170. }
  171. /**
  172. * Retrieve All Sharelinks
  173. */
  174. async retrieveShareLinksByPagingNum(page) {
  175. const params = {
  176. page,
  177. };
  178. const { data } = await this.appContainer.apiv3.get('/security-setting/all-share-links', params);
  179. if (data.paginateResult == null) {
  180. throw new Error('data must conclude \'paginateResult\' property.');
  181. }
  182. const { docs: shareLinks, totalDocs: totalshareLinks, limit: shareLinksPagingLimit } = data.paginateResult;
  183. this.setState({
  184. shareLinks,
  185. totalshareLinks,
  186. shareLinksPagingLimit,
  187. shareLinksActivePage: page,
  188. });
  189. }
  190. /**
  191. * Switch local enabled
  192. */
  193. async switchIsLocalEnabled() {
  194. this.switchAuthentication('isLocalEnabled', 'local');
  195. }
  196. /**
  197. * Switch LDAP enabled
  198. */
  199. async switchIsLdapEnabled() {
  200. this.switchAuthentication('isLdapEnabled', 'ldap');
  201. }
  202. /**
  203. * Switch SAML enabled
  204. */
  205. async switchIsSamlEnabled() {
  206. this.switchAuthentication('isSamlEnabled', 'saml');
  207. }
  208. /**
  209. * Switch Oidc enabled
  210. */
  211. async switchIsOidcEnabled() {
  212. this.switchAuthentication('isOidcEnabled', 'oidc');
  213. }
  214. /**
  215. * Switch Basic enabled
  216. */
  217. async switchIsBasicEnabled() {
  218. this.switchAuthentication('isBasicEnabled', 'basic');
  219. }
  220. /**
  221. * Switch GoogleOAuth enabled
  222. */
  223. async switchIsGoogleOAuthEnabled() {
  224. this.switchAuthentication('isGoogleEnabled', 'google');
  225. }
  226. /**
  227. * Switch GitHubOAuth enabled
  228. */
  229. async switchIsGitHubOAuthEnabled() {
  230. this.switchAuthentication('isGitHubEnabled', 'github');
  231. }
  232. /**
  233. * Switch TwitterOAuth enabled
  234. */
  235. async switchIsTwitterOAuthEnabled() {
  236. this.switchAuthentication('isTwitterEnabled', 'twitter');
  237. }
  238. }