Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jetpack: Add Account Protection security settings #40938

Merged
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Adds Account Protection requests
10 changes: 10 additions & 0 deletions projects/js-packages/api/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,16 @@ 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
@@ -1,16 +1,81 @@
<?php
/**
* Package description here
* Class used to define Account Protection.
*
* @package automattic/jetpack-account-protection
*/

namespace Automattic\Jetpack;
namespace Automattic\Jetpack\Account_Protection;

use Automattic\Jetpack\Modules;
dkmyta marked this conversation as resolved.
Show resolved Hide resolved

/**
* Class description.
* Class Account_Protection
*/
class Account_Protection {

const PACKAGE_VERSION = '0.1.0-alpha';
const PACKAGE_VERSION = '0.1.0-alpha';
const ACCOUNT_PROTECTION_MODULE_NAME = 'account-protection';
const STRICT_MODE_OPTION_NAME = 'jetpack_account_protection_strict_mode';

/**
* Initializes the configurations needed for the account protection module.
*/
public function init() {
// Account protection activation/deactivation hooks
add_action( 'jetpack_activate_module_' . self::ACCOUNT_PROTECTION_MODULE_NAME, array( $this, 'on_account_protection_activation' ) );
add_action( 'jetpack_deactivate_module_' . self::ACCOUNT_PROTECTION_MODULE_NAME, array( $this, 'on_account_protection_deactivation' ) );

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

/**
* Activate the account protection on module activation.
*/
public function on_account_protection_activation() {
// Account protection activated
}

/**
* Deactivate the account protection on module activation.
*/
public function on_account_protection_deactivation() {
// Account protection deactivated
}

/**
* Determines if the account protection module is enabled on the site.
*
* @return bool
*/
public function is_enabled() {
return ( new Modules() )->is_active( self::ACCOUNT_PROTECTION_MODULE_NAME );

Check failure on line 53 in projects/packages/account-protection/src/class-account-protection.php

View workflow job for this annotation

GitHub Actions / Static analysis

UndefError PhanUndeclaredClassMethod Call to method __construct from undeclared class \Automattic\Jetpack\Modules

Check failure on line 53 in projects/packages/account-protection/src/class-account-protection.php

View workflow job for this annotation

GitHub Actions / Static analysis

UndefError PhanUndeclaredClassMethod Call to method is_active from undeclared class \Automattic\Jetpack\Modules
}

/**
* Enables the account protection module.
*
* @return bool
*/
public function enable() {
// Return true if already enabled.
if ( $this->is_enabled() ) {
return true;
}
return ( new Modules() )->activate( self::ACCOUNT_PROTECTION_MODULE_NAME, false, false );

Check failure on line 66 in projects/packages/account-protection/src/class-account-protection.php

View workflow job for this annotation

GitHub Actions / Static analysis

UndefError PhanUndeclaredClassMethod Call to method __construct from undeclared class \Automattic\Jetpack\Modules

Check failure on line 66 in projects/packages/account-protection/src/class-account-protection.php

View workflow job for this annotation

GitHub Actions / Static analysis

UndefError PhanUndeclaredClassMethod Call to method activate from undeclared class \Automattic\Jetpack\Modules
}

/**
* Disables the account protection module.
*
* @return bool
*/
public function disable() {
// Return true if already disabled.
if ( ! $this->is_enabled() ) {
return true;
}
return ( new Modules() )->deactivate( self::ACCOUNT_PROTECTION_MODULE_NAME );

Check failure on line 79 in projects/packages/account-protection/src/class-account-protection.php

View workflow job for this annotation

GitHub Actions / Static analysis

UndefError PhanUndeclaredClassMethod Call to method __construct from undeclared class \Automattic\Jetpack\Modules

Check failure on line 79 in projects/packages/account-protection/src/class-account-protection.php

View workflow job for this annotation

GitHub Actions / Static analysis

UndefError PhanUndeclaredClassMethod Call to method deactivate from undeclared class \Automattic\Jetpack\Modules
}
}
106 changes: 106 additions & 0 deletions projects/packages/account-protection/src/class-rest-controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
/**
* Class use to register REST API endpoints used by the Accont Protection module.
*
* @package automattic/jetpack-waf
*/

namespace Automattic\Jetpack\Account_Protection;

use Automattic\Jetpack\Connection\REST_Connector;
use WP_Error;
use WP_REST_Request;
use WP_REST_Response;
use WP_REST_Server;

/**
* Defines our endponts.
*/
class REST_Controller {
/**
* Tracks whether routes have already been registered.
*
* @var bool
*/
private $routes_registered = false;

/**
* Register REST API endpoints.
*
* @return void
*/
public function register_rest_routes() {
// Ensure routes are only initialized once.
if ( $this->routes_registered ) {
return;
}

register_rest_route(
'jetpack/v4',
'/account-protection',
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_settings' ),
'permission_callback' => array( $this, 'permissions_callback' ),
)
);

register_rest_route(
'jetpack/v4',
'/account-protection',
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array( $this, 'update_settings' ),
'permission_callback' => array( $this, 'permissions_callback' ),
)
);

$this->routes_registered = true;
}

