AdminGeneralSecurityContainer.js 14 KB

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