2
0

AdminGeneralSecurityContainer.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. shareLinks: [],
  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. currentRestrictGuestMode: generalSetting.restrictGuestMode,
  37. currentPageCompleteDeletionAuthority: generalSetting.pageCompleteDeletionAuthority,
  38. isShowRestrictedByOwner: !generalSetting.hideRestrictedByOwner,
  39. isShowRestrictedByGroup: !generalSetting.hideRestrictedByGroup,
  40. wikiMode: generalSetting.wikiMode,
  41. isLocalEnabled: generalAuth.isLocalEnabled,
  42. isLdapEnabled: generalAuth.isLdapEnabled,
  43. isSamlEnabled: generalAuth.isSamlEnabled,
  44. isOidcEnabled: generalAuth.isOidcEnabled,
  45. isBasicEnabled: generalAuth.isBasicEnabled,
  46. isGoogleEnabled: generalAuth.isGoogleEnabled,
  47. isGitHubEnabled: generalAuth.isGitHubEnabled,
  48. isTwitterEnabled: generalAuth.isTwitterEnabled,
  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. * get isWikiModeForced
  59. * @return {bool} isWikiModeForced
  60. */
  61. get isWikiModeForced() {
  62. return this.state.wikiMode === 'public' || this.state.wikiMode === 'private';
  63. }
  64. /**
  65. * Change restrictGuestMode
  66. */
  67. changeRestrictGuestMode(restrictGuestModeLabel) {
  68. this.setState({ currentRestrictGuestMode: restrictGuestModeLabel });
  69. }
  70. /**
  71. * Change pageCompleteDeletionAuthority
  72. */
  73. changePageCompleteDeletionAuthority(pageCompleteDeletionAuthorityLabel) {
  74. this.setState({ currentPageCompleteDeletionAuthority: pageCompleteDeletionAuthorityLabel });
  75. }
  76. /**
  77. * Switch showRestrictedByOwner
  78. */
  79. switchIsShowRestrictedByOwner() {
  80. this.setState({ isShowRestrictedByOwner: !this.state.isShowRestrictedByOwner });
  81. }
  82. /**
  83. * Switch showRestrictedByGroup
  84. */
  85. switchIsShowRestrictedByGroup() {
  86. this.setState({ isShowRestrictedByGroup: !this.state.isShowRestrictedByGroup });
  87. }
  88. /**
  89. * Update restrictGuestMode
  90. * @memberOf AdminGeneralSecuritySContainer
  91. * @return {string} Appearance
  92. */
  93. async updateGeneralSecuritySetting() {
  94. let requestParams = {
  95. restrictGuestMode: this.state.currentRestrictGuestMode,
  96. pageCompleteDeletionAuthority: this.state.currentPageCompleteDeletionAuthority,
  97. hideRestrictedByGroup: !this.state.isShowRestrictedByGroup,
  98. hideRestrictedByOwner: !this.state.isShowRestrictedByOwner,
  99. };
  100. requestParams = await removeNullPropertyFromObject(requestParams);
  101. const response = await this.appContainer.apiv3.put('/security-setting/general-setting', requestParams);
  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. await this.retrieveSetupStratedies();
  116. this.setState({ [stateVariableName]: isEnabled });
  117. }
  118. catch (err) {
  119. toastError(err);
  120. }
  121. }
  122. /**
  123. * Retrieve SetupStratedies
  124. */
  125. async retrieveSetupStratedies() {
  126. try {
  127. const response = await this.appContainer.apiv3.get('/security-setting/authentication');
  128. const { setupStrategies } = response.data;
  129. this.setState({ setupStrategies });
  130. }
  131. catch (err) {
  132. toastError(err);
  133. }
  134. }
  135. /**
  136. * Retrieve All Sharelinks
  137. */
  138. async retriveShareLinks() {
  139. try {
  140. const response = await this.appContainer.apiv3.get('/all-share-links/');
  141. const { shareLinks } = response.data;
  142. this.setState({ shareLinks });
  143. }
  144. catch (err) {
  145. toastError(err);
  146. }
  147. }
  148. /**
  149. * Switch local enabled
  150. */
  151. async switchIsLocalEnabled() {
  152. this.switchAuthentication('isLocalEnabled', 'local');
  153. }
  154. /**
  155. * Switch LDAP enabled
  156. */
  157. async switchIsLdapEnabled() {
  158. this.switchAuthentication('isLdapEnabled', 'ldap');
  159. }
  160. /**
  161. * Switch SAML enabled
  162. */
  163. async switchIsSamlEnabled() {
  164. this.switchAuthentication('isSamlEnabled', 'saml');
  165. }
  166. /**
  167. * Switch Oidc enabled
  168. */
  169. async switchIsOidcEnabled() {
  170. this.switchAuthentication('isOidcEnabled', 'oidc');
  171. }
  172. /**
  173. * Switch Basic enabled
  174. */
  175. async switchIsBasicEnabled() {
  176. this.switchAuthentication('isBasicEnabled', 'basic');
  177. }
  178. /**
  179. * Switch GoogleOAuth enabled
  180. */
  181. async switchIsGoogleOAuthEnabled() {
  182. this.switchAuthentication('isGoogleEnabled', 'google');
  183. }
  184. /**
  185. * Switch GitHubOAuth enabled
  186. */
  187. async switchIsGitHubOAuthEnabled() {
  188. this.switchAuthentication('isGitHubEnabled', 'github');
  189. }
  190. /**
  191. * Switch TwitterOAuth enabled
  192. */
  193. async switchIsTwitterOAuthEnabled() {
  194. this.switchAuthentication('isTwitterEnabled', 'twitter');
  195. }
  196. }