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

Move actions from methods to functions for profile delete and change email #751

Merged
merged 2 commits into from
Dec 17, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
33 changes: 22 additions & 11 deletions WP_Auth0.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,24 +126,13 @@ public function init() {
$initial_setup = new WP_Auth0_InitialSetup( $this->a0_options );
$initial_setup->init();

$users_repo = new WP_Auth0_UsersRepo( $this->a0_options );

$this->router = new WP_Auth0_Routes( $this->a0_options );

$error_log = new WP_Auth0_ErrorLog();
$error_log->init();

$import_settings = new WP_Auth0_Import_Settings( $this->a0_options );
$import_settings->init();

$api_client_creds = new WP_Auth0_Api_Client_Credentials( $this->a0_options );

$api_change_email = new WP_Auth0_Api_Change_Email( $this->a0_options, $api_client_creds );
$profile_change_email = new WP_Auth0_Profile_Change_Email( $api_change_email );
$profile_change_email->init();

$profile_delete_data = new WP_Auth0_Profile_Delete_Data( $users_repo );
$profile_delete_data->init();
}

/**
Expand Down Expand Up @@ -494,6 +483,15 @@ function wp_auth0_db_check_update() {
* Core WP hooks
*/

function wp_auth0_profile_change_email( $wp_user_id, $old_user_data ) {
$options = WP_Auth0_Options::Instance();
$api_client_creds = new WP_Auth0_Api_Client_Credentials( $options );
$api_change_email = new WP_Auth0_Api_Change_Email( $options, $api_client_creds );
$profile_change_email = new WP_Auth0_Profile_Change_Email( $api_change_email );
return $profile_change_email->update_email( $wp_user_id, $old_user_data );
}
add_action( 'profile_update', 'wp_auth0_profile_change_email', 100, 2 );

