Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for adding and removing of multiple user roles #437

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions features/user.feature
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,14 @@ 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`
Expand All @@ -217,6 +225,22 @@ Feature: Manage WordPress users
administrator, editor
"""

When I run `wp user add-role 1 editor contributor`
Then STDOUT should not be empty
danielbachhuber marked this conversation as resolved.
Show resolved Hide resolved
And I run `wp user get 1 --field=roles`
danielbachhuber marked this conversation as resolved.
Show resolved Hide resolved
Then STDOUT should be:
"""
administrator, editor, contributor
"""

When I run `wp user remove-role 1 editor contributor`
Then STDOUT should not be empty
And 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
46 changes: 33 additions & 13 deletions src/User_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -692,26 +692,38 @@ 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' role for johndoe (12).
* Success: Added 'editor' role 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 );
danielbachhuber marked this conversation as resolved.
Show resolved Hide resolved

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 );
WP_CLI::success( "Added '{$role}' role for {$user->user_login} ({$user->ID})." );
danielbachhuber marked this conversation as resolved.
Show resolved Hide resolved
}
}

/**
Expand All @@ -722,27 +734,35 @@ 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' role for johndoe (12).
* Success: Removed 'editor' role 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];
$roles = $args;
danielbachhuber marked this conversation as resolved.
Show resolved Hide resolved
array_shift( $roles );

self::validate_role( $role );

$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 );
WP_CLI::success( "Removed '{$role}' role for {$user->user_login} ({$user->ID})." );
danielbachhuber marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
// Multisite
if ( function_exists( 'remove_user_from_blog' ) ) {
Expand Down
Loading