Skip to content

Commit d5bbbfc

Browse files
apermoCopilotswissspidyCopilotiandunn
authored
Fix: Resolve admin user dynamically instead of hardcoding user ID 1 (#6270)
* fix(context): resolve admin user dynamically Replace hardcoded user ID 1 fallback in Admin context with smart user resolution. On multisite, queries get_super_admins() to find a valid super admin. On single site, queries for the first user with the administrator role. Emits a clear error if no suitable user is found. Fixes #6269 * fix(context): optimize query and add test cases Use get_users() with login__in for single DB query instead of looping get_user_by(). Add single-site error case test. Replace wp super-admin commands with direct option manipulation in tests. * fix(context): fix linting issues Use single quotes for strings per PHPCS rules. Fix gherkin use-and lint violations by replacing consecutive When steps with And. * fix(context): address review feedback Revert multisite super admin lookup from get_users() back to foreach + get_user_by('login') loop because get_users() only fetches users on the current site but a super admin might not be a member of any site. Add debug logging after resolving admin user ID. * fix(context): use instanceof for type safety * Fix autoloader priority: locally installed packages now fully override phar-bundled versions (#6218) Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Pascal Birchler <pascalb@google.com> * Add environment variable configuration support to wp-cli.yml (#6169) Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Pascal Birchler <pascalb@google.com> * Autocomplete `--url` (#5704) Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Pascal Birchler <pascalb@google.com> Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com> * Add WP-CLI handbook link to `wp help` output (#6273) * Initial plan * Add WP-CLI handbook URL reference to wp help output Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> * Update file(s) from wp-cli/.github * Update file(s) from wp-cli/.github * Fix SSH alias path not forwarded to remote when defined as a separate config key (#6274) * Initial plan * Fix SSH alias path not forwarded to remote when path is a separate config key Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> * Fix: forward active alias to runcommand subprocess (#6272) Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> * Fix vagrant SSH strict host key checking failure (#6275) * Initial plan * Fix vagrant SSH strict host key checking failure When ssh: vagrant is configured and vagrant ssh-config is parsed, add -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null to the SSH command to match what vagrant itself sets. This prevents failures when the vagrant VM has been recreated and has a different host key than what is stored in ~/.ssh/known_hosts. Adds a Behat test scenario to verify the fix. Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Apply suggestion from @swissspidy --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Co-authored-by: Pascal Birchler <pascalb@google.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Add optional `$newline` parameter to `WP_CLI::log()` and `WP_CLI::line()` (#6276) * Initial plan * Add optional $newline parameter to WP_CLI::log(), WP_CLI::line(), and logger info() methods Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> * Undo Base class change --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Co-authored-by: Pascal Birchler <pascalb@google.com> * Refactor: remove unused imports (#6277) * Harden some tests on macOS * Update tests --------- Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: Pascal Birchler <pascalb@google.com> Co-authored-by: Ian Dunn <ian@iandunn.name> Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com> Co-authored-by: swissspidy <swissspidy@users.noreply.github.com> Co-authored-by: Sören Wünsch <soerenwrede@gmail.com>
1 parent f0f7f3d commit d5bbbfc

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

features/context.feature

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,44 @@ Feature: Context handling via --context global flag
276276
User ID in admin_init: {EDITOR_ID}
277277
"""
278278

279+
Scenario: Admin context resolves a super admin on multisite when no user is specified
280+
Given a WP multisite install
281+
And I run `wp user create anotheradmin anotheradmin@example.com --role=administrator`
282+
And I run `wp eval 'update_site_option( "site_admins", array( "anotheradmin" ) );'`
283+
And I run `wp --context=admin eval 'echo "Current user: " . wp_get_current_user()->user_login;'`
284+
Then STDOUT should contain:
285+
"""
286+
Current user: anotheradmin
287+
"""
288+
289+
Scenario: Admin context resolves an administrator on single site when no user is specified
290+
Given a WP install
291+
When I run `wp --context=admin eval 'echo "User ID: " . get_current_user_id();'`
292+
Then STDOUT should be:
293+
"""
294+
User ID: 1
295+
"""
296+
297+
Scenario: Admin context emits error when no suitable admin user is found on multisite
298+
Given a WP multisite install
299+
And I run `wp eval 'update_site_option( "site_admins", array() );'`
300+
And I try `wp --context=admin eval ''`
301+
Then the return code should be 1
302+
And STDERR should contain:
303+
"""
304+
Error: No super admin user found. Specify one with --user=<login>.
305+
"""
306+
307+
Scenario: Admin context emits error when no administrator is found on single site
308+
Given a WP install
309+
When I run `wp user update 1 --role=subscriber`
310+
And I try `wp --context=admin eval ''`
311+
Then the return code should be 1
312+
And STDERR should contain:
313+
"""
314+
Error: No administrator user found. Specify one with --user=<login>.
315+
"""
316+
279317
Scenario: Admin context throws an error for a non-existent user
280318
Given a WP install
281319
And a wp-cli.yml file:

php/WP_CLI/Context/Admin.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use WP_CLI;
66
use WP_CLI\Context;
77
use WP_CLI\Fetchers\User;
8+
use WP_User;
89

910
/**
1011
* Context which simulates the administrator backend.
@@ -48,14 +49,15 @@ function () use ( $config ) {
4849
$user = $fetcher->get_check( $config['user'] );
4950
$admin_user_id = $user->ID;
5051
} else {
51-
// TODO: Add logic to find an administrator user.
52-
$admin_user_id = 1;
52+
$admin_user_id = $this->find_admin_user_id();
5353
}
5454

5555
/**
5656
* @var int<1, max> $admin_user_id
5757
*/
5858

59+
WP_CLI::debug( sprintf( 'Continuing as admin user %d', $admin_user_id ), Context::DEBUG_GROUP );
60+
5961
$this->log_in_as_admin_user( $admin_user_id );
6062
},
6163
defined( 'PHP_INT_MIN' ) ? PHP_INT_MIN : -2147483648, // phpcs:ignore PHPCompatibility.Constants.NewConstants.php_int_minFound
@@ -72,6 +74,45 @@ function () {
7274
);
7375
}
7476

77+
/**
78+
* Find a suitable admin user ID for the current environment.
79+
*
80+
* On multisite, resolves a super admin via get_super_admins().
81+
* On single site, finds a user with the administrator role.
82+
*
83+
* @return int<1, max> Admin user ID.
84+
*/
85+
private function find_admin_user_id() {
86+
if ( is_multisite() ) {
87+
$super_admins = get_super_admins();
88+
89+
foreach ( $super_admins as $super_admin_login ) {
90+
$user = get_user_by( 'login', $super_admin_login );
91+
if ( $user instanceof WP_User ) {
92+
/** @var int<1, max> */
93+
return $user->ID;
94+
}
95+
}
96+
97+
WP_CLI::error( 'No super admin user found. Specify one with --user=<login>.' );
98+
}
99+
100+
$admins = get_users(
101+
[
102+
'role' => 'administrator',
103+
'number' => 1,
104+
'orderby' => 'ID',
105+
'order' => 'ASC',
106+
]
107+
);
108+
109+
if ( ! empty( $admins ) ) {
110+
return $admins[0]->ID;
111+
}
112+
113+
WP_CLI::error( 'No administrator user found. Specify one with --user=<login>.' );
114+
}
115+
75116
/**
76117
* Get a fake admin page filename that reflects the current command.
77118
*

0 commit comments

Comments
 (0)