function wp_auth0_validate_new_password( $errors, $user ) {
$options = WP_Auth0_Options::Instance();
$api_client_creds = new WP_Auth0_Api_Client_Credentials( $options );
Expand All @@ -511,6 +509,19 @@ function wp_auth0_validate_new_password( $errors, $user ) {
// Used during WooCommerce edit account save.
add_action( 'woocommerce_save_account_details_errors', 'wp_auth0_validate_new_password', 10, 2 );

function wp_auth0_show_delete_identity() {
$profile_delete_data = new WP_Auth0_Profile_Delete_Data();
$profile_delete_data->show_delete_identity();
}
add_action( 'edit_user_profile', 'wp_auth0_show_delete_identity' );
add_action( 'show_user_profile', 'wp_auth0_show_delete_identity' );

function wp_auth0_delete_user_data() {
$profile_delete_data = new WP_Auth0_Profile_Delete_Data();
$profile_delete_data->delete_user_data();
}
add_action( 'wp_ajax_auth0_delete_data', 'wp_auth0_delete_user_data' );

function wp_auth0_init_admin_menu() {

if ( isset( $_REQUEST['page'] ) && $_REQUEST['page'] === 'wpa0-help' ) {
Expand Down
10 changes: 10 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,16 @@ function wp_auth0_url_base64_decode( $input ) {
return base64_decode( strtr( $input, '-_', '+/' ) );
}

/**
* @param $user_id
*/
function wp_auth0_delete_auth0_object( $user_id ) {
WP_Auth0_UsersRepo::delete_meta( $user_id, 'auth0_id' );
WP_Auth0_UsersRepo::delete_meta( $user_id, 'auth0_obj' );
WP_Auth0_UsersRepo::delete_meta( $user_id, 'last_update' );
WP_Auth0_UsersRepo::delete_meta( $user_id, 'auth0_transient_email_update' );
}

if ( ! function_exists( 'get_auth0userinfo' ) ) {
/**
* Get the Auth0 profile from the database, if one exists.
Expand Down
12 changes: 0 additions & 12 deletions lib/WP_Auth0_UsersRepo.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,6 @@ public function update_auth0_object( $user_id, $userinfo ) {
self::update_meta( $user_id, 'last_update', date( 'c' ) );
}

/**
* Delete all Auth0 meta fields for a WordPress user.
*
* @param int $user_id - WordPress user ID.
*/
public function delete_auth0_object( $user_id ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved this to wp_auth0_delete_auth0_object() to avoid an unnecessary dependency injection.

self::delete_meta( $user_id, 'auth0_id' );
self::delete_meta( $user_id, 'auth0_obj' );
self::delete_meta( $user_id, 'last_update' );
self::delete_meta( $user_id, 'auth0_transient_email_update' );
}

/**
* Get a user's Auth0 meta data.
*
Expand Down
13 changes: 0 additions & 13 deletions lib/profile/WP_Auth0_Profile_Change_Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,6 @@ public function __construct( WP_Auth0_Api_Change_Email $api_change_email ) {
$this->api_change_email = $api_change_email;
}

/**
* Add actions for the update user process.
*
* @deprecated - 3.10.0, will move add_action calls out of this class in the next major.
*
* @codeCoverageIgnore - Deprecated.
*/
public function init() {

// Used during profile update in wp-admin or email verification link.
add_action( 'profile_update', [ $this, 'update_email' ], 100, 2 );
}

/**
* Update the user's email at Auth0 when changing email for a database connection user.
* This runs AFTER a successful email change is saved in WP.
Expand Down
31 changes: 1 addition & 30 deletions lib/profile/WP_Auth0_Profile_Delete_Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,6 @@
*/
class WP_Auth0_Profile_Delete_Data {

/**
* WP_Auth0_UsersRepo instance.
*
* @var WP_Auth0_UsersRepo
*/
protected $users_repo;

/**
* WP_Auth0_Profile_Delete_Data constructor.
*
* @param WP_Auth0_UsersRepo $users_repo - WP_Auth0_UsersRepo instance.
*/
public function __construct( WP_Auth0_UsersRepo $users_repo ) {
$this->users_repo = $users_repo;
}

/**
* Add actions and filters for the profile page.
*
* @deprecated - 3.10.0, will move add_action calls out of this class in the next major.
*
* @codeCoverageIgnore - Deprecated.
*/
public function init() {
add_action( 'edit_user_profile', [ $this, 'show_delete_identity' ] );
add_action( 'show_user_profile', [ $this, 'show_delete_identity' ] );
add_action( 'wp_ajax_auth0_delete_data', [ $this, 'delete_user_data' ] );
}

/**
* Show the delete Auth0 user data button.
* Hooked to: edit_user_profile, show_user_profile
Expand Down Expand Up @@ -95,7 +66,7 @@ public function delete_user_data() {
wp_send_json_error( [ 'error' => __( 'Forbidden', 'wp-auth0' ) ] );
}

$this->users_repo->delete_auth0_object( $user_id );
wp_auth0_delete_auth0_object( $user_id );
wp_send_json_success();
}
}
62 changes: 13 additions & 49 deletions tests/testProfileChangeEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,31 @@ public static function setUpBeforeClass() {
*/
public function testThatHooksAreLoaded() {
$expect_hooked = [
'update_email' => [
'wp_auth0_profile_change_email' => [
'priority' => 100,
'accepted_args' => 2,
],
];
$this->assertHookedClass( 'profile_update', 'WP_Auth0_Profile_Change_Email', $expect_hooked );
$this->assertHookedFunction( 'profile_update', $expect_hooked );
}

/**
* Test that an email update works.
*/
public function testSuccessfulEmailUpdate() {
$this->startHttpMocking();
$this->setApiToken( 'update:users' );
$this->http_request_type = 'success_empty_body';

$user = $this->createUser( [], false );
$new_email = $user->data->user_email;
$old_user = clone $user;
$old_user->data->user_email = 'OLD-' . $new_email;

// API call mocked to succeed.
$change_email = $this->getStub( true );

// Store userinfo for a DB strategy user.
$this->storeAuth0Data( $user->ID, 'auth0' );

$this->assertTrue( $change_email->update_email( $user->ID, $old_user ) );
$this->assertTrue( wp_auth0_profile_change_email( $user->ID, $old_user ) );
$this->assertEquals( $new_email, get_user_by( 'id', $user->ID )->data->user_email );
$this->assertEmpty( WP_Auth0_UsersRepo::get_meta( $user->ID, 'auth0_transient_email_update' ) );
}
Expand All @@ -77,10 +78,7 @@ public function testThatNonAuth0UserSkipsUpdate() {
$old_user = clone $user;
$old_user->data->user_email = 'OLD-' . $old_user->data->user_email;

// API call mocked to succeed.
$change_email = $this->getStub( true );

$this->assertFalse( $change_email->update_email( $user->ID, $old_user ) );
$this->assertFalse( wp_auth0_profile_change_email( $user->ID, $old_user ) );
}

/**
Expand All @@ -91,13 +89,10 @@ public function testThatNonDbUserSkipsUpdate() {
$old_user = clone $user;
$old_user->data->user_email = 'OLD-' . $old_user->data->user_email;

// API call mocked to succeed.
$change_email = $this->getStub( true );

// Store userinfo for a DB strategy user.
$this->storeAuth0Data( $user->ID, 'not-auth0' );

$this->assertFalse( $change_email->update_email( $user->ID, $old_user ) );
$this->assertFalse( wp_auth0_profile_change_email( $user->ID, $old_user ) );
}

/**
Expand All @@ -107,13 +102,10 @@ public function testThatSameEmailSkipsUpdate() {
$user = $this->createUser( [], false );
$old_user = clone $user;

// API call mocked to succeed.
$change_email = $this->getStub( true );

// Store userinfo for a DB strategy user.
$this->storeAuth0Data( $user->ID, 'not-auth0' );

$this->assertFalse( $change_email->update_email( $user->ID, $old_user ) );
$this->assertFalse( wp_auth0_profile_change_email( $user->ID, $old_user ) );
}

/**
Expand All @@ -130,14 +122,10 @@ public function testThatFailedApiCallStopsEmailUpdate() {
// Store the usermeta value set for email verification.
update_user_meta( $user->ID, '_new_email', $user->data->user_email );

// API call mocked to fail.
$change_email = $this->getStub( false );

// Need to remove existing filters and re-init with filters from the test class.
remove_all_filters( 'profile_update' );
$change_email->init();

$this->assertFalse( $change_email->update_email( $user->ID, $old_user ) );
$this->assertFalse( wp_auth0_profile_change_email( $user->ID, $old_user ) );
$this->assertEquals( $old_user->data->user_email, get_user_by( 'id', $user->ID )->data->user_email );
$this->assertEmpty( get_user_meta( $user->ID, '_new_email', true ) );
$this->assertEmpty( WP_Auth0_UsersRepo::get_meta( $user->ID, 'auth0_transient_email_update' ) );
Expand All @@ -156,20 +144,16 @@ public function testThatFailedApiRedirectsOnUserEditPage() {
// Store userinfo for a DB strategy user.
$this->storeAuth0Data( $user->ID, 'auth0' );

// API call mocked to fail.
$change_email = $this->getStub( false );

// Need to remove existing filters and re-init with filters from the test class.
remove_all_filters( 'profile_update' );
$change_email->init();

// Set current page to the user profile.
global $pagenow;
$pagenow = 'user-edit.php';

$caught_redirect = [];
try {
$change_email->update_email( $user->ID, $old_user );
wp_auth0_profile_change_email( $user->ID, $old_user );
} catch ( Exception $e ) {
$caught_redirect = unserialize( $e->getMessage() );
}
Expand All @@ -193,32 +177,12 @@ public function testThatEmailUpdateFlagIsSetBeforeApiCall() {
$old_user->data->user_email = 'OLD-' . $new_email;
$this->storeAuth0Data( $user->ID, 'auth0' );

$api_change_email = new WP_Auth0_Api_Change_Email( self::$opts, self::$api_client_creds );
$change_email = new WP_Auth0_Profile_Change_Email( $api_change_email );

try {
$change_email->update_email( $user->ID, $old_user );
wp_auth0_profile_change_email( $user->ID, $old_user );
} catch ( Exception $e ) {
// Just need to stop the API call.
}

$this->assertEquals( $new_email, WP_Auth0_UsersRepo::get_meta( $user->ID, 'auth0_transient_email_update' ) );
}

/**
* Get an API stub set to pass or fail.
*
* @param boolean $success - True for the API call to succeed, false for it to fail.
*
* @return WP_Auth0_Profile_Change_Email
*/
public function getStub( $success ) {
$mock_api_test_email = $this
->getMockBuilder( WP_Auth0_Api_Change_Email::class )
->setMethods( [ 'call' ] )
->setConstructorArgs( [ self::$opts, self::$api_client_creds ] )
->getMock();
$mock_api_test_email->method( 'call' )->willReturn( $success );
return new WP_Auth0_Profile_Change_Email( $mock_api_test_email );
}
}
Loading