AdminGeneralSecurityContainer.js 6.5 KB

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