AdminGeneralSecurityContainer.js 9.9 KB

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