AdminGeneralSecurityContainer.js 6.1 KB

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