AdminGeneralSecurityContainer.js 11 KB

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