AdminGeneralSecurityContainer.js 9.7 KB

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