AdminGeneralSecurityContainer.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. totalUsers: 0,
  30. pagingLimit: Infinity,
  31. activePage: 1,
  32. };
  33. }
  34. async retrieveSecurityData() {
  35. await this.retrieveSetupStratedies();
  36. const response = await this.appContainer.apiv3.get('/security-setting/');
  37. const { generalSetting, generalAuth } = response.data.securityParams;
  38. this.setState({
  39. currentRestrictGuestMode: generalSetting.restrictGuestMode,
  40. currentPageCompleteDeletionAuthority: generalSetting.pageCompleteDeletionAuthority,
  41. isShowRestrictedByOwner: !generalSetting.hideRestrictedByOwner,
  42. isShowRestrictedByGroup: !generalSetting.hideRestrictedByGroup,
  43. wikiMode: generalSetting.wikiMode,
  44. isLocalEnabled: generalAuth.isLocalEnabled,
  45. isLdapEnabled: generalAuth.isLdapEnabled,
  46. isSamlEnabled: generalAuth.isSamlEnabled,
  47. isOidcEnabled: generalAuth.isOidcEnabled,
  48. isBasicEnabled: generalAuth.isBasicEnabled,
  49. isGoogleEnabled: generalAuth.isGoogleEnabled,
  50. isGitHubEnabled: generalAuth.isGitHubEnabled,
  51. isTwitterEnabled: generalAuth.isTwitterEnabled,
  52. });
  53. }
  54. /**
  55. * Workaround for the mangling in production build to break constructor.name
  56. */
  57. static getClassName() {
  58. return 'AdminGeneralSecurityContainer';
  59. }
  60. /**
  61. * get isWikiModeForced
  62. * @return {bool} isWikiModeForced
  63. */
  64. get isWikiModeForced() {
  65. return this.state.wikiMode === 'public' || this.state.wikiMode === 'private';
  66. }
  67. /**
  68. * Change restrictGuestMode
  69. */
  70. changeRestrictGuestMode(restrictGuestModeLabel) {
  71. this.setState({ currentRestrictGuestMode: restrictGuestModeLabel });
  72. }
  73. /**
  74. * Change pageCompleteDeletionAuthority
  75. */
  76. changePageCompleteDeletionAuthority(pageCompleteDeletionAuthorityLabel) {
  77. this.setState({ currentPageCompleteDeletionAuthority: pageCompleteDeletionAuthorityLabel });
  78. }
  79. /**
  80. * Switch showRestrictedByOwner
  81. */
  82. switchIsShowRestrictedByOwner() {
  83. this.setState({ isShowRestrictedByOwner: !this.state.isShowRestrictedByOwner });
  84. }
  85. /**
  86. * Switch showRestrictedByGroup
  87. */
  88. switchIsShowRestrictedByGroup() {
  89. this.setState({ isShowRestrictedByGroup: !this.state.isShowRestrictedByGroup });
  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. * Retrieve All Sharelinks
  140. */
  141. async retrieveShareLinksByPagingNum(page) {
  142. const params = {
  143. page,
  144. };
  145. const { data } = await this.appContainer.apiv3.get('/all-share-links/', params);
  146. if (data.paginateResult == null) {
  147. throw new Error('data must conclude \'paginateResult\' property.');
  148. }
  149. const { docs: shareLinks, totalDocs: totalshareLinks, limit: pagingLimit } = data.paginateResult;
  150. this.setState({
  151. shareLinks,
  152. totalshareLinks,
  153. pagingLimit,
  154. activePage: page,
  155. });
  156. }
  157. /**
  158. * Switch local enabled
  159. */
  160. async switchIsLocalEnabled() {
  161. this.switchAuthentication('isLocalEnabled', 'local');
  162. }
  163. /**
  164. * Switch LDAP enabled
  165. */
  166. async switchIsLdapEnabled() {
  167. this.switchAuthentication('isLdapEnabled', 'ldap');
  168. }
  169. /**
  170. * Switch SAML enabled
  171. */
  172. async switchIsSamlEnabled() {
  173. this.switchAuthentication('isSamlEnabled', 'saml');
  174. }
  175. /**
  176. * Switch Oidc enabled
  177. */
  178. async switchIsOidcEnabled() {
  179. this.switchAuthentication('isOidcEnabled', 'oidc');
  180. }
  181. /**
  182. * Switch Basic enabled
  183. */
  184. async switchIsBasicEnabled() {
  185. this.switchAuthentication('isBasicEnabled', 'basic');
  186. }
  187. /**
  188. * Switch GoogleOAuth enabled
  189. */
  190. async switchIsGoogleOAuthEnabled() {
  191. this.switchAuthentication('isGoogleEnabled', 'google');
  192. }
  193. /**
  194. * Switch GitHubOAuth enabled
  195. */
  196. async switchIsGitHubOAuthEnabled() {
  197. this.switchAuthentication('isGitHubEnabled', 'github');
  198. }
  199. /**
  200. * Switch TwitterOAuth enabled
  201. */
  202. async switchIsTwitterOAuthEnabled() {
  203. this.switchAuthentication('isTwitterEnabled', 'twitter');
  204. }
  205. }