/* eslint-disable react/no-danger */ import React from 'react'; import { useTranslation } from 'next-i18next'; import PropTypes from 'prop-types'; import { Collapse } from 'reactstrap'; import AdminGeneralSecurityContainer from '~/client/services/AdminGeneralSecurityContainer'; import { toastSuccess, toastError } from '~/client/util/toastr'; import { PageDeleteConfigValue } from '~/interfaces/page-delete-config'; import { validateDeleteConfigs, prepareDeleteConfigValuesForCalc } from '~/utils/page-delete-config'; import { withUnstatedContainers } from '../../UnstatedUtils'; // 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; } }; const getDeleteConfigValueForT = (DeleteConfigValue) => { switch (DeleteConfigValue) { case PageDeleteConfigValue.Anyone: case null: return 'security_settings.anyone'; case PageDeleteConfigValue.Inherit: return 'security_settings.inherit'; case PageDeleteConfigValue.AdminOnly: return 'security_settings.admin_only'; case PageDeleteConfigValue.AdminAndAuthor: return 'security_settings.admin_and_author'; } }; /** * 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); // functions this.putSecuritySetting = this.putSecuritySetting.bind(this); this.getRecursiveDeletionConfigState = this.getRecursiveDeletionConfigState.bind(this); this.previousPageRecursiveAuthorityState = this.previousPageRecursiveAuthorityState.bind(this); this.setPagePreviousRecursiveAuthorityState = this.setPagePreviousRecursiveAuthorityState.bind(this); this.expantDeleteOptionsState = this.expantDeleteOptionsState.bind(this); this.setExpantOtherDeleteOptionsState = this.setExpantOtherDeleteOptionsState.bind(this); this.setDeletionConfigState = this.setDeletionConfigState.bind(this); // render 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_settings.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, ]; } previousPageRecursiveAuthorityState(deletionType) { const { adminGeneralSecurityContainer } = this.props; return isTypeDeletion(deletionType) ? adminGeneralSecurityContainer.state.previousPageRecursiveDeletionAuthority : adminGeneralSecurityContainer.state.previousPageRecursiveCompleteDeletionAuthority; } setPagePreviousRecursiveAuthorityState(deletionType, previousState) { const { adminGeneralSecurityContainer } = this.props; if (isTypeDeletion(deletionType)) { adminGeneralSecurityContainer.changePreviousPageRecursiveDeletionAuthority(previousState); return; } adminGeneralSecurityContainer.changePreviousPageRecursiveCompleteDeletionAuthority(previousState); } expantDeleteOptionsState(deletionType) { const { adminGeneralSecurityContainer } = this.props; return isTypeDeletion(deletionType) ? adminGeneralSecurityContainer.state.expandOtherOptionsForDeletion : adminGeneralSecurityContainer.state.expandOtherOptionsForCompleteDeletion; } setExpantOtherDeleteOptionsState(deletionType, bool) { const { adminGeneralSecurityContainer } = this.props; if (isTypeDeletion(deletionType)) { adminGeneralSecurityContainer.switchExpandOtherOptionsForDeletion(bool); return; } adminGeneralSecurityContainer.switchExpandOtherOptionsForCompleteDeletion(bool); return; } /** * Force update deletion config for recursive operation when the deletion config for general operation is updated. * @param deletionType Deletion type */ setDeletionConfigState(newState, setState, deletionType) { setState(newState); if (this.previousPageRecursiveAuthorityState(deletionType) !== null) { this.setPagePreviousRecursiveAuthorityState(deletionType, null); } if (isRecursiveDeletion(deletionType)) { return; } const [recursiveState, setRecursiveState] = this.getRecursiveDeletionConfigState(deletionType); const calculableValue = prepareDeleteConfigValuesForCalc(newState, recursiveState); const shouldForceUpdate = !validateDeleteConfigs(calculableValue[0], calculableValue[1]); if (shouldForceUpdate) { setRecursiveState(newState); this.setPagePreviousRecursiveAuthorityState(deletionType, recursiveState); this.setExpantOtherDeleteOptionsState(deletionType, true); } return; } renderPageDeletePermissionDropdown(currentState, setState, deletionType, isButtonDisabled) { const { t } = this.props; return (
{ isRecursiveDeletion(deletionType) ? ( ) : ( ) }

