AdminGeneralSecurityContainer.js 5.5 KB

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