diff --git a/features/user.feature b/features/user.feature index aed87855..b310d981 100644 --- a/features/user.feature +++ b/features/user.feature @@ -432,6 +432,39 @@ Feature: Manage WordPress users publish_posts """ + Scenario: Show error when trying to remove capability same as role + Given a WP install + + When I run `wp user create testuser2 testuser2@example.com --first_name=test --last_name=user --role=contributor --porcelain` + Then STDOUT should be a number + And save STDOUT as {USER_ID} + + When I run `wp user list-caps {USER_ID}` + Then STDOUT should contain: + """ + contributor + """ + + When I run `wp user get {USER_ID} --field=roles` + Then STDOUT should contain: + """ + contributor + """ + + When I try `wp user remove-cap {USER_ID} contributor` + Then the return code should be 1 + And STDERR should be: + """ + Error: Aborting because a role has the same name as 'contributor'. Use `wp user remove-cap {USER_ID} contributor --force` to proceed with the removal. + """ + And STDOUT should be empty + + When I run `wp user remove-cap {USER_ID} contributor --force` + Then STDOUT should be: + """ + Success: Removed 'contributor' cap for testuser2 ({USER_ID}). + """ + Scenario: Show password when creating a user Given a WP install diff --git a/src/User_Command.php b/src/User_Command.php index bfc8382a..5bad384c 100644 --- a/src/User_Command.php +++ b/src/User_Command.php @@ -861,6 +861,9 @@ public function add_cap( $args, $assoc_args ) { * * : The capability to be removed. * + * [--force] + * : Forcefully remove a capability. + * * ## EXAMPLES * * $ wp user remove-cap 11 publish_newsletters @@ -872,6 +875,9 @@ public function add_cap( $args, $assoc_args ) { * $ wp user remove-cap 11 nonexistent_cap * Error: No such 'nonexistent_cap' cap for supervisor (11). * + * $ wp user remove-cap 11 publish_newsletters --force + * Success: Removed 'publish_newsletters' cap for supervisor (11). + * * @subcommand remove-cap */ public function remove_cap( $args, $assoc_args ) { @@ -884,6 +890,11 @@ public function remove_cap( $args, $assoc_args ) { } WP_CLI::error( "No such '{$cap}' cap for {$user->user_login} ({$user->ID})." ); } + + $user_roles = $user->roles; + if ( ! empty( $user_roles ) && in_array( $cap, $user_roles, true ) && ! Utils\get_flag_value( $assoc_args, 'force' ) ) { + WP_CLI::error( "Aborting because a role has the same name as '{$cap}'. Use `wp user remove-cap {$user->ID} {$cap} --force` to proceed with the removal." ); + } $user->remove_cap( $cap ); WP_CLI::success( "Removed '{$cap}' cap for {$user->user_login} ({$user->ID})." );