AdminGeneralSecurityContainer.js 11 KB

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