Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 0 additions & 14 deletions assets/js/googlesitekit/datastore/site/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,6 @@ const baseSelectors = {
return connection;
},

/**
* Gets owner ID.
*
* @since 1.16.0
*
* @param {Object} state Data store's state.
* @return {number|undefined} Owner ID if it exists, otherwise undefined.
*/
getOwnerID: createRegistrySelector( ( select ) => () => {
const { ownerID } = select( CORE_SITE ).getConnection() || {};

return ownerID;
} ),

/**
* Gets information about connected admins.
*
Expand Down
2 changes: 0 additions & 2 deletions assets/js/googlesitekit/datastore/site/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ describe( 'core/site connection', () => {
resettable: true,
setupCompleted: true,
hasConnectedAdmins: true,
ownerID: 123,
};

let registry;
Expand Down Expand Up @@ -168,7 +167,6 @@ describe( 'core/site connection', () => {
} );

describe.each( [
[ 'getOwnerID', 'ownerID' ],
[ 'hasConnectedAdmins', 'hasConnectedAdmins' ],
[ 'isConnected', 'connected' ],
[ 'isResettable', 'resettable' ],
Expand Down
25 changes: 2 additions & 23 deletions includes/Core/Authentication/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,6 @@ final class Authentication implements Provides_Feature_Metrics {
*/
protected $token;

/**
* Owner_ID instance.
*
* @since 1.16.0
* @var Owner_ID
*/
protected $owner_id;

