import { PRODUCT_JETPACK_SCAN, WPCOM_FEATURES_SCAN } from '@automattic/calypso-products'; import { Card } from '@automattic/components'; import { ToggleControl } from '@wordpress/components'; import { localize } from 'i18n-calypso'; import moment from 'moment'; import PropTypes from 'prop-types'; import { Component } from 'react'; import { connect } from 'react-redux'; import { UpsellNudge } from 'calypso/blocks/upsell-nudge'; import QueryJetpackConnection from 'calypso/components/data/query-jetpack-connection'; import FormFieldset from 'calypso/components/forms/form-fieldset'; import FormSettingExplanation from 'calypso/components/forms/form-setting-explanation'; import FormTextarea from 'calypso/components/forms/form-textarea'; import { activateModule } from 'calypso/state/jetpack/modules/actions'; import getJetpackModule from 'calypso/state/selectors/get-jetpack-module'; import isFetchingJetpackModules from 'calypso/state/selectors/is-fetching-jetpack-modules'; import isJetpackModuleActive from 'calypso/state/selectors/is-jetpack-module-active'; import isJetpackModuleUnavailableInDevelopmentMode from 'calypso/state/selectors/is-jetpack-module-unavailable-in-development-mode'; import isJetpackSiteInDevelopmentMode from 'calypso/state/selectors/is-jetpack-site-in-development-mode'; import siteHasFeature from 'calypso/state/selectors/site-has-feature'; import { getSelectedSiteId, getSelectedSiteSlug } from 'calypso/state/ui/selectors'; import AllowList from './allow-list'; import Protect from './protect'; import SettingsSectionHeader from './settings-section-header'; class Firewall extends Component { static propTypes = { fields: PropTypes.object.isRequired, handleSubmitForm: PropTypes.func.isRequired, isAtomic: PropTypes.bool.isRequired, isRequestingSettings: PropTypes.bool.isRequired, isSavingSettings: PropTypes.bool.isRequired, isSimple: PropTypes.bool.isRequired, isVip: PropTypes.bool.isRequired, onChangeField: PropTypes.func.isRequired, setFieldValue: PropTypes.func.isRequired, settings: PropTypes.object.isRequired, }; static defaultProps = { isSavingSettings: false, isRequestingSettings: true, fields: {}, settings: {}, }; disableForm() { return ( this.props.moduleDetailsLoading || this.props.isRequestingSettings || this.props.isSavingSettings ); } wafModuleSupported = () => { return ( this.props.firewallModuleAvailable && ! this.props.isAtomic && ! this.props.isVip && ! this.props.isSimple ); }; disableWafForm = () => { return this.disableForm() || ! this.wafModuleSupported(); }; ensureWafModuleActive = () => { if ( this.wafModuleSupported() && ! this.props.firewallModuleActive ) { this.props.activateModule( this.props.selectedSiteId, 'waf', true ); } }; hasAutomaticRulesInstalled = () => { return !! this.props.settings.jetpack_waf_automatic_rules_last_updated_timestamp; }; canUpdateAutomaticRules = () => { return this.props.hasRequiredFeature || this.hasAutomaticRulesInstalled(); }; automaticRulesLastUpdated = () => { const timestamp = parseInt( this.props.settings.jetpack_waf_automatic_rules_last_updated_timestamp ); if ( timestamp ) { return moment( timestamp * 1000 ).format( 'MMMM D, YYYY h:mm A' ); } return ''; }; handleAutomaticRulesToggle = ( event ) => { this.ensureWafModuleActive(); this.props.handleAutosavingToggle( 'jetpack_waf_automatic_rules' )( event ); }; handleBlockListToggle = ( event ) => { this.ensureWafModuleActive(); this.props.handleAutosavingToggle( 'jetpack_waf_ip_block_list_enabled' )( event ); }; handleSubmitForm = ( event ) => { this.ensureWafModuleActive(); return this.props.handleSubmitForm( event ); }; render() { const { dirtyFields, disableProtect, fields, handleAutosavingToggle, hasRequiredFeature, isRequestingSettings, isSavingSettings, onChangeField, selectedSiteId, selectedSiteSlug, setFieldValue, settings, translate, } = this.props; return (
{ /* Automatic Rules */ } { this.wafModuleSupported() && this.canUpdateAutomaticRules() && ( { this.automaticRulesLastUpdated() && ( { translate( 'Automatic security rules installed. Last updated on %(date)s.', { args: { date: this.automaticRulesLastUpdated(), }, } ) } ) } { translate( 'Block untrusted traffic sources by scanning every request made to your site. Jetpack’s advanced security rules are automatically kept up-to-date to protect your site from the latest threats.' ) } ) } { /* Upgrade Prompt */ } { ! hasRequiredFeature && this.wafModuleSupported() && ( ) } { /* Brute Force Login Protection */ } { /* IP Block List */ } { this.wafModuleSupported() && settings.jetpack_waf_ip_block_list_enabled !== undefined && ( { translate( 'IP addresses added to this list will be blocked from accessing your site.' ) } { fields.jetpack_waf_ip_block_list_enabled && (
) }
) } { /* IP Allow List */ }
); } } export default connect( ( state ) => { const selectedSiteSlug = getSelectedSiteSlug( state ); const selectedSiteId = getSelectedSiteId( state ); const moduleDetailsLoading = isFetchingJetpackModules( state, selectedSiteId ); const firewallModuleDetails = getJetpackModule( state, selectedSiteId, 'waf' ); const siteInDevMode = isJetpackSiteInDevelopmentMode( state, selectedSiteId ); const moduleUnavailableInDevMode = isJetpackModuleUnavailableInDevelopmentMode( state, selectedSiteId, 'waf' ); const hasRequiredFeature = siteHasFeature( state, selectedSiteId, WPCOM_FEATURES_SCAN ); return { selectedSiteId, selectedSiteSlug, firewallModuleActive: !! isJetpackModuleActive( state, selectedSiteId, 'waf' ), firewallModuleAvailable: firewallModuleDetails !== null && ( ! siteInDevMode || ! moduleUnavailableInDevMode ), moduleDetailsLoading, hasRequiredFeature, }; }, { activateModule, } )( localize( Firewall ) );