AdminGeneralSecurityContainer.js 11 KB

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