/* eslint-disable react/no-danger */ import React from 'react'; import PropTypes from 'prop-types'; import { Collapse } from 'reactstrap'; import { withTranslation } from 'react-i18next'; import { validateDeleteConfigs } from '~/utils/page-delete-config'; import { withUnstatedContainers } from '../../UnstatedUtils'; import { toastSuccess, toastError } from '~/client/util/apiNotification'; import { PageDeleteConfigValue } from '~/interfaces/page-delete-config'; import AppContainer from '~/client/services/AppContainer'; import AdminGeneralSecurityContainer from '~/client/services/AdminGeneralSecurityContainer'; // used as the prefix of translation const DeletionTypeForT = Object.freeze({ Deletion: 'deletion', CompleteDeletion: 'complete_deletion', RecursiveDeletion: 'recursive_deletion', RecursiveCompleteDeletion: 'recursive_complete_deletion', }); const DeletionType = Object.freeze({ Deletion: 'deletion', CompleteDeletion: 'completeDeletion', RecursiveDeletion: 'recursiveDeletion', RecursiveCompleteDeletion: 'recursiveCompleteDeletion', }); const getDeletionTypeForT = (deletionType) => { switch (deletionType) { case DeletionType.Deletion: return DeletionTypeForT.Deletion; case DeletionType.RecursiveDeletion: return DeletionTypeForT.RecursiveDeletion; case DeletionType.CompleteDeletion: return DeletionTypeForT.CompleteDeletion; case DeletionType.RecursiveCompleteDeletion: return DeletionTypeForT.RecursiveCompleteDeletion; } }; /** * Return true if "deletionType" is DeletionType.RecursiveDeletion or DeletionType.RecursiveCompleteDeletion. * @param deletionType Deletion type * @returns boolean */ const isRecursiveDeletion = (deletionType) => { return deletionType === DeletionType.RecursiveDeletion || deletionType === DeletionType.RecursiveCompleteDeletion; }; /** * Return true if "deletionType" is DeletionType.Deletion or DeletionType.RecursiveDeletion. * @param deletionType Deletion type * @returns boolean */ const isTypeDeletion = (deletionType) => { return deletionType === DeletionType.Deletion || deletionType === DeletionType.RecursiveDeletion; }; class SecuritySetting extends React.Component { constructor(props) { super(props); this.putSecuritySetting = this.putSecuritySetting.bind(this); this.getRecursiveDeletionConfigState = this.getRecursiveDeletionConfigState.bind(this); this.setDeletionConfigState = this.setDeletionConfigState.bind(this); this.renderPageDeletePermission = this.renderPageDeletePermission.bind(this); this.renderPageDeletePermissionDropdown = this.renderPageDeletePermissionDropdown.bind(this); } async putSecuritySetting() { const { t, adminGeneralSecurityContainer } = this.props; try { await adminGeneralSecurityContainer.updateGeneralSecuritySetting(); toastSuccess(t('security_setting.updated_general_security_setting')); } catch (err) { toastError(err); } } getRecursiveDeletionConfigState(deletionType) { const { adminGeneralSecurityContainer } = this.props; if (isTypeDeletion(deletionType)) { return [ adminGeneralSecurityContainer.state.currentPageRecursiveDeletionAuthority, adminGeneralSecurityContainer.changePageRecursiveDeletionAuthority, ]; } return [ adminGeneralSecurityContainer.state.currentPageRecursiveCompleteDeletionAuthority, adminGeneralSecurityContainer.changePageRecursiveCompleteDeletionAuthority, ]; } /** * Force update deletion config for recursive operation when the deletion config for general operation is updated. * @param deletionType Deletion type */ setDeletionConfigState(newState, setState, deletionType) { if (isRecursiveDeletion(deletionType)) { setState(newState); return; } const [recursiveState, setRecursiveState] = this.getRecursiveDeletionConfigState(deletionType); const shouldForceUpdate = !validateDeleteConfigs(newState, recursiveState); if (shouldForceUpdate) { setState(newState); setRecursiveState(newState); } else { setState(newState); } return; } renderPageDeletePermissionDropdown(currentState, setState, deletionType, isButtonDisabled) { const { t } = this.props; return (
{ isRecursiveDeletion(deletionType) ? ( ) : ( ) }

