AdminGeneralSecurityContainer.js 13 KB

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