From c21ba54339d8f88d4e0c5b3a2967892502dae798 Mon Sep 17 00:00:00 2001 From: Bunty Date: Sun, 6 Apr 2025 03:26:10 +0530 Subject: [PATCH 1/6] Issue #98 - Added error message when we try to remove capability same as role --- src/User_Command.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/User_Command.php b/src/User_Command.php index bfc8382a..c397d317 100644 --- a/src/User_Command.php +++ b/src/User_Command.php @@ -884,6 +884,10 @@ 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_role ) && in_array( $cap, $user_roles, true ) ) { + WP_CLI::error( "There is a role similar to '{$cap}' capability. Use `wp user remove-role` instead." ); + } $user->remove_cap( $cap ); WP_CLI::success( "Removed '{$cap}' cap for {$user->user_login} ({$user->ID})." ); From cde87dde66908de7d178c97c52fce5d459bc39ee Mon Sep 17 00:00:00 2001 From: Bunty Date: Thu, 10 Apr 2025 02:23:54 +0530 Subject: [PATCH 2/6] Add behat test for remove-cap improvement --- features/user.feature | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/features/user.feature b/features/user.feature index aed87855..dc1b79b3 100644 --- a/features/user.feature +++ b/features/user.feature @@ -379,6 +379,26 @@ Feature: Manage WordPress users administrator """ + 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 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-role` instead. + """ + Scenario: Managing user capabilities Given a WP install From 6c0b7f69c8eb31eaa8c14e4a663695921cb37d10 Mon Sep 17 00:00:00 2001 From: Bunty Date: Thu, 10 Apr 2025 02:56:12 +0530 Subject: [PATCH 3/6] Improve behat test --- features/user.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/user.feature b/features/user.feature index dc1b79b3..9894ffb2 100644 --- a/features/user.feature +++ b/features/user.feature @@ -392,7 +392,7 @@ Feature: Manage WordPress users contributor """ - When I run `wp user remove-cap {USER_ID}` contributor + When I run `wp user remove-cap {USER_ID} contributor` Then the return code should be 1 And STDERR should be: """ From 84f3aa4fc649de2ae8d1237f687ca75af53ecc49 Mon Sep 17 00:00:00 2001 From: Bunty Date: Thu, 10 Apr 2025 04:20:01 +0530 Subject: [PATCH 4/6] Fix behat test failing --- features/user.feature | 47 +++++++++++++++++++++++++------------------ src/User_Command.php | 3 ++- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/features/user.feature b/features/user.feature index 9894ffb2..6f817903 100644 --- a/features/user.feature +++ b/features/user.feature @@ -379,26 +379,6 @@ Feature: Manage WordPress users administrator """ - 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 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-role` instead. - """ - Scenario: Managing user capabilities Given a WP install @@ -452,6 +432,33 @@ 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-role` instead. + """ + And STDOUT should be empty + Scenario: Show password when creating a user Given a WP install diff --git a/src/User_Command.php b/src/User_Command.php index c397d317..6b2d7171 100644 --- a/src/User_Command.php +++ b/src/User_Command.php @@ -884,8 +884,9 @@ 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_role ) && in_array( $cap, $user_roles, true ) ) { + if ( ! empty( $user_roles ) && in_array( $cap, $user_roles, true ) ) { WP_CLI::error( "There is a role similar to '{$cap}' capability. Use `wp user remove-role` instead." ); } $user->remove_cap( $cap ); From 6ecf307372362236b617c6e68092f3bdfda0f482 Mon Sep 17 00:00:00 2001 From: Bunty Date: Thu, 1 May 2025 11:12:10 +0530 Subject: [PATCH 5/6] Provided flag to remove role forcefully --- features/user.feature | 8 +++++++- src/User_Command.php | 10 ++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/features/user.feature b/features/user.feature index 6f817903..ed81a84a 100644 --- a/features/user.feature +++ b/features/user.feature @@ -455,10 +455,16 @@ Feature: Manage WordPress users Then the return code should be 1 And STDERR should be: """ - Error: There is a role similar to 'contributor' capability. Use `wp user remove-role` instead. + 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 diff --git a/src/User_Command.php b/src/User_Command.php index 6b2d7171..adc07559 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 ) { @@ -886,8 +892,8 @@ public function remove_cap( $args, $assoc_args ) { } $user_roles = $user->roles; - if ( ! empty( $user_roles ) && in_array( $cap, $user_roles, true ) ) { - WP_CLI::error( "There is a role similar to '{$cap}' capability. Use `wp user remove-role` instead." ); + 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 ); From 19aa20824282cc8ddb7ea6eb4acec57d377257ce Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Tue, 6 May 2025 16:40:07 +0200 Subject: [PATCH 6/6] Adapt error message --- features/user.feature | 2 +- src/User_Command.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/features/user.feature b/features/user.feature index ed81a84a..b310d981 100644 --- a/features/user.feature +++ b/features/user.feature @@ -455,7 +455,7 @@ Feature: Manage WordPress users 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. + 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 diff --git a/src/User_Command.php b/src/User_Command.php index adc07559..5bad384c 100644 --- a/src/User_Command.php +++ b/src/User_Command.php @@ -893,7 +893,7 @@ public function remove_cap( $args, $assoc_args ) { $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." ); + 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 );