AdminGeneralSecurityContainer.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import { Container } from 'unstated';
  2. import loggerFactory from '@alias/logger';
  3. // eslint-disable-next-line no-unused-vars
  4. const logger = loggerFactory('growi:security:AdminGeneralSecurityContainer');
  5. /**
  6. * Service container for admin security page (SecuritySetting.jsx)
  7. * @extends {Container} unstated Container
  8. */
  9. export default class AdminGeneralSecurityContainer extends Container {
  10. constructor(appContainer) {
  11. super();
  12. this.appContainer = appContainer;
  13. this.state = {
  14. isWikiModeForced: false,
  15. wikiMode: '',
  16. currentRestrictGuestMode: 'deny',
  17. currentPageCompleteDeletionAuthority: 'anyone',
  18. isHideRestrictedByOwner: true,
  19. isHideRestrictedByGroup: true,
  20. useOnlyEnvVarsForSomeOptions: true,
  21. appSiteUrl: appContainer.config.crowi.url || '',
  22. isLocalEnabled: true,
  23. registrationMode: 'open',
  24. registrationWhiteList: '',
  25. isLdapEnabled: true,
  26. isSamlEnabled: true,
  27. isOidcEnabled: true,
  28. isBasicEnabled: true,
  29. isGoogleOAuthEnabled: true,
  30. isGithubOAuthEnabled: true,
  31. isTwitterOAuthEnabled: true,
  32. };
  33. this.onIsWikiModeForced = this.onIsWikiModeForced.bind(this);
  34. }
  35. async retrieveSecurityData() {
  36. const response = await this.appContainer.apiv3.get('/security-setting/');
  37. const { generalSetting } = response.data.securityParams;
  38. const { localSetting } = response.data.securityParams;
  39. this.onIsWikiModeForced(generalSetting.wikiMode);
  40. this.setState({
  41. currentRestrictGuestMode: generalSetting.restrictGuestMode || 'deny',
  42. currentPageCompleteDeletionAuthority: generalSetting.pageCompleteDeletionAuthority || 'anyone',
  43. isHideRestrictedByOwner: generalSetting.hideRestrictedByOwner || false,
  44. isHideRestrictedByGroup: generalSetting.hideRestrictedByGroup || false,
  45. wikiMode: generalSetting.wikiMode || '',
  46. isLocalEnabled: localSetting.isLocalEnabled || false,
  47. registrationMode: localSetting.registrationMode || 'open',
  48. registrationWhiteList: localSetting.registrationWhiteList.join('\n') || '',
  49. });
  50. }
  51. /**
  52. * Workaround for the mangling in production build to break constructor.name
  53. */
  54. static getClassName() {
  55. return 'AdminGeneralSecurityContainer';
  56. }
  57. /**
  58. * Change restrictGuestMode
  59. */
  60. changeRestrictGuestMode(restrictGuestModeLabel) {
  61. this.setState({ currentRestrictGuestMode: restrictGuestModeLabel });
  62. }
  63. /**
  64. * Change pageCompleteDeletionAuthority
  65. */
  66. changePageCompleteDeletionAuthority(pageCompleteDeletionAuthorityLabel) {
  67. this.setState({ currentPageCompleteDeletionAuthority: pageCompleteDeletionAuthorityLabel });
  68. }
  69. /**
  70. * Switch hideRestrictedByOwner
  71. */
  72. switchIsHideRestrictedByOwner() {
  73. this.setState({ isHideRestrictedByOwner: !this.state.isHideRestrictedByOwner });
  74. }
  75. /**
  76. * Switch hideRestrictedByGroup
  77. */
  78. switchIsHideRestrictedByGroup() {
  79. this.setState({ isHideRestrictedByGroup: !this.state.isHideRestrictedByGroup });
  80. }
  81. onIsWikiModeForced(wikiModeSetting) {
  82. if (wikiModeSetting === 'private') {
  83. this.setState({ isWikiModeForced: true });
  84. }
  85. else {
  86. this.setState({ isWikiModeForced: false });
  87. }
  88. }
  89. /**
  90. * Update restrictGuestMode
  91. * @memberOf AdminGeneralSecuritySContainer
  92. * @return {string} Appearance
  93. */
  94. async updateGeneralSecuritySetting() {
  95. const response = await this.appContainer.apiv3.put('/security-setting/general-setting', {
  96. restrictGuestMode: this.state.currentRestrictGuestMode,
  97. pageCompleteDeletionAuthority: this.state.currentPageCompleteDeletionAuthority,
  98. hideRestrictedByGroup: this.state.isHideRestrictedByGroup,
  99. hideRestrictedByOwner: this.state.isHideRestrictedByOwner,
  100. });
  101. const { securitySettingParams } = response.data;
  102. return securitySettingParams;
  103. }
  104. /**
  105. * Switch local enabled
  106. */
  107. switchIsLocalEnabled() {
  108. this.setState({ isLocalEnabled: !this.state.isLocalEnabled });
  109. }
  110. /**
  111. * Change registration mode
  112. */
  113. changeRegistrationMode(value) {
  114. this.setState({ registrationMode: value });
  115. }
  116. /**
  117. * Change registration white list
  118. */
  119. changeRegistrationWhiteList(value) {
  120. this.setState({ registrationWhiteList: value });
  121. }
  122. /**
  123. * update local security setting
  124. */
  125. async updateLocalSecuritySetting() {
  126. let { registrationWhiteList } = this.state;
  127. registrationWhiteList = Array.isArray(registrationWhiteList) ? registrationWhiteList : registrationWhiteList.split('\n');
  128. const response = await this.appContainer.apiv3.put('/security-setting/local-setting', {
  129. isLocalEnabled: this.state.isLocalEnabled,
  130. registrationMode: this.state.registrationMode,
  131. registrationWhiteList,
  132. });
  133. const { localSecuritySettingParams } = response.data;
  134. return localSecuritySettingParams;
  135. }
  136. /**
  137. * Switch LDAP enabled
  138. */
  139. switchIsLdapEnabled() {
  140. this.setState({ isLdapEnabled: !this.state.isLdapEnabled });
  141. }
  142. /**
  143. * Switch SAML enabled
  144. */
  145. switchIsSamlEnabled() {
  146. this.setState({ isSamlEnabled: !this.state.isSamlEnabled });
  147. }
  148. /**
  149. * Switch Oidc enabled
  150. */
  151. switchIsOidcEnabled() {
  152. this.setState({ isOidcEnabled: !this.state.isOidcEnabled });
  153. }
  154. /**
  155. * Switch Basic enabled
  156. */
  157. switchIsBasicEnabled() {
  158. this.setState({ isBasicEnabled: !this.state.isBasicEnabled });
  159. }
  160. /**
  161. * Switch GoogleOAuth enabled
  162. */
  163. switchIsGoogleOAuthEnabled() {
  164. this.setState({ isGoogleOAuthEnabled: !this.state.isGoogleOAuthEnabled });
  165. }
  166. /**
  167. * Switch GithubOAuth enabled
  168. */
  169. switchIsGithubOAuthEnabled() {
  170. this.setState({ isGithubOAuthEnabled: !this.state.isGithubOAuthEnabled });
  171. }
  172. /**
  173. * Switch TwitterOAuth enabled
  174. */
  175. switchIsTwitterOAuthEnabled() {
  176. this.setState({ isTwitterOAuthEnabled: !this.state.isTwitterOAuthEnabled });
  177. }
  178. }