AdminGeneralSecurityContainer.js 6.7 KB

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