AdminGeneralSecurityContainer.js 7.4 KB

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