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

Add origin and exclude-role-names filters to list-caps command #445

Merged
merged 1 commit into from Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions features/user.feature
Expand Up @@ -443,6 +443,34 @@ Feature: Manage WordPress users
6
"""

When I run `wp user list-caps bob --exclude-role-names`
Then STDOUT should be:
"""
edit_posts
read
level_1
level_0
delete_posts
"""

When I run `wp user add-cap bob newcap`
And I run `wp user list-caps bob --origin=role`
Then STDOUT should be:
"""
edit_posts
read
level_1
level_0
delete_posts
contributor
"""

And I run `wp user list-caps bob --origin=user`
Then STDOUT should be:
"""
newcap
"""

Scenario: Make sure WordPress receives the slashed data it expects
Given a WP install

Expand Down
89 changes: 67 additions & 22 deletions src/User_Command.php
Expand Up @@ -870,6 +870,19 @@ public function remove_cap( $args, $assoc_args ) {
* - yaml
* ---
*
* [--origin=<origin>]
* : Render output in a particular format.
* ---
* default: all
* options:
* - all
* - user
* - role
* ---
*
* [--exclude-role-names]
* : Exclude capabilities that match role names from output.
*
* ## EXAMPLES
*
* $ wp user list-caps 21
Expand All @@ -879,37 +892,69 @@ public function remove_cap( $args, $assoc_args ) {
* @subcommand list-caps
*/
public function list_caps( $args, $assoc_args ) {
$user = $this->fetcher->get_check( $args[0] );
$user = $this->fetcher->get_check( $args[0] );
$exclude_role_names = Utils\get_flag_value( $assoc_args, 'exclude-role-names' );

if ( $user ) {
$user->get_role_caps();
$active_user_cap_list = [];

$user_caps_list = $user->allcaps;
$user_roles = $user->roles;

$active_user_cap_list = [];
switch ( $assoc_args['origin'] ) {
case 'all':
$user->get_role_caps();
$user_caps_list = $user->allcaps;

foreach ( $user_caps_list as $cap => $active ) {
if ( $active ) {
$active_user_cap_list[] = $cap;
foreach ( $user_caps_list as $capability => $active ) {
if ( $exclude_role_names && in_array( $capability, $user_roles, true ) ) {
continue;
}
if ( $active ) {
$active_user_cap_list[] = $capability;
}
}
}

if ( 'list' === $assoc_args['format'] ) {
foreach ( $active_user_cap_list as $cap ) {
WP_CLI::line( $cap );
break;
case 'user':
// Get the user's capabilities
$user_capabilities = get_user_meta( $user->ID, 'wp_capabilities', true );

// Loop through each capability and only return the non-inherited ones
foreach ( $user_capabilities as $capability => $active ) {
if ( true === $active && ! in_array( $capability, $user_roles, true ) ) {
$active_user_cap_list[] = $capability;
}
}
} else {
$output_caps = [];
foreach ( $active_user_cap_list as $cap ) {
$output_cap = new stdClass();
break;
case 'role':
// Get the user's capabilities
$user_capabilities = get_user_meta( $user->ID, 'wp_capabilities', true );

// Loop through each capability and only return the inherited ones (including the role name)
foreach ( $user->get_role_caps() as $capability => $active ) {
if ( true === $active && ! isset( $user_capabilities[ $capability ] ) ) {
$active_user_cap_list[] = $capability;
}
if ( true === $active && ! $exclude_role_names && in_array( $capability, $user_roles, true ) ) {
$active_user_cap_list[] = $capability;
}
}
break;
}

if ( 'list' === $assoc_args['format'] ) {
foreach ( $active_user_cap_list as $cap ) {
WP_CLI::line( $cap );
}
} else {
$output_caps = [];
foreach ( $active_user_cap_list as $cap ) {
$output_cap = new stdClass();

$output_cap->name = $cap;
$output_cap->name = $cap;

$output_caps[] = $output_cap;
}
$formatter = new Formatter( $assoc_args, $this->cap_fields );
$formatter->display_items( $output_caps );
$output_caps[] = $output_cap;
}
$formatter = new Formatter( $assoc_args, $this->cap_fields );
$formatter->display_items( $output_caps );
}
}

Expand Down