Skip to content

Commit f1c43e6

Browse files
authored
Merge pull request #125 from wp-cli/issue-97-remove-cap-error
Return error if cap doesn't exist or is role cap on remove cap.
2 parents 8000ffa + d2ff88f commit f1c43e6

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

features/user.feature

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,38 @@ Feature: Manage WordPress users
196196
Success: Removed 'edit_vip_product' cap for admin (1).
197197
"""
198198

199+
And I try the previous command again
200+
Then the return code should be 1
201+
And STDERR should be:
202+
"""
203+
Error: No such 'edit_vip_product' cap for admin (1).
204+
"""
205+
And STDOUT should be empty
206+
207+
When I run `wp user list-caps 1`
208+
Then STDOUT should not contain:
209+
"""
210+
edit_vip_product
211+
"""
212+
And STDOUT should contain:
213+
"""
214+
publish_posts
215+
"""
216+
217+
When I try `wp user remove-cap 1 publish_posts`
218+
Then the return code should be 1
219+
And STDERR should be:
220+
"""
221+
Error: The 'publish_posts' cap for admin (1) is inherited from a role.
222+
"""
223+
And STDOUT should be empty
224+
225+
And I run `wp user list-caps 1`
226+
Then STDOUT should contain:
227+
"""
228+
publish_posts
229+
"""
230+
199231
Scenario: Show password when creating a user
200232
Given a WP install
201233

src/User_Command.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,12 +747,24 @@ public function add_cap( $args, $assoc_args ) {
747747
* $ wp user remove-cap 11 publish_newsletters
748748
* Success: Removed 'publish_newsletters' cap for supervisor (11).
749749
*
750+
* $ wp user remove-cap 11 publish_posts
751+
* Error: The 'publish_posts' cap for supervisor (11) is inherited from a role.
752+
*
753+
* $ wp user remove-cap 11 nonexistent_cap
754+
* Error: No such 'nonexistent_cap' cap for supervisor (11).
755+
*
750756
* @subcommand remove-cap
751757
*/
752758
public function remove_cap( $args, $assoc_args ) {
753759
$user = $this->fetcher->get_check( $args[0] );
754760
if ( $user ) {
755761
$cap = $args[1];
762+
if ( ! isset( $user->caps[ $cap ] ) ) {
763+
if ( isset( $user->allcaps[ $cap ] ) ) {
764+
WP_CLI::error( sprintf( "The '%s' cap for %s (%d) is inherited from a role.", $cap, $user->user_login, $user->ID ) );
765+
}
766+
WP_CLI::error( sprintf( "No such '%s' cap for %s (%d).", $cap, $user->user_login, $user->ID ) );
767+
}
756768
$user->remove_cap( $cap );
757769

758770
WP_CLI::success( sprintf( "Removed '%s' cap for %s (%d).", $cap, $user->user_login, $user->ID ) );

0 commit comments

Comments
 (0)