{t(`security_settings.${getDeletionTypeForT(deletionType)}_explanation`)}

); } renderPageDeletePermission(currentState, setState, deletionType, isButtonDisabled) { const { t, adminGeneralSecurityContainer } = this.props; const expantDeleteOptionsState = this.expantDeleteOptionsState(deletionType); return (
{!isRecursiveDeletion(deletionType) && isTypeDeletion(deletionType) && ( {t('security_settings.page_delete')} )} {!isRecursiveDeletion(deletionType) && !isTypeDeletion(deletionType) && ( {t('security_settings.page_delete_completely')} )}
{ !isRecursiveDeletion(deletionType) ? ( <> {this.renderPageDeletePermissionDropdown(currentState, setState, deletionType, isButtonDisabled)} {currentState === PageDeleteConfigValue.Anyone && deletionType === DeletionType.CompleteDeletion && ( <> { adminGeneralSecurityContainer.switchIsAllGroupMembershipRequiredForPageCompleteDeletion() }} />

{t('security_settings.is_all_group_membership_required_for_page_complete_deletion_explanation')}

)} ) : ( <>

info {/* eslint-disable-next-line react/no-danger */}

{this.previousPageRecursiveAuthorityState(deletionType) !== null && (
{t('security_settings.forced_update_desc')} {t(getDeleteConfigValueForT(this.previousPageRecursiveAuthorityState(deletionType)))}
)} {this.renderPageDeletePermissionDropdown(currentState, setState, deletionType, isButtonDisabled)}
) }
); } render() { const { t, adminGeneralSecurityContainer } = this.props; const { currentRestrictGuestMode, currentPageDeletionAuthority, currentPageCompleteDeletionAuthority, currentPageRecursiveDeletionAuthority, currentPageRecursiveCompleteDeletionAuthority, isRomUserAllowedToComment, } = adminGeneralSecurityContainer.state; const isButtonDisabledForDeletion = !validateDeleteConfigs( adminGeneralSecurityContainer.state.currentPageDeletionAuthority, PageDeleteConfigValue.AdminAndAuthor, ); const isButtonDisabledForCompleteDeletion = !validateDeleteConfigs( adminGeneralSecurityContainer.state.currentPageCompleteDeletionAuthority, PageDeleteConfigValue.AdminAndAuthor, ); return (

{t('security_settings.security_settings')}

{adminGeneralSecurityContainer.retrieveError != null && (

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

)}

{t('security_settings.page_list_and_search_results')}

{/* Left Column: Labels */}
{t('public')}
{t('anyone_with_the_link')}
{t('only_me')}
{t('only_inside_the_group')}
{/* Right Column: Content */}
{t('security_settings.always_displayed')}
{t('security_settings.always_hidden')}
{/* Owner Restriction Dropdown */}
{/* Group Restriction Dropdown */}

{t('security_settings.page_access_rights')}

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

error FIXED

)}

{t('security_settings.page_delete_rights')}

{/* Render PageDeletePermission */} { [ [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_settings.user_homepage_deletion.user_homepage_deletion')}

{ adminGeneralSecurityContainer.switchIsUsersHomepageDeletionEnabled() }} />
{ adminGeneralSecurityContainer.switchIsForceDeleteUserHomepageOnUserDeletion() }} disabled={!adminGeneralSecurityContainer.state.isUsersHomepageDeletionEnabled} />

{t('security_settings.comment_manage_rights')}

{t('security_settings.readonly_users_access')}

{t('security_settings.session')}

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

info {t('security_settings.max_age_caution')}

); } } SecuritySetting.propTypes = { t: PropTypes.func.isRequired, // i18next adminGeneralSecurityContainer: PropTypes.instanceOf(AdminGeneralSecurityContainer).isRequired, }; const SecuritySettingWrapperFC = (props) => { const { t } = useTranslation('admin'); return ; }; const SecuritySettingWrapper = withUnstatedContainers(SecuritySettingWrapperFC, [AdminGeneralSecurityContainer]); export default SecuritySettingWrapper;