{t(`security_setting.${getDeletionTypeForT(deletionType)}_explain`)}

); } renderPageDeletePermission(currentState, setState, deletionType, isButtonDisabled) { const { t, adminGeneralSecurityContainer } = this.props; const expandOtherOptions = isTypeDeletion(deletionType) ? adminGeneralSecurityContainer.state.expandOtherOptionsForDeletion : adminGeneralSecurityContainer.state.expandOtherOptionsForCompleteDeletion; const setExpantOtherOptions = () => { if (isTypeDeletion(deletionType)) { adminGeneralSecurityContainer.switchExpandOtherOptionsForDeletion(); return; } adminGeneralSecurityContainer.switchExpandOtherOptionsForCompleteDeletion(); return; }; return (
{!isRecursiveDeletion(deletionType) && isTypeDeletion(deletionType) && ( {t('security_setting.page_delete')} )} {!isRecursiveDeletion(deletionType) && !isTypeDeletion(deletionType) && ( {t('security_setting.page_delete_completely')} )}
{ !isRecursiveDeletion(deletionType) ? ( <>{this.renderPageDeletePermissionDropdown(currentState, setState, deletionType, isButtonDisabled)} ) : ( <>
{this.renderPageDeletePermissionDropdown(currentState, setState, deletionType, isButtonDisabled)}
) }
); } render() { const { t, adminGeneralSecurityContainer } = this.props; const { currentRestrictGuestMode, currentPageDeletionAuthority, currentPageCompleteDeletionAuthority, currentPageRecursiveDeletionAuthority, currentPageRecursiveCompleteDeletionAuthority, } = adminGeneralSecurityContainer.state; const isButtonDisabledForDeletion = !validateDeleteConfigs( adminGeneralSecurityContainer.state.currentPageDeletionAuthority, PageDeleteConfigValue.AdminAndAuthor, ); const isButtonDisabledForCompleteDeletion = !validateDeleteConfigs( adminGeneralSecurityContainer.state.currentPageCompleteDeletionAuthority, PageDeleteConfigValue.AdminAndAuthor, ); return (

{t('security_settings')}

{adminGeneralSecurityContainer.retrieveError != null && (

{t('Error occurred')} : {adminGeneralSecurityContainer.retrieveError}

)}

{ t('security_setting.page_list_and_search_results') }

{ t('scope_of_page_disclosure') } { t('set_point') }
{ t('Public') } { t('always_displayed') }
{ t('Anyone with the link') } { t('always_hidden') }
{ t('Only me') }
{ adminGeneralSecurityContainer.switchIsShowRestrictedByOwner() }} />
{ t('Only inside the group') }
{ adminGeneralSecurityContainer.switchIsShowRestrictedByGroup() }} />

{t('security_setting.page_access_rights')}

{t('security_setting.Guest Users Access')}
{adminGeneralSecurityContainer.isWikiModeForced && (

FIXED

)}

{t('security_setting.page_delete_rights')}

{/* Render PageDeletePermissionDropdown */} { [ [currentPageDeletionAuthority, adminGeneralSecurityContainer.changePageDeletionAuthority, DeletionType.Deletion, false], // eslint-disable-next-line max-len [currentPageRecursiveDeletionAuthority, adminGeneralSecurityContainer.changePageRecursiveDeletionAuthority, DeletionType.RecursiveDeletion, isButtonDisabledForDeletion], ].map(arr => this.renderPageDeletePermission(arr[0], arr[1], arr[2], arr[3])) } { [ [currentPageCompleteDeletionAuthority, adminGeneralSecurityContainer.changePageCompleteDeletionAuthority, DeletionType.CompleteDeletion, false], // eslint-disable-next-line max-len [currentPageRecursiveCompleteDeletionAuthority, adminGeneralSecurityContainer.changePageRecursiveCompleteDeletionAuthority, DeletionType.RecursiveCompleteDeletion, isButtonDisabledForCompleteDeletion], ].map(arr => this.renderPageDeletePermission(arr[0], arr[1], arr[2], arr[3])) }

{t('security_setting.session')}

{ adminGeneralSecurityContainer.setSessionMaxAge(e.target.value); }} placeholder="2592000000" /> {/* eslint-disable-next-line react/no-danger */}

{t('security_setting.max_age_caution')}

); } } SecuritySetting.propTypes = { t: PropTypes.func.isRequired, // i18next appContainer: PropTypes.instanceOf(AppContainer).isRequired, csrf: PropTypes.string, adminGeneralSecurityContainer: PropTypes.instanceOf(AdminGeneralSecurityContainer).isRequired, }; const SecuritySettingWrapper = withUnstatedContainers(SecuritySetting, [AppContainer, AdminGeneralSecurityContainer]); export default withTranslation()(SecuritySettingWrapper);