Skip to content

Commit

Permalink
Account Protection: Remove strict mode (#41316)
Browse files Browse the repository at this point in the history
* Add Account Protection toggle to Jetpack security settings

* Import package and run activation/deactivation on module toggle

* changelog

* Add Protect Settings page and hook up Account Protection toggle

* changelog

* Update changelog

* Register modules on plugin activation

* Ensure package is initialized on plugin activation

* Make account protection class init static

* Add auth hooks, redirect and a custom login action template

* Reorg, add Password_Detection class

* Remove user cxn req and banner

* Do not enabled module by default

* Add strict mode option and settings toggle

* changelog

* Add strict mode toggle

* Add strict mode toggle and endpoints

* Reorg and add kill switch and is supported check

* Add testing infrastructure

* Add email handlings, resend AJAX action, and attempt limitations

* Add nonces, checks and template error handling

* Use method over template to avoid lint errors

* Improve render_password_detection_template, update SVG file ext

* Remove template file and include

* Prep for validation endpoints

* Update classes to be dynamic

* Add constructors

* Reorg user meta methods

* Add type declarations and hinting

* Simplify method naming

* Use dynamic classes

* Update class dependencies

* Fix copy

* Revert unrelated changes

* Revert unrelated changes

* Fix method calls

* Do not activate by default

* Fix phan errors

* Changelog

* Update composer deps

* Update lock files, add constructor method

* Fix php warning

* Update lock file

* Changelog

* Fix Password_Detection constructor

* Changelog

* More changelogs

* Remove comments

* Fix static analysis errors

* Remove top level phpunit.xml.dist

* Remove never return type

* Revert tests dir changes in favour of a dedicated task

* Add tests dir

* Reapply default test infrastructure

* Reorg and rename

* Update @Package

* Use never phpdoc return type as per static analysis error

* Enable module by default

* Enable module by default

* Remove all reference to and functionality of strict mode

* Remove unneeded strict mode code, update Protect settings UI

* Updates/fixes

* Fix import

* Update placeholder content

* Revert unrelated changes

* Remove missed code
  • Loading branch information
dkmyta authored Jan 29, 2025
1 parent 1ec2c34 commit 639a306
Show file tree
Hide file tree
Showing 22 changed files with 45 additions and 685 deletions.
10 changes: 0 additions & 10 deletions projects/js-packages/api/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -510,16 +510,6 @@ function JetpackRestApiClient( root, nonce ) {
getRequest( `${ wpcomOriginApiUrl }jetpack/v4/search/stats`, getParams )
.then( checkStatus )
.then( parseJsonResponse ),
fetchAccountProtectionSettings: () =>
getRequest( `${ apiRoot }jetpack/v4/account-protection`, getParams )
.then( checkStatus )
.then( parseJsonResponse ),
updateAccountProtectionSettings: newSettings =>
postRequest( `${ apiRoot }jetpack/v4/account-protection`, postParams, {
body: JSON.stringify( newSettings ),
} )
.then( checkStatus )
.then( parseJsonResponse ),
fetchWafSettings: () =>
getRequest( `${ apiRoot }jetpack/v4/waf`, getParams )
.then( checkStatus )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
class Account_Protection {
const PACKAGE_VERSION = '0.1.0-alpha';
const ACCOUNT_PROTECTION_MODULE_NAME = 'account-protection';
const STRICT_MODE_OPTION_NAME = 'jetpack_account_protection_strict_mode';

/**
* Modules instance.
Expand Down Expand Up @@ -64,9 +63,6 @@ private function register_hooks(): void {
// Do not run in unsupported environments
add_action( 'jetpack_get_available_modules', array( $this, 'remove_module_on_unsupported_environments' ) );
add_action( 'jetpack_get_available_standalone_modules', array( $this, 'remove_standalone_module_on_unsupported_environments' ) );

// Register REST routes
add_action( 'rest_api_init', array( new REST_Controller(), 'register_rest_routes' ) );
}

/**
Expand Down Expand Up @@ -189,17 +185,4 @@ function ( $module ) {

return $modules;
}

/**
* Get the account protection settings.
*
* @return array
*/
public function get_settings(): array {
$settings = array(
self::STRICT_MODE_OPTION_NAME => get_option( self::STRICT_MODE_OPTION_NAME, false ),
);

return $settings;
}
}
104 changes: 0 additions & 104 deletions projects/packages/account-protection/src/class-rest-controller.php

This file was deleted.

144 changes: 2 additions & 142 deletions projects/plugins/jetpack/_inc/client/security/account-protection.jsx
Original file line number Diff line number Diff line change
@@ -1,108 +1,22 @@
import { ToggleControl } from '@automattic/jetpack-components';
import { ExternalLink } from '@wordpress/components';
import { createInterpolateElement } from '@wordpress/element';
import { __, _x } from '@wordpress/i18n';
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { FormFieldset } from 'components/forms';
import { createNotice, removeNotice } from 'components/global-notices/state/notices/actions';
import { withModuleSettingsFormHelpers } from 'components/module-settings/with-module-settings-form-helpers';
import { ModuleToggle } from 'components/module-toggle';
import SettingsCard from 'components/settings-card';
import SettingsGroup from 'components/settings-group';
import QueryAccountProtectionSettings from '../components/data/query-account-protection-settings';
import InfoPopover from '../components/info-popover';
import { FEATURE_JETPACK_ACCOUNT_PROTECTION } from '../lib/plans/constants';
import { updateAccountProtectionSettings } from '../state/account-protection/actions';
import {
getAccountProtectionSettings,
isFetchingAccountProtectionSettings,
isUpdatingAccountProtectionSettings,
} from '../state/account-protection/reducer';

const AccountProtection = class extends Component {
/**
* Get options for initial state.
*
* @return {object}
*/
state = {
strictMode: this.props.settings?.strictMode,
};

/**
* Keep the form values in sync with updates to the settings prop.
*
* @param {object} prevProps - Next render props.
*/
componentDidUpdate = prevProps => {
// Sync the form values with the settings prop.
if ( this.props.settings !== prevProps.settings ) {
this.setState( {
...this.state,
strictMode: this.props.settings?.strictMode,
} );
}
};

/**
* Handle settings updates.
*
* @return {void}
*/
onSubmit = () => {
this.props.removeNotice( 'module-setting-update' );
this.props.removeNotice( 'module-setting-update-success' );

this.props.createNotice( 'is-info', __( 'Updating settings…', 'jetpack' ), {
id: 'module-setting-update',
} );
this.props
.updateAccountProtectionSettings( this.state )
.then( () => {
this.props.removeNotice( 'module-setting-update' );
this.props.createNotice( 'is-success', __( 'Updated Settings.', 'jetpack' ), {
id: 'module-setting-update-success',
} );
} )
.catch( () => {
this.props.removeNotice( 'module-setting-update' );
this.props.createNotice( 'is-error', __( 'Error updating settings.', 'jetpack' ), {
id: 'module-setting-update',
} );
} );
};

/**
* Toggle strict mode.
*/
toggleStrictMode = () => {
const state = {
...this.state,
strictMode: ! this.state.strictMode,
};

this.setState( state, this.onSubmit );
};

const AccountProtectionComponent = class extends Component {
render() {
const isAccountProtectionActive = this.props.getOptionValue( 'account-protection' ),
unavailableInOfflineMode = this.props.isUnavailableInOfflineMode( 'account-protection' );
const baseInputDisabledCase =
! isAccountProtectionActive ||
unavailableInOfflineMode ||
this.props.isFetchingAccountProtectionSettings ||
this.props.isSavingAnyOption( [ 'account-protection' ] );

return (
<SettingsCard
{ ...this.props }
module="account-protection"
header={ _x( 'Account protection', 'Settings header', 'jetpack' ) }
hideButton={ true }
feature={ FEATURE_JETPACK_ACCOUNT_PROTECTION }
>
{ isAccountProtectionActive && <QueryAccountProtectionSettings /> }
<SettingsGroup
hasChild
disableInOfflineMode
Expand All @@ -128,64 +42,10 @@ const AccountProtection = class extends Component {
) }
</span>
</ModuleToggle>
{ isAccountProtectionActive && (
<FormFieldset className="account-protection__settings">
<div className="account-protection__settings__toggle-setting">
<ToggleControl
checked={ this.props.settings?.strictMode }
disabled={ baseInputDisabledCase }
toggling={
this.props.isUpdatingAccountProtectionSettings &&
this.state.strictMode !== this.props.settings?.strictMode
}
onChange={ this.toggleStrictMode }
label={
<div className="account-protection__settings__toggle-setting__label">
<span className="jp-form-toggle-explanation">
{ __( 'Require strong passwords', 'jetpack' ) }
</span>
<InfoPopover
position="right"
screenReaderText={ __( 'Learn more', 'jetpack' ) }
className="account-protection__settings__strict-mode-popover"
>
{ createInterpolateElement(
__(
'Allow Jetpack to enforce strict password rules. <ExternalLink>Learn more</ExternalLink> <hr /> <ExternalLink>Privacy Information</ExternalLink>',
'jetpack'
),
{
ExternalLink: <ExternalLink href="#" />, // TODO: Update this redirect URL
hr: <hr />,
}
) }
</InfoPopover>
</div>
}
/>
</div>
</FormFieldset>
) }
</SettingsGroup>
</SettingsCard>
);
}
};

export default connect(
state => {
return {
isFetchingSettings: isFetchingAccountProtectionSettings( state ),
isUpdatingAccountProtectionSettings: isUpdatingAccountProtectionSettings( state ),
settings: getAccountProtectionSettings( state ),
};
},
dispatch => {
return {
updateAccountProtectionSettings: newSettings =>
dispatch( updateAccountProtectionSettings( newSettings ) ),
createNotice: ( type, message, props ) => dispatch( createNotice( type, message, props ) ),
removeNotice: notice => dispatch( removeNotice( notice ) ),
};
}
)( withModuleSettingsFormHelpers( AccountProtection ) );
export const AccountProtection = withModuleSettingsFormHelpers( AccountProtectionComponent );
2 changes: 1 addition & 1 deletion projects/plugins/jetpack/_inc/client/security/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { isModuleFound } from 'state/search';
import { getSettings } from 'state/settings';
import { siteHasFeature } from 'state/site';
import { isPluginActive, isPluginInstalled } from 'state/site/plugins';
import AccountProtection from './account-protection';
import { AccountProtection } from './account-protection';
import AllowList from './allowList';
import Antispam from './antispam';
import BackupsScan from './backups-scan';
Expand Down
20 changes: 0 additions & 20 deletions projects/plugins/jetpack/_inc/client/security/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -192,23 +192,3 @@
.jp-form-settings-group p {
margin-bottom: 0.5rem;
}

.account-protection__settings {
&__toggle-setting {
flex-wrap: wrap;
display: flex;
margin-bottom: 24px;

&__label {
display: flex;
align-items: center;
}
}

&__strict-mode-popover {
display: flex;
align-items: center;
margin-left: 4px;
}

}
Loading

0 comments on commit 639a306

Please sign in to comment.