AdminGeneralSecurityContainer.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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.state = {
  13. wikiMode: '',
  14. currentRestrictGuestMode: 'Deny',
  15. currentPageCompleteDeletionAuthority: 'adminOnly',
  16. isShowRestrictedByOwner: false,
  17. isShowRestrictedByGroup: false,
  18. appSiteUrl: appContainer.config.crowi.url || '',
  19. isLocalEnabled: false,
  20. isLdapEnabled: false,
  21. isSamlEnabled: false,
  22. isOidcEnabled: false,
  23. isBasicEnabled: false,
  24. isGoogleEnabled: false,
  25. isGitHubEnabled: false,
  26. isTwitterEnabled: false,
  27. setupStrategies: [],
  28. };
  29. }
  30. async retrieveSecurityData() {
  31. await this.retrieveSetupStratedies();
  32. const response = await this.appContainer.apiv3.get('/security-setting/');
  33. const { generalSetting, generalAuth } = response.data.securityParams;
  34. this.setState({
  35. currentRestrictGuestMode: generalSetting.restrictGuestMode,
  36. currentPageCompleteDeletionAuthority: generalSetting.pageCompleteDeletionAuthority,
  37. isShowRestrictedByOwner: !generalSetting.hideRestrictedByOwner,
  38. isShowRestrictedByGroup: !generalSetting.hideRestrictedByGroup,
  39. wikiMode: generalSetting.wikiMode,
  40. isLocalEnabled: generalAuth.isLocalEnabled,
  41. isLdapEnabled: generalAuth.isLdapEnabled,
  42. isSamlEnabled: generalAuth.isSamlEnabled,
  43. isOidcEnabled: generalAuth.isOidcEnabled,
  44. isBasicEnabled: generalAuth.isBasicEnabled,
  45. isGoogleEnabled: generalAuth.isGoogleEnabled,
  46. isGitHubEnabled: generalAuth.isGitHubEnabled,
  47. isTwitterEnabled: generalAuth.isTwitterEnabled,
  48. });
  49. }
  50. /**
  51. * Workaround for the mangling in production build to break constructor.name
  52. */
  53. static getClassName() {
  54. return 'AdminGeneralSecurityContainer';
  55. }
  56. /**
  57. * get isWikiModeForced
  58. * @return {bool} isWikiModeForced
  59. */
  60. get isWikiModeForced() {
  61. return this.state.wikiMode === 'public' || this.state.wikiMode === 'private';
  62. }
  63. /**
  64. * Change restrictGuestMode
  65. */
  66. changeRestrictGuestMode(restrictGuestModeLabel) {
  67. this.setState({ currentRestrictGuestMode: restrictGuestModeLabel });
  68. }
  69. /**
  70. * Change pageCompleteDeletionAuthority
  71. */
  72. changePageCompleteDeletionAuthority(pageCompleteDeletionAuthorityLabel) {
  73. this.setState({ currentPageCompleteDeletionAuthority: pageCompleteDeletionAuthorityLabel });
  74. }
  75. /**
  76. * Switch showRestrictedByOwner
  77. */
  78. switchIsShowRestrictedByOwner() {
  79. this.setState({ isShowRestrictedByOwner: !this.state.isShowRestrictedByOwner });
  80. }
  81. /**
  82. * Switch showRestrictedByGroup
  83. */
  84. switchIsShowRestrictedByGroup() {
  85. this.setState({ isShowRestrictedByGroup: !this.state.isShowRestrictedByGroup });
  86. }
  87. /**
  88. * Update restrictGuestMode
  89. * @memberOf AdminGeneralSecuritySContainer
  90. * @return {string} Appearance
  91. */
  92. async updateGeneralSecuritySetting() {
  93. let requestParams = {
  94. restrictGuestMode: this.state.currentRestrictGuestMode,
  95. pageCompleteDeletionAuthority: this.state.currentPageCompleteDeletionAuthority,
  96. hideRestrictedByGroup: !this.state.isShowRestrictedByGroup,
  97. hideRestrictedByOwner: !this.state.isShowRestrictedByOwner,
  98. };
  99. requestParams = await removeNullPropertyFromObject(requestParams);
  100. const response = await this.appContainer.apiv3.put('/security-setting/general-setting', requestParams);
  101. const { securitySettingParams } = response.data;
  102. return securitySettingParams;
  103. }
  104. /**
  105. * Switch authentication
  106. */
  107. async switchAuthentication(stateVariableName, authId) {
  108. const isEnabled = !this.state[stateVariableName];
  109. try {
  110. await this.appContainer.apiv3.put('/security-setting/authentication/enabled', {
  111. isEnabled,
  112. authId,
  113. });
  114. await this.retrieveSetupStratedies();
  115. this.setState({ [stateVariableName]: isEnabled });
  116. }
  117. catch (err) {
  118. toastError(err);
  119. }
  120. }
  121. /**
  122. * Retrieve SetupStratedies
  123. */
  124. async retrieveSetupStratedies() {
  125. try {
  126. const response = await this.appContainer.apiv3.get('/security-setting/authentication');
  127. const { setupStrategies } = response.data;
  128. this.setState({ setupStrategies });
  129. }
  130. catch (err) {
  131. toastError(err);
  132. }
  133. }
  134. /**
  135. * Switch local enabled
  136. */
  137. async switchIsLocalEnabled() {
  138. this.switchAuthentication('isLocalEnabled', 'local');
  139. }
  140. /**
  141. * Switch LDAP enabled
  142. */
  143. async switchIsLdapEnabled() {
  144. this.switchAuthentication('isLdapEnabled', 'ldap');
  145. }
  146. /**
  147. * Switch SAML enabled
  148. */
  149. async switchIsSamlEnabled() {
  150. this.switchAuthentication('isSamlEnabled', 'saml');
  151. }
  152. /**
  153. * Switch Oidc enabled
  154. */
  155. async switchIsOidcEnabled() {
  156. this.switchAuthentication('isOidcEnabled', 'oidc');
  157. }
  158. /**
  159. * Switch Basic enabled
  160. */
  161. async switchIsBasicEnabled() {
  162. this.switchAuthentication('isBasicEnabled', 'basic');
  163. }
  164. /**
  165. * Switch GoogleOAuth enabled
  166. */
  167. async switchIsGoogleOAuthEnabled() {
  168. this.switchAuthentication('isGoogleEnabled', 'google');
  169. }
  170. /**
  171. * Switch GitHubOAuth enabled
  172. */
  173. async switchIsGitHubOAuthEnabled() {
  174. this.switchAuthentication('isGitHubEnabled', 'github');
  175. }
  176. /**
  177. * Switch TwitterOAuth enabled
  178. */
  179. async switchIsTwitterOAuthEnabled() {
  180. this.switchAuthentication('isTwitterEnabled', 'twitter');
  181. }
  182. }