Skip to content

Improve wp remove-cap command #530

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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: 33 additions & 0 deletions features/user.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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: There is a role similar to 'contributor' capability. Use `wp user remove-cap {USER_ID} contributor --force` to remove capability.
"""
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

Expand Down
11 changes: 11 additions & 0 deletions src/User_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,9 @@ public function add_cap( $args, $assoc_args ) {
* <cap>
* : The capability to be removed.
*
* [--force]
* : Forcefully remove a capability.
*
* ## EXAMPLES
*
* $ wp user remove-cap 11 publish_newsletters
Expand All @@ -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 ) {
Expand All @@ -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( "There is a role similar to '{$cap}' capability. Use `wp user remove-cap {$user->ID} {$cap} --force` to remove capability." );
}
$user->remove_cap( $cap );

WP_CLI::success( "Removed '{$cap}' cap for {$user->user_login} ({$user->ID})." );
Expand Down