Skip to content

Commit a0c38aa

Browse files
bindlegirlmatticbot
authored andcommitted
Connection: add modal for disconnecting owner account (#41923)
Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/13496170765 Upstream-Ref: Automattic/jetpack@a24c547
1 parent 9a2b369 commit a0c38aa

19 files changed

+179
-133
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
88

99
This is an alpha version! The changes listed here are not final.
1010

11+
### Added
12+
- Connection: Disconnecting a connection owner account will disconnect all other users first.
13+
1114
### Changed
1215
- Code: Use function-style exit() and die() with a default status code of 0.
1316
- Connection: Display connection status on Users page independent of the SSO module.

jetpack_vendor/automattic/jetpack-connection/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
This is an alpha version! The changes listed here are not final.
1111

12+
### Added
13+
- Connection: Disconnection connection ower will disconnect all other users first.
14+
1215
### Changed
1316
- moved endpoint for unlinking user to connection package
1417

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('react', 'wp-components', 'wp-compose', 'wp-data', 'wp-element', 'wp-i18n', 'wp-polyfill', 'wp-url'), 'version' => '3a37101ab5cb857d48cd');
1+
<?php return array('dependencies' => array('react', 'wp-components', 'wp-compose', 'wp-data', 'wp-element', 'wp-i18n', 'wp-polyfill', 'wp-url'), 'version' => 'd5c9b5c171dc14dcc54a');

jetpack_vendor/automattic/jetpack-connection/dist/identity-crisis.js

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('jetpack-script-data', 'react', 'react-jsx-runtime', 'wp-components', 'wp-compose', 'wp-data', 'wp-element', 'wp-i18n', 'wp-polyfill', 'wp-primitives', 'wp-url'), 'version' => 'e26cbfaac86c4072d9fc');
1+
<?php return array('dependencies' => array('jetpack-script-data', 'react', 'react-jsx-runtime', 'wp-components', 'wp-compose', 'wp-data', 'wp-element', 'wp-i18n', 'wp-polyfill', 'wp-primitives', 'wp-url'), 'version' => '51f69fbd939224c71267');

jetpack_vendor/automattic/jetpack-connection/dist/jetpack-connection.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jetpack_vendor/automattic/jetpack-connection/dist/jetpack-connection.js

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jetpack_vendor/automattic/jetpack-connection/dist/jetpack-connection.rtl.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jetpack_vendor/automattic/jetpack-connection/src/class-manager.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -944,19 +944,48 @@ public function connect_user( $user_id = null, $redirect_url = null ) {
944944
/**
945945
* Force user disconnect.
946946
*
947-
* @param int $user_id Local (external) user ID.
947+
* @param int $user_id Local (external) user ID.
948+
* @param bool $disconnect_all_users Whether to disconnect all users before disconnecting the primary user.
948949
*
949950
* @return bool
950951
*/
951-
public function disconnect_user_force( $user_id ) {
952+
public function disconnect_user_force( $user_id, $disconnect_all_users = false ) {
952953
if ( ! (int) $user_id ) {
953954
// Missing user ID.
954955
return false;
955956
}
957+
// If we are disconnecting the primary user we may need to disconnect all other users first
958+
if ( $user_id === $this->get_connection_owner_id() && $disconnect_all_users && ! $this->disconnect_all_users_except_primary() ) {
959+
return false;
960+
}
956961

957962
return $this->disconnect_user( $user_id, true, true );
958963
}
959964

965+
/**
966+
* Disconnects all users except the primary user.
967+
*
968+
* @return bool
969+
*/
970+
public function disconnect_all_users_except_primary() {
971+
972+
$all_connected_users = $this->get_connected_users();
973+
974+
foreach ( $all_connected_users as $user ) {
975+
// Skip the primary.
976+
if ( $user->ID === $this->get_connection_owner_id() ) {
977+
continue;
978+
}
979+
$disconnected = $this->disconnect_user( $user->ID, false, true );
980+
// If we fail to disconnect any user, we should not proceed with disconnecting the primary user.
981+
if ( ! $disconnected ) {
982+
return false;
983+
}
984+
}
985+
986+
return true;
987+
}
988+
960989
/**
961990
* Unlinks the current user from the linked WordPress.com user.
962991
*

jetpack_vendor/automattic/jetpack-connection/src/class-rest-connector.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1001,9 +1001,20 @@ public static function unlink_user( $request ) {
10011001
return new WP_Error( 'invalid_param', esc_html__( 'Invalid Parameter', 'jetpack-connection' ), array( 'status' => 404 ) );
10021002
}
10031003

1004+
// If the user is also connection owner, we need to disconnect all users. Since disconnecting all users is a destructive action, we need to pass a parameter to confirm the action.
1005+
$disconnect_all_users = false;
1006+
1007+
if ( ( new Manager() )->get_connection_owner_id() === get_current_user_id() ) {
1008+
if ( isset( $request['disconnect-all-users'] ) && false !== $request['disconnect-all-users'] ) {
1009+
$disconnect_all_users = true;
1010+
} else {
1011+
return new WP_Error( 'unlink_user_failed', esc_html__( 'Unable to unlink the connection owner.', 'jetpack-connection' ), array( 'status' => 400 ) );
1012+
}
1013+
}
1014+
10041015
// Allow admins to force a disconnect by passing the "force" parameter
10051016
// This allows an admin to disconnect themselves
1006-
if ( isset( $request['force'] ) && false !== $request['force'] && current_user_can( 'manage_options' ) && ( new Manager( 'jetpack' ) )->disconnect_user_force( get_current_user_id() ) ) {
1017+
if ( isset( $request['force'] ) && false !== $request['force'] && current_user_can( 'manage_options' ) && ( new Manager( 'jetpack' ) )->disconnect_user_force( get_current_user_id(), $disconnect_all_users ) ) {
10071018
return rest_ensure_response(
10081019
array(
10091020
'code' => 'success',

0 commit comments

Comments
 (0)