| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504 |
- /* eslint-disable react/no-danger */
- import React from 'react';
- import PropTypes from 'prop-types';
- import { useTranslation } from 'react-i18next';
- import { Collapse } from 'reactstrap';
- import AdminGeneralSecurityContainer from '~/client/services/AdminGeneralSecurityContainer';
- import { toastSuccess, toastError } from '~/client/util/apiNotification';
- 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_setting.anyone';
- case PageDeleteConfigValue.Inherit:
- return 'security_setting.inherit';
- case PageDeleteConfigValue.AdminOnly:
- return 'security_setting.admin_only';
- case PageDeleteConfigValue.AdminAndAuthor:
- return 'security_setting.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_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,
- ];
- }
- 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 (
- <div className="dropdown">
- <button
- className="btn btn-outline-secondary dropdown-toggle text-right"
- type="button"
- id="dropdownMenuButton"
- data-toggle="dropdown"
- aria-haspopup="true"
- aria-expanded="true"
- >
- <span className="float-left">
- {t(getDeleteConfigValueForT(currentState))}
- </span>
- </button>
- <div className="dropdown-menu" aria-labelledby="dropdownMenuButton">
- {
- isRecursiveDeletion(deletionType)
- ? (
- <button
- className="dropdown-item"
- type="button"
- onClick={() => { this.setDeletionConfigState(PageDeleteConfigValue.Inherit, setState, deletionType) }}
- >
- {t('security_setting.inherit')}
- </button>
- )
- : (
- <button
- className="dropdown-item"
- type="button"
- onClick={() => { this.setDeletionConfigState(PageDeleteConfigValue.Anyone, setState, deletionType) }}
- >
- {t('security_setting.anyone')}
- </button>
- )
- }
- <button
- className={`dropdown-item ${isButtonDisabled ? 'disabled' : ''}`}
- type="button"
- onClick={() => { this.setDeletionConfigState(PageDeleteConfigValue.AdminAndAuthor, setState, deletionType) }}
- >
- {t('security_setting.admin_and_author')}
- </button>
- <button
- className="dropdown-item"
- type="button"
- onClick={() => { this.setDeletionConfigState(PageDeleteConfigValue.AdminOnly, setState, deletionType) }}
- >
- {t('security_setting.admin_only')}
- </button>
- </div>
- <p className="form-text text-muted small">
- {t(`security_setting.${getDeletionTypeForT(deletionType)}_explain`)}
- </p>
- </div>
- );
- }
- renderPageDeletePermission(currentState, setState, deletionType, isButtonDisabled) {
- const { t } = this.props;
- const expantDeleteOptionsState = this.expantDeleteOptionsState(deletionType);
- return (
- <div key={`page-delete-permission-dropdown-${deletionType}`} className="row">
- <div className="col-md-3 text-md-right">
- {!isRecursiveDeletion(deletionType) && isTypeDeletion(deletionType) && (
- <strong>{t('security_setting.page_delete')}</strong>
- )}
- {!isRecursiveDeletion(deletionType) && !isTypeDeletion(deletionType) && (
- <strong>{t('security_setting.page_delete_completely')}</strong>
- )}
- </div>
- <div className="col-md-6">
- {
- !isRecursiveDeletion(deletionType)
- ? (
- <>{this.renderPageDeletePermissionDropdown(currentState, setState, deletionType, isButtonDisabled)}</>
- )
- : (
- <>
- <button
- type="button"
- className="btn btn-link p-0 mb-4"
- aria-expanded="false"
- onClick={() => this.setExpantOtherDeleteOptionsState(deletionType, !expantDeleteOptionsState)}
- >
- <i className={`fa fa-fw fa-arrow-right ${expantDeleteOptionsState ? 'fa-rotate-90' : ''}`}></i>
- { t('security_setting.other_options') }
- </button>
- <Collapse isOpen={expantDeleteOptionsState}>
- <div className="pb-4">
- <p className="card well">
- <span className="text-warning">
- <i className="icon-info"></i>
- {/* eslint-disable-next-line react/no-danger */}
- <span dangerouslySetInnerHTML={{ __html: t('security_setting.page_delete_rights_caution') }} />
- </span>
- </p>
- { this.previousPageRecursiveAuthorityState(deletionType) !== null && (
- <div className="mb-3">
- <strong>
- {t('security_setting.forced_update_desc')}
- </strong>
- <code>
- {t(getDeleteConfigValueForT(this.previousPageRecursiveAuthorityState(deletionType)))}
- </code>
- </div>
- )}
- {this.renderPageDeletePermissionDropdown(currentState, setState, deletionType, isButtonDisabled)}
- </div>
- </Collapse>
- </>
- )
- }
- </div>
- </div>
- );
- }
- 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 (
- <React.Fragment>
- <h2 className="alert-anchor border-bottom">
- {t('security_settings')}
- </h2>
- {adminGeneralSecurityContainer.retrieveError != null && (
- <div className="alert alert-danger">
- <p>{t('Error occurred')} : {adminGeneralSecurityContainer.retrieveError}</p>
- </div>
- )}
- <h4 className="mt-4">{ t('security_setting.page_list_and_search_results') }</h4>
- <div className="row justify-content-md-center">
- <table className="table table-bordered col-lg-9 mb-5">
- <thead>
- <tr>
- <th scope="col">{ t('scope_of_page_disclosure') }</th>
- <th scope="col">{ t('set_point') }</th>
- </tr>
- </thead>
- <tbody>
- <tr>
- <th scope="row">{ t('Public') }</th>
- <td>{ t('always_displayed') }</td>
- </tr>
- <tr>
- <th scope="row">{ t('Anyone with the link') }</th>
- <td>{ t('always_hidden') }</td>
- </tr>
- <tr>
- <th scope="row">{ t('Only me') }</th>
- <td>
- <div className="custom-control custom-switch custom-checkbox-success">
- <input
- type="checkbox"
- className="custom-control-input"
- id="isShowRestrictedByOwner"
- checked={adminGeneralSecurityContainer.state.isShowRestrictedByOwner}
- onChange={() => { adminGeneralSecurityContainer.switchIsShowRestrictedByOwner() }}
- />
- <label className="custom-control-label" htmlFor="isShowRestrictedByOwner">
- {t('displayed_or_hidden')}
- </label>
- </div>
- </td>
- </tr>
- <tr>
- <th scope="row">{ t('Only inside the group') }</th>
- <td>
- <div className="custom-control custom-switch custom-checkbox-success">
- <input
- type="checkbox"
- className="custom-control-input"
- id="isShowRestrictedByGroup"
- checked={adminGeneralSecurityContainer.state.isShowRestrictedByGroup}
- onChange={() => { adminGeneralSecurityContainer.switchIsShowRestrictedByGroup() }}
- />
- <label className="custom-control-label" htmlFor="isShowRestrictedByGroup">
- {t('displayed_or_hidden')}
- </label>
- </div>
- </td>
- </tr>
- </tbody>
- </table>
- </div>
- <h4>{t('security_setting.page_access_rights')}</h4>
- <div className="row mb-4">
- <div className="col-md-3 text-md-right py-2">
- <strong>{t('security_setting.Guest Users Access')}</strong>
- </div>
- <div className="col-md-9">
- <div className="dropdown">
- <button
- className={`btn btn-outline-secondary dropdown-toggle text-right col-12
- col-md-auto ${adminGeneralSecurityContainer.isWikiModeForced && 'disabled'}`}
- type="button"
- id="dropdownMenuButton"
- data-toggle="dropdown"
- aria-haspopup="true"
- aria-expanded="true"
- >
- <span className="float-left">
- {currentRestrictGuestMode === 'Deny' && t('security_setting.guest_mode.deny')}
- {currentRestrictGuestMode === 'Readonly' && t('security_setting.guest_mode.readonly')}
- </span>
- </button>
- <div className="dropdown-menu" aria-labelledby="dropdownMenuButton">
- <button className="dropdown-item" type="button" onClick={() => { adminGeneralSecurityContainer.changeRestrictGuestMode('Deny') }}>
- {t('security_setting.guest_mode.deny')}
- </button>
- <button className="dropdown-item" type="button" onClick={() => { adminGeneralSecurityContainer.changeRestrictGuestMode('Readonly') }}>
- {t('security_setting.guest_mode.readonly')}
- </button>
- </div>
- </div>
- {adminGeneralSecurityContainer.isWikiModeForced && (
- <p className="alert alert-warning mt-2 col-6">
- <i className="icon-exclamation icon-fw">
- </i><b>FIXED</b><br />
- <b
- dangerouslySetInnerHTML={{
- __html: t('security_setting.Fixed by env var',
- { key: 'FORCE_WIKI_MODE', value: adminGeneralSecurityContainer.state.wikiMode }),
- }}
- />
- </p>
- )}
- </div>
- </div>
- <h4>{t('security_setting.page_delete_rights')}</h4>
- {/* 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]))
- }
- <h4>{t('security_setting.session')}</h4>
- <div className="form-group row">
- <label className="text-left text-md-right col-md-3 col-form-label">{t('security_setting.max_age')}</label>
- <div className="col-md-6">
- <input
- className="form-control col-md-3"
- type="text"
- defaultValue={adminGeneralSecurityContainer.state.sessionMaxAge || ''}
- onChange={(e) => {
- adminGeneralSecurityContainer.setSessionMaxAge(e.target.value);
- }}
- placeholder="2592000000"
- />
- {/* eslint-disable-next-line react/no-danger */}
- <p className="form-text text-muted" dangerouslySetInnerHTML={{ __html: t('security_setting.max_age_desc') }} />
- <p className="card well">
- <span className="text-warning">
- <i className="icon-info"></i> {t('security_setting.max_age_caution')}
- </span>
- </p>
- </div>
- </div>
- <div className="row my-3">
- <div className="text-center text-md-left offset-md-3 col-md-5">
- <button type="button" className="btn btn-primary" disabled={adminGeneralSecurityContainer.retrieveError != null} onClick={this.putSecuritySetting}>
- {t('Update')}
- </button>
- </div>
- </div>
- </React.Fragment>
- );
- }
- }
- SecuritySetting.propTypes = {
- t: PropTypes.func.isRequired, // i18next
- adminGeneralSecurityContainer: PropTypes.instanceOf(AdminGeneralSecurityContainer).isRequired,
- };
- const SecuritySettingWrapperFC = (props) => {
- const { t } = useTranslation();
- return <SecuritySetting t={t} {...props} />;
- };
- const SecuritySettingWrapper = withUnstatedContainers(SecuritySettingWrapperFC, [AdminGeneralSecurityContainer]);
- export default SecuritySettingWrapper;
|