From 480e4cf4cd862233f8410c841449f25f247b6530 Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Fri, 10 Nov 2023 17:23:45 -0500 Subject: [PATCH 1/6] Implement adding and removing of multiple user roles --- features/user.feature | 16 ++++++++++++++++ src/User_Command.php | 44 +++++++++++++++++++++++++++++-------------- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/features/user.feature b/features/user.feature index 3537f0899..a29cf0c7a 100644 --- a/features/user.feature +++ b/features/user.feature @@ -217,6 +217,22 @@ Feature: Manage WordPress users administrator, editor """ + When I run `wp user add-role 1 editor contributor` + Then STDOUT should not be empty + And 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 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: """ diff --git a/src/User_Command.php b/src/User_Command.php index e0fbd867c..f6162faa2 100644 --- a/src/User_Command.php +++ b/src/User_Command.php @@ -692,26 +692,34 @@ public function set_role( $args, $assoc_args ) { * * : User ID, user email, or user login. * - * - * : Add the specified role to the user. + * [...] + * : 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]; - - self::validate_role( $role ); + $roles = $args; + array_shift( $roles ); - $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})." ); + } } /** @@ -722,27 +730,35 @@ public function add_role( $args, $assoc_args ) { * * : User ID, user email, or user login. * - * [] - * : A specific role to remove. + * [...] + * : 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; + 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})." ); + } } else { // Multisite if ( function_exists( 'remove_user_from_blog' ) ) { From 19b100519647352f2c1c5559008ba8fdcc7ba1ab Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Fri, 10 Nov 2023 17:51:27 -0500 Subject: [PATCH 2/6] Exit early with error if no roles are specified during user add-role --- features/user.feature | 8 ++++++++ src/User_Command.php | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/features/user.feature b/features/user.feature index a29cf0c7a..906fbaf04 100644 --- a/features/user.feature +++ b/features/user.feature @@ -209,6 +209,14 @@ Feature: Manage WordPress users Scenario: Managing user roles Given a WP install + When I run `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` diff --git a/src/User_Command.php b/src/User_Command.php index f6162faa2..a1ac635be 100644 --- a/src/User_Command.php +++ b/src/User_Command.php @@ -712,6 +712,10 @@ public function add_role( $args, $assoc_args ) { $roles = $args; array_shift( $roles ); + if ( empty( $roles ) ) { + WP_CLI::error( 'Please specify at least one role to add.' ); + } + foreach ( $roles as $role ) { self::validate_role( $role ); } From 63d1389b9b2d6b8e77cd80cf356cd9f441003e57 Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Fri, 10 Nov 2023 17:59:04 -0500 Subject: [PATCH 3/6] Fix test when user add-role fails due to no roles --- features/user.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/user.feature b/features/user.feature index 906fbaf04..acf7b28d4 100644 --- a/features/user.feature +++ b/features/user.feature @@ -209,7 +209,7 @@ Feature: Manage WordPress users Scenario: Managing user roles Given a WP install - When I run `wp user add-role 1` + When I try `wp user add-role 1` Then the return code should be 1 And STDERR should be: """ From 9356f956e71fa3305ad16823cfb92620c5dd46d0 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Fri, 10 Nov 2023 15:11:11 -0800 Subject: [PATCH 4/6] Separate out the steps --- features/user.feature | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/features/user.feature b/features/user.feature index acf7b28d4..7853e405e 100644 --- a/features/user.feature +++ b/features/user.feature @@ -219,7 +219,8 @@ Feature: Manage WordPress users When I run `wp user add-role 1 editor` Then STDOUT should not be empty - And I run `wp user get 1 --field=roles` + + When I run `wp user get 1 --field=roles` Then STDOUT should be: """ administrator, editor @@ -227,7 +228,8 @@ Feature: Manage WordPress users When I run `wp user add-role 1 editor contributor` Then STDOUT should not be empty - And I run `wp user get 1 --field=roles` + + When I run `wp user get 1 --field=roles` Then STDOUT should be: """ administrator, editor, contributor @@ -235,7 +237,8 @@ Feature: Manage WordPress users 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` + + When I run `wp user get 1 --field=roles` Then STDOUT should be: """ administrator From c5a7db495b436f1ec1519c903c724bf5e4d05ad0 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Fri, 10 Nov 2023 15:17:23 -0800 Subject: [PATCH 5/6] Consolidate the success messages --- features/user.feature | 15 ++++++++++++--- src/User_Command.php | 14 ++++++++------ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/features/user.feature b/features/user.feature index 7853e405e..53d380a4d 100644 --- a/features/user.feature +++ b/features/user.feature @@ -218,7 +218,10 @@ Feature: Manage WordPress users And STDOUT should be empty When I run `wp user add-role 1 editor` - Then STDOUT should not be empty + Then STDOUT should be: + """ + Success: Added 'editor' role for admin (1). + """ When I run `wp user get 1 --field=roles` Then STDOUT should be: @@ -227,7 +230,10 @@ Feature: Manage WordPress users """ When I run `wp user add-role 1 editor contributor` - Then STDOUT should not be empty + 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: @@ -236,7 +242,10 @@ Feature: Manage WordPress users """ When I run `wp user remove-role 1 editor contributor` - Then STDOUT should not be empty + 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: diff --git a/src/User_Command.php b/src/User_Command.php index a1ac635be..defd6b304 100644 --- a/src/User_Command.php +++ b/src/User_Command.php @@ -701,8 +701,7 @@ public function set_role( $args, $assoc_args ) { * 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). + * Success: Added 'author', 'editor' roles for johndoe (12). * * @subcommand add-role */ @@ -722,8 +721,10 @@ public function add_role( $args, $assoc_args ) { foreach ( $roles as $role ) { $user->add_role( $role ); - WP_CLI::success( "Added '{$role}' role for {$user->user_login} ({$user->ID})." ); } + $message = implode( "', '", $roles ); + $label = count( $roles ) > 1 ? 'roles' : 'role'; + WP_CLI::success( "Added '{$message}' {$label} for {$user->user_login} ({$user->ID})." ); } /** @@ -743,8 +744,7 @@ public function add_role( $args, $assoc_args ) { * 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). + * Success: Removed 'author', 'editor' roles for johndoe (12). * * @subcommand remove-role */ @@ -761,8 +761,10 @@ public function remove_role( $args, $assoc_args ) { foreach ( $roles as $role ) { $user->remove_role( $role ); - WP_CLI::success( "Removed '{$role}' role for {$user->user_login} ({$user->ID})." ); } + $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' ) ) { From 06f257216d42dde49054d39accd0f34e9f7418d9 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Fri, 10 Nov 2023 15:19:25 -0800 Subject: [PATCH 6/6] Update README.md --- README.md | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index d52b3b5c1..443b4e98c 100644 --- a/README.md +++ b/README.md @@ -4637,7 +4637,7 @@ wp user add-cap Adds a role for a user. ~~~ -wp user add-role +wp user add-role [...] ~~~ **OPTIONS** @@ -4645,14 +4645,17 @@ wp user add-role User ID, user email, or user login. - - Add the specified role to the user. + [...] + 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 @@ -5357,7 +5360,7 @@ wp user remove-cap Removes a user's role. ~~~ -wp user remove-role [] +wp user remove-role [...] ~~~ **OPTIONS** @@ -5365,14 +5368,17 @@ wp user remove-role [] User ID, user email, or user login. - [] - A specific role to remove. + [...] + 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