/**
* Has_Connected_Admins instance.
*
Expand Down Expand Up @@ -257,7 +249,6 @@ public function __construct(
$this->verification_file = new Verification_File( $this->user_options );
$this->profile = new Profile( $this->user_options );
$this->token = new Token( $this->user_options );
$this->owner_id = new Owner_ID( $this->options );
$this->has_connected_admins = new Has_Connected_Admins( $this->options, $this->user_options );
$this->has_multiple_admins = new Has_Multiple_Admins( $this->transients );
$this->connected_proxy_url = new Connected_Proxy_URL( $this->options );
Expand All @@ -278,7 +269,6 @@ public function register() {
$this->verification_meta()->register();
$this->has_connected_admins->register();
$this->has_multiple_admins->register();
$this->owner_id->register();
$this->connected_proxy_url->register();
$this->disconnected_reason->register();
$this->initial_version->register();
Expand Down Expand Up @@ -733,7 +723,7 @@ public function do_refresh_user_token() {
* @since 1.32.0 Moved connect and disconnect actions to dedicated handlers.
*/
private function handle_oauth() {
if ( defined( 'WP_CLI' ) && WP_CLI ) {
if ( defined( 'WP_CLI' ) && constant( 'WP_CLI' ) ) {
return;
}

Expand Down Expand Up @@ -840,7 +830,7 @@ private function get_update_core_url() {
* @return array Filtered $data.
*/
private function inline_js_base_data( $data ) {
$data['isOwner'] = $this->owner_id->get() === get_current_user_id();
$data['isOwner'] = current_user_can( Permissions::MANAGE_OPTIONS );
$data['splashURL'] = esc_url_raw( $this->context->admin_url( 'splash' ) );
$data['proxySetupURL'] = '';
$data['proxyPermissionsURL'] = '';
Expand Down Expand Up @@ -1388,17 +1378,6 @@ public function get_has_multiple_admins_instance() {
return $this->has_multiple_admins;
}

/**
* Helper method to return owner_id property.
*
* @since 1.131.0
*
* @return Owner_ID
*/
public function get_owner_id_instance() {
return $this->owner_id;
}

/**
* Helper method to return disconnected_reason property.
*
Expand Down
42 changes: 0 additions & 42 deletions includes/Core/Authentication/Clients/OAuth_Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Google\Site_Kit\Core\Authentication\Credentials;
use Google\Site_Kit\Core\Authentication\Exception\Google_Proxy_Code_Exception;
use Google\Site_Kit\Core\Authentication\Google_Proxy;
use Google\Site_Kit\Core\Authentication\Owner_ID;
use Google\Site_Kit\Core\Authentication\Profile;
use Google\Site_Kit\Core\Authentication\Token;
use Google\Site_Kit\Core\Dismissals\Dismissed_Items;
Expand Down Expand Up @@ -43,14 +42,6 @@ final class OAuth_Client extends OAuth_Client_Base {
const OPTION_ERROR_REDIRECT_URL = 'googlesitekit_error_redirect_url';
const CRON_REFRESH_PROFILE_DATA = 'googlesitekit_cron_refresh_profile_data';

/**
* Owner_ID instance.
*
* @since 1.16.0
* @var Owner_ID
*/
private $owner_id;

/**
* Transients instance.
*
Expand Down Expand Up @@ -101,7 +92,6 @@ public function __construct(
$token
);

$this->owner_id = new Owner_ID( $this->options );
$this->transients = $transients ?: new Transients( $this->context );
$this->dismissed_items = new Dismissed_Items( $this->user_options );
}
Expand Down Expand Up @@ -510,13 +500,6 @@ function ( $scope ) {
*/
do_action( 'googlesitekit_authorize_user', $token_response, $scopes, $previous_scopes );

// This must happen after googlesitekit_authorize_user as the permissions checks depend on
// values set which affect the meta capability mapping.
$current_user_id = get_current_user_id();
if ( $this->should_update_owner_id( $current_user_id ) ) {
$this->owner_id->set( $current_user_id );
}

$redirect_url = $this->get_authorize_user_redirect_url();

// Store the redirect URL in transients using the authorization code hash as the key.
Expand Down Expand Up @@ -578,31 +561,6 @@ public function refresh_profile_data( $retry_after = 0 ) {
}
}

/**
* Determines whether the current owner ID must be changed or not.
*
* @since 1.16.0
*
* @param int $user_id Current user ID.
* @return bool TRUE if owner needs to be changed, otherwise FALSE.
*/
private function should_update_owner_id( $user_id ) {
$current_owner_id = $this->owner_id->get();
if ( $current_owner_id === $user_id ) {
return false;
}

if ( ! empty( $current_owner_id ) && user_can( $current_owner_id, Permissions::MANAGE_OPTIONS ) ) {
return false;
}

if ( ! user_can( $user_id, Permissions::MANAGE_OPTIONS ) ) {
return false;
}

return true;
}

/**
* Returns the permissions URL to the authentication proxy.
*
Expand Down
75 changes: 0 additions & 75 deletions includes/Core/Authentication/Owner_ID.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ private function get_rest_routes() {
'setupCompleted' => $this->authentication->is_setup_completed(),
'hasConnectedAdmins' => $this->authentication->get_has_connected_admins_instance()->get(),
'hasMultipleAdmins' => $this->authentication->get_has_multiple_admins_instance()->get(),
'ownerID' => $this->authentication->get_owner_id_instance()->get(),
);

return new WP_REST_Response( $data );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@

use Google\Site_Kit\Context;
use Google\Site_Kit\Core\Authentication\Clients\OAuth_Client;
use Google\Site_Kit\Core\Authentication\Owner_ID;
use Google\Site_Kit\Core\Authentication\Profile;
use Google\Site_Kit\Core\Dismissals\Dismissed_Items;
use Google\Site_Kit\Tests\Exception\RedirectException;
use Google\Site_Kit\Core\Permissions\Permissions;
use Google\Site_Kit\Core\Storage\Options;
use Google\Site_Kit\Core\Storage\Transients;
use Google\Site_Kit\Core\Storage\User_Options;
use Google\Site_Kit\Tests\Fake_Site_Connection_Trait;
Expand Down Expand Up @@ -663,51 +660,6 @@ function ( Request $request ) {
}
}

public function test_should_update_owner_id() {
$admin_id = $this->factory()->user->create( array( 'role' => 'administrator' ) );
$owner_id = $this->factory()->user->create( array( 'role' => 'administrator' ) );
wp_set_current_user( $admin_id );

$context = new Context( GOOGLESITEKIT_PLUGIN_MAIN_FILE );
$options = new Options( new Context( GOOGLESITEKIT_PLUGIN_MAIN_FILE ) );
$client = new OAuth_Client( $context, $options );

$class = new \ReflectionClass( OAuth_Client::class );
$method = $class->getMethod( 'should_update_owner_id' );
$method->setAccessible( true );

$should_update_owner_id = function ( ...$args ) use ( $method, $client ) {
return $method->invoke( $client, $args );
};

$map_meta_cap = function ( $user_id ) {
return function ( $caps, $cap, $uid ) use ( $user_id ) {
if ( Permissions::MANAGE_OPTIONS === $cap && $uid === $user_id ) {
return array( 'manage_options' );
}
return $caps;
};
};

// Should return FALSE when user is already an owner.
$options->set( Owner_ID::OPTION, $owner_id );
$this->assertFalse( $should_update_owner_id( $owner_id ) );

// Should return FALSE when the current owner is set and has MANAGE_OPTIONS permissions.
$map_owner_id_meta_cap = $map_meta_cap( $owner_id );
add_filter( 'map_meta_cap', $map_owner_id_meta_cap, 99, 3 );
$this->assertFalse( $should_update_owner_id( $admin_id ) );
remove_filter( 'map_meta_cap', $map_owner_id_meta_cap, 99, 3 );

// Should return FALSE when passed user has no MANAGE_OPTIONS permssions.
$this->assertFalse( $should_update_owner_id( $admin_id ) );

// Should return TRUE when the current owner has appropriate permissions and not equals to provided user who has appropriate permissions too.
$map_admin_id_meta_cap = $map_meta_cap( $admin_id );
add_filter( 'map_meta_cap', $map_admin_id_meta_cap, 99, 3 );
$this->assertFalse( $should_update_owner_id( $admin_id ) );
}

public function test_refresh_profile_data() {
$context = new Context( GOOGLESITEKIT_PLUGIN_MAIN_FILE );
$user_id_a = $this->factory()->user->create();
Expand Down
Loading
Loading