AdminGeneralSecurityContainer.js 5.4 KB

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