Skip to content

Commit b5846bf

Browse files
Support for adding and removing of multiple user roles (#437)
* Implement adding and removing of multiple user roles * Exit early with error if no roles are specified during user add-role * Fix test when user add-role fails due to no roles * Separate out the steps * Consolidate the success messages * Update README.md --------- Co-authored-by: Daniel Bachhuber <[email protected]>
1 parent 0cd4840 commit b5846bf

File tree

3 files changed

+85
-21
lines changed

3 files changed

+85
-21
lines changed

README.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4637,22 +4637,25 @@ wp user add-cap <user> <cap>
46374637
Adds a role for a user.
46384638

46394639
~~~
4640-
wp user add-role <user> <role>
4640+
wp user add-role <user> [<role>...]
46414641
~~~
46424642

46434643
**OPTIONS**
46444644

46454645
<user>
46464646
User ID, user email, or user login.
46474647

4648-
<role>
4649-
Add the specified role to the user.
4648+
[<role>...]
4649+
Add the specified role(s) to the user.
46504650

46514651
**EXAMPLES**
46524652

46534653
$ wp user add-role 12 author
46544654
Success: Added 'author' role for johndoe (12).
46554655

4656+
$ wp user add-role 12 author editor
4657+
Success: Added 'author', 'editor' roles for johndoe (12).
4658+
46564659

46574660

46584661
### wp user create
@@ -5357,22 +5360,25 @@ wp user remove-cap <user> <cap>
53575360
Removes a user's role.
53585361

53595362
~~~
5360-
wp user remove-role <user> [<role>]
5363+
wp user remove-role <user> [<role>...]
53615364
~~~
53625365

53635366
**OPTIONS**
53645367

53655368
<user>
53665369
User ID, user email, or user login.
53675370

5368-
[<role>]
5369-
A specific role to remove.
5371+
[<role>...]
5372+
Remove the specified role(s) from the user.
53705373

53715374
**EXAMPLES**
53725375

53735376
$ wp user remove-role 12 author
53745377
Success: Removed 'author' role for johndoe (12).
53755378

5379+
$ wp user remove-role 12 author editor
5380+
Success: Removed 'author', 'editor' roles for johndoe (12).
5381+
53765382

53775383

53785384
### wp user reset-password

features/user.feature

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,50 @@ Feature: Manage WordPress users
209209
Scenario: Managing user roles
210210
Given a WP install
211211

212+
When I try `wp user add-role 1`
213+
Then the return code should be 1
214+
And STDERR should be:
215+
"""
216+
Error: Please specify at least one role to add.
217+
"""
218+
And STDOUT should be empty
219+
212220
When I run `wp user add-role 1 editor`
213-
Then STDOUT should not be empty
214-
And I run `wp user get 1 --field=roles`
221+
Then STDOUT should be:
222+
"""
223+
Success: Added 'editor' role for admin (1).
224+
"""
225+
226+
When I run `wp user get 1 --field=roles`
215227
Then STDOUT should be:
216228
"""
217229
administrator, editor
218230
"""
219231

232+
When I run `wp user add-role 1 editor contributor`
233+
Then STDOUT should be:
234+
"""
235+
Success: Added 'editor', 'contributor' roles for admin (1).
236+
"""
237+
238+
When I run `wp user get 1 --field=roles`
239+
Then STDOUT should be:
240+
"""
241+
administrator, editor, contributor
242+
"""
243+
244+
When I run `wp user remove-role 1 editor contributor`
245+
Then STDOUT should be:
246+
"""
247+
Success: Removed 'editor', 'contributor' roles from admin (1).
248+
"""
249+
250+
When I run `wp user get 1 --field=roles`
251+
Then STDOUT should be:
252+
"""
253+
administrator
254+
"""
255+
220256
When I try `wp user add-role 1 edit`
221257
Then STDERR should contain:
222258
"""

src/User_Command.php

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -692,26 +692,39 @@ public function set_role( $args, $assoc_args ) {
692692
* <user>
693693
* : User ID, user email, or user login.
694694
*
695-
* <role>
696-
* : Add the specified role to the user.
695+
* [<role>...]
696+
* : Add the specified role(s) to the user.
697697
*
698698
* ## EXAMPLES
699699
*
700700
* $ wp user add-role 12 author
701701
* Success: Added 'author' role for johndoe (12).
702702
*
703+
* $ wp user add-role 12 author editor
704+
* Success: Added 'author', 'editor' roles for johndoe (12).
705+
*
703706
* @subcommand add-role
704707
*/
705708
public function add_role( $args, $assoc_args ) {
706709
$user = $this->fetcher->get_check( $args[0] );
707710

708-
$role = $args[1];
711+
$roles = $args;
712+
array_shift( $roles );
709713

710-
self::validate_role( $role );
714+
if ( empty( $roles ) ) {
715+
WP_CLI::error( 'Please specify at least one role to add.' );
716+
}
711717

712-
$user->add_role( $role );
718+
foreach ( $roles as $role ) {
719+
self::validate_role( $role );
720+
}
713721

714-
WP_CLI::success( "Added '{$role}' role for {$user->user_login} ({$user->ID})." );
722+
foreach ( $roles as $role ) {
723+
$user->add_role( $role );
724+
}
725+
$message = implode( "', '", $roles );
726+
$label = count( $roles ) > 1 ? 'roles' : 'role';
727+
WP_CLI::success( "Added '{$message}' {$label} for {$user->user_login} ({$user->ID})." );
715728
}
716729

717730
/**
@@ -722,27 +735,36 @@ public function add_role( $args, $assoc_args ) {
722735
* <user>
723736
* : User ID, user email, or user login.
724737
*
725-
* [<role>]
726-
* : A specific role to remove.
738+
* [<role>...]
739+
* : Remove the specified role(s) from the user.
727740
*
728741
* ## EXAMPLES
729742
*
730743
* $ wp user remove-role 12 author
731744
* Success: Removed 'author' role for johndoe (12).
732745
*
746+
* $ wp user remove-role 12 author editor
747+
* Success: Removed 'author', 'editor' roles for johndoe (12).
748+
*
733749
* @subcommand remove-role
734750
*/
735751
public function remove_role( $args, $assoc_args ) {
736752
$user = $this->fetcher->get_check( $args[0] );
737753

738754
if ( isset( $args[1] ) ) {
739-
$role = $args[1];
740-
741-
self::validate_role( $role );
755+
$roles = $args;
756+
array_shift( $roles );
742757

743-
$user->remove_role( $role );
758+
foreach ( $roles as $role ) {
759+
self::validate_role( $role );
760+
}
744761

745-
WP_CLI::success( "Removed '{$role}' role for {$user->user_login} ({$user->ID})." );
762+
foreach ( $roles as $role ) {
763+
$user->remove_role( $role );
764+
}
765+
$message = implode( "', '", $roles );
766+
$label = count( $roles ) > 1 ? 'roles' : 'role';
767+
WP_CLI::success( "Removed '{$message}' {$label} from {$user->user_login} ({$user->ID})." );
746768
} else {
747769
// Multisite
748770
if ( function_exists( 'remove_user_from_blog' ) ) {

0 commit comments

Comments
 (0)