Skip to content

Commit 213611f

Browse files
authored
Merge pull request #530 from BhargavBhandari90/issue-98-remove-cap-improvement
2 parents d7b61a2 + 19aa208 commit 213611f

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

features/user.feature

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,39 @@ Feature: Manage WordPress users
432432
publish_posts
433433
"""
434434
435+
Scenario: Show error when trying to remove capability same as role
436+
Given a WP install
437+
438+
When I run `wp user create testuser2 [email protected] --first_name=test --last_name=user --role=contributor --porcelain`
439+
Then STDOUT should be a number
440+
And save STDOUT as {USER_ID}
441+
442+
When I run `wp user list-caps {USER_ID}`
443+
Then STDOUT should contain:
444+
"""
445+
contributor
446+
"""
447+
448+
When I run `wp user get {USER_ID} --field=roles`
449+
Then STDOUT should contain:
450+
"""
451+
contributor
452+
"""
453+
454+
When I try `wp user remove-cap {USER_ID} contributor`
455+
Then the return code should be 1
456+
And STDERR should be:
457+
"""
458+
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.
459+
"""
460+
And STDOUT should be empty
461+
462+
When I run `wp user remove-cap {USER_ID} contributor --force`
463+
Then STDOUT should be:
464+
"""
465+
Success: Removed 'contributor' cap for testuser2 ({USER_ID}).
466+
"""
467+
435468
Scenario: Show password when creating a user
436469
Given a WP install
437470

src/User_Command.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,9 @@ public function add_cap( $args, $assoc_args ) {
861861
* <cap>
862862
* : The capability to be removed.
863863
*
864+
* [--force]
865+
* : Forcefully remove a capability.
866+
*
864867
* ## EXAMPLES
865868
*
866869
* $ wp user remove-cap 11 publish_newsletters
@@ -872,6 +875,9 @@ public function add_cap( $args, $assoc_args ) {
872875
* $ wp user remove-cap 11 nonexistent_cap
873876
* Error: No such 'nonexistent_cap' cap for supervisor (11).
874877
*
878+
* $ wp user remove-cap 11 publish_newsletters --force
879+
* Success: Removed 'publish_newsletters' cap for supervisor (11).
880+
*
875881
* @subcommand remove-cap
876882
*/
877883
public function remove_cap( $args, $assoc_args ) {
@@ -884,6 +890,11 @@ public function remove_cap( $args, $assoc_args ) {
884890
}
885891
WP_CLI::error( "No such '{$cap}' cap for {$user->user_login} ({$user->ID})." );
886892
}
893+
894+
$user_roles = $user->roles;
895+
if ( ! empty( $user_roles ) && in_array( $cap, $user_roles, true ) && ! Utils\get_flag_value( $assoc_args, 'force' ) ) {
896+
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." );
897+
}
887898
$user->remove_cap( $cap );
888899

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

0 commit comments

Comments
 (0)