AdminGeneralSecurityContainer.js 5.8 KB

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