AdminGeneralSecurityContainer.js 12 KB

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