AdminGeneralSecurityContainer.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. import { Container } from 'unstated';
  2. import {
  3. PageSingleDeleteConfigValue, PageSingleDeleteCompConfigValue,
  4. PageRecursiveDeleteConfigValue, PageRecursiveDeleteCompConfigValue,
  5. } from '~/interfaces/page-delete-config';
  6. import { toastError } from '../util/apiNotification';
  7. import { removeNullPropertyFromObject } from '~/utils/object-utils';
  8. /**
  9. * Service container for admin security page (SecuritySetting.jsx)
  10. * @extends {Container} unstated Container
  11. */
  12. export default class AdminGeneralSecurityContainer extends Container {
  13. constructor(appContainer) {
  14. super();
  15. this.appContainer = appContainer;
  16. this.dummyCurrentRestrictGuestMode = 0;
  17. this.dummyCurrentRestrictGuestModeForError = 1;
  18. this.state = {
  19. retrieveError: null,
  20. sessionMaxAge: null,
  21. wikiMode: '',
  22. // set dummy value tile for using suspense
  23. currentRestrictGuestMode: this.dummyCurrentRestrictGuestMode,
  24. currentPageDeletionAuthority: PageSingleDeleteConfigValue.AdminOnly,
  25. currentPageRecursiveDeletionAuthority: PageRecursiveDeleteConfigValue.Inherit,
  26. currentPageCompleteDeletionAuthority: PageSingleDeleteCompConfigValue.AdminOnly,
  27. currentPageRecursiveCompleteDeletionAuthority: PageRecursiveDeleteCompConfigValue.Inherit,
  28. expandOtherOptionsForDeletion: false,
  29. expandOtherOptionsForCompleteDeletion: false,
  30. isShowRestrictedByOwner: false,
  31. isShowRestrictedByGroup: false,
  32. appSiteUrl: appContainer.config.crowi.url || '',
  33. isLocalEnabled: false,
  34. isLdapEnabled: false,
  35. isSamlEnabled: false,
  36. isOidcEnabled: false,
  37. isBasicEnabled: false,
  38. isGoogleEnabled: false,
  39. isGitHubEnabled: false,
  40. isTwitterEnabled: 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. }
  53. async retrieveSecurityData() {
  54. await this.retrieveSetupStratedies();
  55. const response = await this.appContainer.apiv3.get('/security-setting/');
  56. const { generalSetting, shareLinkSetting, generalAuth } = response.data.securityParams;
  57. this.setState({
  58. currentRestrictGuestMode: generalSetting.restrictGuestMode,
  59. currentPageDeletionAuthority: generalSetting.pageDeletionAuthority,
  60. currentPageCompleteDeletionAuthority: generalSetting.pageCompleteDeletionAuthority,
  61. currentPageRecursiveDeletionAuthority: generalSetting.pageRecursiveDeletionAuthority,
  62. currentPageRecursiveCompleteDeletionAuthority: generalSetting.pageRecursiveCompleteDeletionAuthority,
  63. isShowRestrictedByOwner: !generalSetting.hideRestrictedByOwner,
  64. isShowRestrictedByGroup: !generalSetting.hideRestrictedByGroup,
  65. sessionMaxAge: generalSetting.sessionMaxAge,
  66. wikiMode: generalSetting.wikiMode,
  67. disableLinkSharing: shareLinkSetting.disableLinkSharing,
  68. isLocalEnabled: generalAuth.isLocalEnabled,
  69. isLdapEnabled: generalAuth.isLdapEnabled,
  70. isSamlEnabled: generalAuth.isSamlEnabled,
  71. isOidcEnabled: generalAuth.isOidcEnabled,
  72. isBasicEnabled: generalAuth.isBasicEnabled,
  73. isGoogleEnabled: generalAuth.isGoogleEnabled,
  74. isGitHubEnabled: generalAuth.isGitHubEnabled,
  75. isTwitterEnabled: generalAuth.isTwitterEnabled,
  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. * Switch ExpandOtherOptionsForDeletion
  135. */
  136. switchExpandOtherOptionsForDeletion() {
  137. this.setState({ expandOtherOptionsForDeletion: !this.state.expandOtherOptionsForDeletion });
  138. }
  139. /**
  140. * Switch ExpandOtherOptionsForDeletion
  141. */
  142. switchExpandOtherOptionsForCompleteDeletion() {
  143. this.setState({ expandOtherOptionsForCompleteDeletion: !this.state.expandOtherOptionsForCompleteDeletion });
  144. }
  145. /**
  146. * Switch showRestrictedByOwner
  147. */
  148. switchIsShowRestrictedByOwner() {
  149. this.setState({ isShowRestrictedByOwner: !this.state.isShowRestrictedByOwner });
  150. }
  151. /**
  152. * Switch showRestrictedByGroup
  153. */
  154. switchIsShowRestrictedByGroup() {
  155. this.setState({ isShowRestrictedByGroup: !this.state.isShowRestrictedByGroup });
  156. }
  157. /**
  158. * Update restrictGuestMode
  159. * @memberOf AdminGeneralSecuritySContainer
  160. * @return {string} Appearance
  161. */
  162. async updateGeneralSecuritySetting() {
  163. let requestParams = {
  164. sessionMaxAge: this.state.sessionMaxAge,
  165. restrictGuestMode: this.state.currentRestrictGuestMode,
  166. pageDeletionAuthority: this.state.currentPageDeletionAuthority,
  167. pageCompleteDeletionAuthority: this.state.currentPageCompleteDeletionAuthority,
  168. pageRecursiveDeletionAuthority: this.state.currentPageRecursiveDeletionAuthority,
  169. pageRecursiveCompleteDeletionAuthority: this.state.currentPageRecursiveCompleteDeletionAuthority,
  170. hideRestrictedByGroup: !this.state.isShowRestrictedByGroup,
  171. hideRestrictedByOwner: !this.state.isShowRestrictedByOwner,
  172. };
  173. requestParams = await removeNullPropertyFromObject(requestParams);
  174. const response = await this.appContainer.apiv3.put('/security-setting/general-setting', requestParams);
  175. const { securitySettingParams } = response.data;
  176. return securitySettingParams;
  177. }
  178. /**
  179. * Switch disableLinkSharing
  180. */
  181. async switchDisableLinkSharing() {
  182. const requestParams = {
  183. disableLinkSharing: !this.state.disableLinkSharing,
  184. };
  185. const response = await this.appContainer.apiv3.put('/security-setting/share-link-setting', requestParams);
  186. this.setDisableLinkSharing(!this.state.disableLinkSharing);
  187. return response;
  188. }
  189. /**
  190. * Switch authentication
  191. */
  192. async switchAuthentication(stateVariableName, authId) {
  193. const isEnabled = !this.state[stateVariableName];
  194. try {
  195. await this.appContainer.apiv3.put('/security-setting/authentication/enabled', {
  196. isEnabled,
  197. authId,
  198. });
  199. await this.retrieveSetupStratedies();
  200. this.setState({ [stateVariableName]: isEnabled });
  201. }
  202. catch (err) {
  203. toastError(err);
  204. }
  205. }
  206. /**
  207. * Retrieve SetupStratedies
  208. */
  209. async retrieveSetupStratedies() {
  210. try {
  211. const response = await this.appContainer.apiv3.get('/security-setting/authentication');
  212. const { setupStrategies } = response.data;
  213. this.setState({ setupStrategies });
  214. }
  215. catch (err) {
  216. toastError(err);
  217. }
  218. }
  219. /**
  220. * Retrieve All Sharelinks
  221. */
  222. async retrieveShareLinksByPagingNum(page) {
  223. const params = {
  224. page,
  225. };
  226. const { data } = await this.appContainer.apiv3.get('/security-setting/all-share-links', params);
  227. if (data.paginateResult == null) {
  228. throw new Error('data must conclude \'paginateResult\' property.');
  229. }
  230. const { docs: shareLinks, totalDocs: totalshareLinks, limit: shareLinksPagingLimit } = data.paginateResult;
  231. this.setState({
  232. shareLinks,
  233. totalshareLinks,
  234. shareLinksPagingLimit,
  235. shareLinksActivePage: page,
  236. });
  237. }
  238. /**
  239. * Switch local enabled
  240. */
  241. async switchIsLocalEnabled() {
  242. this.switchAuthentication('isLocalEnabled', 'local');
  243. }
  244. /**
  245. * Switch LDAP enabled
  246. */
  247. async switchIsLdapEnabled() {
  248. this.switchAuthentication('isLdapEnabled', 'ldap');
  249. }
  250. /**
  251. * Switch SAML enabled
  252. */
  253. async switchIsSamlEnabled() {
  254. this.switchAuthentication('isSamlEnabled', 'saml');
  255. }
  256. /**
  257. * Switch Oidc enabled
  258. */
  259. async switchIsOidcEnabled() {
  260. this.switchAuthentication('isOidcEnabled', 'oidc');
  261. }
  262. /**
  263. * Switch Basic enabled
  264. */
  265. async switchIsBasicEnabled() {
  266. this.switchAuthentication('isBasicEnabled', 'basic');
  267. }
  268. /**
  269. * Switch GoogleOAuth enabled
  270. */
  271. async switchIsGoogleOAuthEnabled() {
  272. this.switchAuthentication('isGoogleEnabled', 'google');
  273. }
  274. /**
  275. * Switch GitHubOAuth enabled
  276. */
  277. async switchIsGitHubOAuthEnabled() {
  278. this.switchAuthentication('isGitHubEnabled', 'github');
  279. }
  280. /**
  281. * Switch TwitterOAuth enabled
  282. */
  283. async switchIsTwitterOAuthEnabled() {
  284. this.switchAuthentication('isTwitterEnabled', 'twitter');
  285. }
  286. }