/**
* Account Protection Settings Endpoint
*
* @return WP_REST_Response
*/
public function get_settings() {
return rest_ensure_response(
array(
Account_Protection::STRICT_MODE_OPTION_NAME => get_option( Account_Protection::STRICT_MODE_OPTION_NAME ),
)
);
}

/**
* Update Account Protection Settings Endpoint
*
* @param WP_REST_Request $request The API request.
*
* @return WP_REST_Response|WP_Error
*/
public function update_settings( $request ) {
// Strict Mode
if ( isset( $request[ Account_Protection::STRICT_MODE_OPTION_NAME ] ) ) {
update_option( Account_Protection::STRICT_MODE_OPTION_NAME, $request[ Account_Protection::STRICT_MODE_OPTION_NAME ] ? '1' : '' );
}

return $this->get_settings();
}

/**
* Account Protection Endpoint Permissions Callback
*
* @return bool|WP_Error True if user can view the Jetpack admin page.
*/
public function permissions_callback() {
if ( current_user_can( 'manage_options' ) ) {
return true;
}

return new WP_Error(
'invalid_user_permission_manage_options',
REST_Connector::get_user_permissions_error_msg(),

Check failure on line 102 in projects/packages/account-protection/src/class-rest-controller.php

View workflow job for this annotation

GitHub Actions / Static analysis

UndefError PhanUndeclaredClassMethod Call to method get_user_permissions_error_msg from undeclared class \Automattic\Jetpack\Connection\REST_Connector
array( 'status' => rest_authorization_required_code() )
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import PropTypes from 'prop-types';
import { Component } from 'react';
import { connect } from 'react-redux';
import {
fetchAccountProtectionSettings,
isFetchingAccountProtectionSettings,
} from 'state/account-protection';
import { isOfflineMode } from 'state/connection';

class QueryAccountProtectionSettings extends Component {
static propTypes = {
isFetchingAccountProtectionSettings: PropTypes.bool,
isOfflineMode: PropTypes.bool,
};

static defaultProps = {
isFetchingAccountProtectionSettings: false,
isOfflineMode: false,
};

componentDidMount() {
if ( ! this.props.isFetchingAccountProtectionSettings && ! this.props.isOfflineMode ) {
this.props.fetchAccountProtectionSettings();
}
}

render() {
return null;
}
}

export default connect(
state => {
return {
isFetchingAccountProtectionSettings: isFetchingAccountProtectionSettings( state ),
isOfflineMode: isOfflineMode( state ),
};
},
dispatch => {
return {
fetchAccountProtectionSettings: () => dispatch( fetchAccountProtectionSettings() ),
};
}
)( QueryAccountProtectionSettings );
2 changes: 2 additions & 0 deletions projects/plugins/jetpack/_inc/client/lib/plans/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ export const FEATURE_POST_BY_EMAIL = 'post-by-email-jetpack';
export const FEATURE_JETPACK_SOCIAL = 'social-jetpack';
export const FEATURE_JETPACK_BLAZE = 'blaze-jetpack';
export const FEATURE_JETPACK_EARN = 'earn-jetpack';
export const FEATURE_JETPACK_ACCOUNT_PROTECTION = 'account-protection-jetpack';

// Upsells
export const JETPACK_FEATURE_PRODUCT_UPSELL_MAP = {
Expand All @@ -439,6 +440,7 @@ export const JETPACK_FEATURE_PRODUCT_UPSELL_MAP = {
[ FEATURE_VIDEOPRESS ]: PLAN_JETPACK_VIDEOPRESS,
[ FEATURE_NEWSLETTER_JETPACK ]: PLAN_JETPACK_CREATOR_YEARLY,
[ FEATURE_WORDADS_JETPACK ]: PLAN_JETPACK_SECURITY_T1_YEARLY,
[ FEATURE_JETPACK_ACCOUNT_PROTECTION ]: PLAN_JETPACK_FREE,
};

/**
Expand Down
Loading
Loading