Skip to content

Commit

Permalink
Support for adding and removing of multiple user roles (#437)
Browse files Browse the repository at this point in the history
* 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 <daniel.bachhuber@automattic.com>
  • Loading branch information
shawnhooper and danielbachhuber authored Nov 10, 2023
1 parent 0cd4840 commit b5846bf
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 21 deletions.
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4637,22 +4637,25 @@ wp user add-cap <user> <cap>
Adds a role for a user.

~~~
wp user add-role <user> <role>
wp user add-role <user> [<role>...]
~~~

**OPTIONS**

<user>
User ID, user email, or user login.

<role>
Add the specified role to the user.
[<role>...]
Add the specified role(s) to the user.

**EXAMPLES**

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

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



### wp user create
Expand Down Expand Up @@ -5357,22 +5360,25 @@ wp user remove-cap <user> <cap>
Removes a user's role.

~~~
wp user remove-role <user> [<role>]
wp user remove-role <user> [<role>...]
~~~

**OPTIONS**

<user>
User ID, user email, or user login.

[<role>]
A specific role to remove.
[<role>...]
Remove the specified role(s) from the user.

**EXAMPLES**

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

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



### wp user reset-password
Expand Down
40 changes: 38 additions & 2 deletions features/user.feature
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,50 @@ Feature: Manage WordPress users
Scenario: Managing user roles
Given a WP install

When I try `wp user add-role 1`
Then the return code should be 1
And STDERR should be:
"""
Error: Please specify at least one role to add.
"""
And STDOUT should be empty

When I run `wp user add-role 1 editor`
Then STDOUT should not be empty
And I run `wp user get 1 --field=roles`
Then STDOUT should be:
"""
Success: Added 'editor' role for admin (1).
"""

When I run `wp user get 1 --field=roles`
Then STDOUT should be:
"""
administrator, editor
"""

When I run `wp user add-role 1 editor contributor`
Then STDOUT should be:
"""
Success: Added 'editor', 'contributor' roles for admin (1).
"""

When I run `wp user get 1 --field=roles`
Then STDOUT should be:
"""
administrator, editor, contributor
"""

When I run `wp user remove-role 1 editor contributor`
Then STDOUT should be:
"""
Success: Removed 'editor', 'contributor' roles from admin (1).
"""

When I run `wp user get 1 --field=roles`
Then STDOUT should be:
"""
administrator
"""

When I try `wp user add-role 1 edit`
Then STDERR should contain:
"""
Expand Down
48 changes: 35 additions & 13 deletions src/User_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -692,26 +692,39 @@ public function set_role( $args, $assoc_args ) {
* <user>
* : User ID, user email, or user login.
*
* <role>
* : Add the specified role to the user.
* [<role>...]
* : Add the specified role(s) to the user.
*
* ## EXAMPLES
*
* $ wp user add-role 12 author
* Success: Added 'author' role for johndoe (12).
*
* $ wp user add-role 12 author editor
* Success: Added 'author', 'editor' roles for johndoe (12).
*
* @subcommand add-role
*/
public function add_role( $args, $assoc_args ) {
$user = $this->fetcher->get_check( $args[0] );

$role = $args[1];
$roles = $args;
array_shift( $roles );

self::validate_role( $role );
if ( empty( $roles ) ) {
WP_CLI::error( 'Please specify at least one role to add.' );
}

$user->add_role( $role );
foreach ( $roles as $role ) {
self::validate_role( $role );
}

WP_CLI::success( "Added '{$role}' role for {$user->user_login} ({$user->ID})." );
foreach ( $roles as $role ) {
$user->add_role( $role );
}
$message = implode( "', '", $roles );
$label = count( $roles ) > 1 ? 'roles' : 'role';
WP_CLI::success( "Added '{$message}' {$label} for {$user->user_login} ({$user->ID})." );
}

/**
Expand All @@ -722,27 +735,36 @@ public function add_role( $args, $assoc_args ) {
* <user>
* : User ID, user email, or user login.
*
* [<role>]
* : A specific role to remove.
* [<role>...]
* : Remove the specified role(s) from the user.
*
* ## EXAMPLES
*
* $ wp user remove-role 12 author
* Success: Removed 'author' role for johndoe (12).
*
* $ wp user remove-role 12 author editor
* Success: Removed 'author', 'editor' roles for johndoe (12).
*
* @subcommand remove-role
*/
public function remove_role( $args, $assoc_args ) {
$user = $this->fetcher->get_check( $args[0] );

if ( isset( $args[1] ) ) {
$role = $args[1];

self::validate_role( $role );
$roles = $args;
array_shift( $roles );

$user->remove_role( $role );
foreach ( $roles as $role ) {
self::validate_role( $role );
}

WP_CLI::success( "Removed '{$role}' role for {$user->user_login} ({$user->ID})." );
foreach ( $roles as $role ) {
$user->remove_role( $role );
}
$message = implode( "', '", $roles );
$label = count( $roles ) > 1 ? 'roles' : 'role';
WP_CLI::success( "Removed '{$message}' {$label} from {$user->user_login} ({$user->ID})." );
} else {
// Multisite
if ( function_exists( 'remove_user_from_blog' ) ) {
Expand Down

0 comments on commit b5846bf

Please sign in to comment.