AdminGeneralSecurityContainer.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. import { Container } from 'unstated';
  2. import {
  3. PageSingleDeleteConfigValue, PageSingleDeleteCompConfigValue,
  4. PageRecursiveDeleteConfigValue, PageRecursiveDeleteCompConfigValue,
  5. } from '~/interfaces/page-delete-config';
  6. import { toastError } from '../util/apiNotification';
  7. import { removeNullPropertyFromObject } from '~/utils/object-utils';
  8. /**
  9. * Service container for admin security page (SecuritySetting.jsx)
  10. * @extends {Container} unstated Container
  11. */
  12. export default class AdminGeneralSecurityContainer extends Container {
  13. constructor(appContainer) {
  14. super();
  15. this.appContainer = appContainer;
  16. this.dummyCurrentRestrictGuestMode = 0;
  17. this.dummyCurrentRestrictGuestModeForError = 1;
  18. this.state = {
  19. retrieveError: null,
  20. sessionMaxAge: null,
  21. wikiMode: '',
  22. // set dummy value tile for using suspense
  23. currentRestrictGuestMode: this.dummyCurrentRestrictGuestMode,
  24. currentPageDeletionAuthority: PageSingleDeleteConfigValue.AdminOnly,
  25. currentPageCompleteDeletionAuthority: PageSingleDeleteCompConfigValue.AdminOnly,
  26. currentPageRecursiveDeletionAuthority: PageRecursiveDeleteConfigValue.Inherit,
  27. currentPageRecursiveCompleteDeletionAuthority: PageRecursiveDeleteCompConfigValue.Inherit,
  28. isShowRestrictedByOwner: false,
  29. isShowRestrictedByGroup: false,
  30. appSiteUrl: appContainer.config.crowi.url || '',
  31. isLocalEnabled: false,
  32. isLdapEnabled: false,
  33. isSamlEnabled: false,
  34. isOidcEnabled: false,
  35. isBasicEnabled: false,
  36. isGoogleEnabled: false,
  37. isGitHubEnabled: false,
  38. isTwitterEnabled: false,
  39. setupStrategies: [],
  40. disableLinkSharing: false,
  41. shareLinks: [],
  42. totalshareLinks: 0,
  43. shareLinksPagingLimit: Infinity,
  44. shareLinksActivePage: 1,
  45. };
  46. this.changePageDeletionAuthority = this.changePageDeletionAuthority.bind(this);
  47. this.changePageCompleteDeletionAuthority = this.changePageCompleteDeletionAuthority.bind(this);
  48. this.changePageRecursiveDeletionAuthority = this.changePageRecursiveDeletionAuthority.bind(this);
  49. this.changePageRecursiveCompleteDeletionAuthority = this.changePageRecursiveCompleteDeletionAuthority.bind(this);
  50. }
  51. async retrieveSecurityData() {
  52. await this.retrieveSetupStratedies();
  53. const response = await this.appContainer.apiv3.get('/security-setting/');
  54. const { generalSetting, shareLinkSetting, generalAuth } = response.data.securityParams;
  55. this.setState({
  56. currentRestrictGuestMode: generalSetting.restrictGuestMode,
  57. currentPageDeletionAuthority: generalSetting.pageDeletionAuthority,
  58. currentPageCompleteDeletionAuthority: generalSetting.pageCompleteDeletionAuthority,
  59. currentPageRecursiveDeletionAuthority: generalSetting.pageRecursiveDeletionAuthority,
  60. currentPageRecursiveCompleteDeletionAuthority: generalSetting.pageRecursiveCompleteDeletionAuthority,
  61. isShowRestrictedByOwner: !generalSetting.hideRestrictedByOwner,
  62. isShowRestrictedByGroup: !generalSetting.hideRestrictedByGroup,
  63. sessionMaxAge: generalSetting.sessionMaxAge,
  64. wikiMode: generalSetting.wikiMode,
  65. disableLinkSharing: shareLinkSetting.disableLinkSharing,
  66. isLocalEnabled: generalAuth.isLocalEnabled,
  67. isLdapEnabled: generalAuth.isLdapEnabled,
  68. isSamlEnabled: generalAuth.isSamlEnabled,
  69. isOidcEnabled: generalAuth.isOidcEnabled,
  70. isBasicEnabled: generalAuth.isBasicEnabled,
  71. isGoogleEnabled: generalAuth.isGoogleEnabled,
  72. isGitHubEnabled: generalAuth.isGitHubEnabled,
  73. isTwitterEnabled: generalAuth.isTwitterEnabled,
  74. });
  75. }
  76. /**
  77. * Workaround for the mangling in production build to break constructor.name
  78. */
  79. static getClassName() {
  80. return 'AdminGeneralSecurityContainer';
  81. }
  82. /**
  83. * get isWikiModeForced
  84. * @return {bool} isWikiModeForced
  85. */
  86. get isWikiModeForced() {
  87. return this.state.wikiMode === 'public' || this.state.wikiMode === 'private';
  88. }
  89. /**
  90. * setter for sessionMaxAge
  91. */
  92. setSessionMaxAge(sessionMaxAge) {
  93. this.setState({ sessionMaxAge });
  94. }
  95. /**
  96. * setter for disableLinkSharing
  97. */
  98. setDisableLinkSharing(disableLinkSharing) {
  99. this.setState({ disableLinkSharing });
  100. }
  101. /**
  102. * Change restrictGuestMode
  103. */
  104. changeRestrictGuestMode(restrictGuestModeLabel) {
  105. this.setState({ currentRestrictGuestMode: restrictGuestModeLabel });
  106. }
  107. /**
  108. * Change pageDeletionAuthority
  109. */
  110. changePageDeletionAuthority(val) {
  111. this.setState({ currentPageDeletionAuthority: val });
  112. }
  113. /**
  114. * Change pageCompleteDeletionAuthority
  115. */
  116. changePageCompleteDeletionAuthority(val) {
  117. this.setState({ currentPageCompleteDeletionAuthority: val });
  118. }
  119. /**
  120. * Change pageRecursiveDeletionAuthority
  121. */
  122. changePageRecursiveDeletionAuthority(val) {
  123. this.setState({ currentPageRecursiveDeletionAuthority: val });
  124. }
  125. /**
  126. * Change pageRecursiveCompleteDeletionAuthority
  127. */
  128. changePageRecursiveCompleteDeletionAuthority(val) {
  129. this.setState({ currentPageRecursiveCompleteDeletionAuthority: val });
  130. }
  131. /**
  132. * Switch showRestrictedByOwner
  133. */
  134. switchIsShowRestrictedByOwner() {
  135. this.setState({ isShowRestrictedByOwner: !this.state.isShowRestrictedByOwner });
  136. }
  137. /**
  138. * Switch showRestrictedByGroup
  139. */
  140. switchIsShowRestrictedByGroup() {
  141. this.setState({ isShowRestrictedByGroup: !this.state.isShowRestrictedByGroup });
  142. }
  143. /**
  144. * Update restrictGuestMode
  145. * @memberOf AdminGeneralSecuritySContainer
  146. * @return {string} Appearance
  147. */
  148. async updateGeneralSecuritySetting() {
  149. let requestParams = {
  150. sessionMaxAge: this.state.sessionMaxAge,
  151. restrictGuestMode: this.state.currentRestrictGuestMode,
  152. pageDeletionAuthority: this.state.currentPageDeletionAuthority,
  153. pageCompleteDeletionAuthority: this.state.currentPageCompleteDeletionAuthority,
  154. pageRecursiveDeletionAuthority: this.state.currentPageRecursiveDeletionAuthority,
  155. pageRecursiveCompleteDeletionAuthority: this.state.currentPageRecursiveCompleteDeletionAuthority,
  156. hideRestrictedByGroup: !this.state.isShowRestrictedByGroup,
  157. hideRestrictedByOwner: !this.state.isShowRestrictedByOwner,
  158. };
  159. requestParams = await removeNullPropertyFromObject(requestParams);
  160. const response = await this.appContainer.apiv3.put('/security-setting/general-setting', requestParams);
  161. const { securitySettingParams } = response.data;
  162. return securitySettingParams;
  163. }
  164. /**
  165. * Switch disableLinkSharing
  166. */
  167. async switchDisableLinkSharing() {
  168. const requestParams = {
  169. disableLinkSharing: !this.state.disableLinkSharing,
  170. };
  171. const response = await this.appContainer.apiv3.put('/security-setting/share-link-setting', requestParams);
  172. this.setDisableLinkSharing(!this.state.disableLinkSharing);
  173. return response;
  174. }
  175. /**
  176. * Switch authentication
  177. */
  178. async switchAuthentication(stateVariableName, authId) {
  179. const isEnabled = !this.state[stateVariableName];
  180. try {
  181. await this.appContainer.apiv3.put('/security-setting/authentication/enabled', {
  182. isEnabled,
  183. authId,
  184. });
  185. await this.retrieveSetupStratedies();
  186. this.setState({ [stateVariableName]: isEnabled });
  187. }
  188. catch (err) {
  189. toastError(err);
  190. }
  191. }
  192. /**
  193. * Retrieve SetupStratedies
  194. */
  195. async retrieveSetupStratedies() {
  196. try {
  197. const response = await this.appContainer.apiv3.get('/security-setting/authentication');
  198. const { setupStrategies } = response.data;
  199. this.setState({ setupStrategies });
  200. }
  201. catch (err) {
  202. toastError(err);
  203. }
  204. }
  205. /**
  206. * Retrieve All Sharelinks
  207. */
  208. async retrieveShareLinksByPagingNum(page) {
  209. const params = {
  210. page,
  211. };
  212. const { data } = await this.appContainer.apiv3.get('/security-setting/all-share-links', params);
  213. if (data.paginateResult == null) {
  214. throw new Error('data must conclude \'paginateResult\' property.');
  215. }
  216. const { docs: shareLinks, totalDocs: totalshareLinks, limit: shareLinksPagingLimit } = data.paginateResult;
  217. this.setState({
  218. shareLinks,
  219. totalshareLinks,
  220. shareLinksPagingLimit,
  221. shareLinksActivePage: page,
  222. });
  223. }
  224. /**
  225. * Switch local enabled
  226. */
  227. async switchIsLocalEnabled() {
  228. this.switchAuthentication('isLocalEnabled', 'local');
  229. }
  230. /**
  231. * Switch LDAP enabled
  232. */
  233. async switchIsLdapEnabled() {
  234. this.switchAuthentication('isLdapEnabled', 'ldap');
  235. }
  236. /**
  237. * Switch SAML enabled
  238. */
  239. async switchIsSamlEnabled() {
  240. this.switchAuthentication('isSamlEnabled', 'saml');
  241. }
  242. /**
  243. * Switch Oidc enabled
  244. */
  245. async switchIsOidcEnabled() {
  246. this.switchAuthentication('isOidcEnabled', 'oidc');
  247. }
  248. /**
  249. * Switch Basic enabled
  250. */
  251. async switchIsBasicEnabled() {
  252. this.switchAuthentication('isBasicEnabled', 'basic');
  253. }
  254. /**
  255. * Switch GoogleOAuth enabled
  256. */
  257. async switchIsGoogleOAuthEnabled() {
  258. this.switchAuthentication('isGoogleEnabled', 'google');
  259. }
  260. /**
  261. * Switch GitHubOAuth enabled
  262. */
  263. async switchIsGitHubOAuthEnabled() {
  264. this.switchAuthentication('isGitHubEnabled', 'github');
  265. }
  266. /**
  267. * Switch TwitterOAuth enabled
  268. */
  269. async switchIsTwitterOAuthEnabled() {
  270. this.switchAuthentication('isTwitterEnabled', 'twitter');
  271. }
  272. }