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

wp-graphql doesn't respect user capabilities for viewing private posts #2859

Open
2 of 3 tasks
brnteka opened this issue Jul 18, 2023 · 4 comments
Open
2 of 3 tasks
Labels
Component: Connections Issues related to connections Status: 🚀 Actionable Issues that have been curated, have enough info to take action, and are ready to be worked on Type: Bug Something isn't working

Comments

@brnteka
Copy link

brnteka commented Jul 18, 2023

Description

Hello, wp-graphql seems to not respect user capabilities for viewing private posts. We have a user with capabilities: "read_private_posts" and "read_private_pages", which are added to the user at the time of its creation, if it passes some conditions

    $user = new WP_User($user_id);
    $user->add_cap( 'read_private_posts' );
    $user->add_cap( 'read_private_pages' );

and when i try to make an authorized request as this user with a query like this

query GetPostsEdges {
  posts(where: { stati: [PUBLISH, PRIVATE]}) {
    edges {
      node {
        id
        title
        date
      }
    }
  }
}

it returns published posts but no private posts, and WPs own front-end shows them in the main loop query, without any additional modifications to the query, which indicates that the capabilities work as expected. Is this a bug, or i missing something?

Steps to reproduce

This is how we add the capabilities

add_action('profile_update', 'set_capabilities', 10, 3);

function set_capabilities($user_id, $oldUserData, $newUserData)
{
    $categories = get_field('cats', 'user_' . $user_id);

    if ($categories) {
        $user = new WP_User($user_id);

        $user->add_cap( 'read_private_posts' );
        $user->add_cap( 'read_private_pages' );

        foreach ($categories as $category) {

            $user->add_cap('read_private_custom_' . $category->slug);
        }
    }
}

Additional context

No response

WPGraphQL Version

1.14.7

WordPress Version

6.2.2

PHP Version

7.4.26

Additional enviornment details

WPGraphQL JWT Authentication 0.7.0 - plugin is active

Please confirm that you have searched existing issues in the repo.

  • Yes

Please confirm that you have disabled ALL plugins except for WPGraphQL.

  • Yes
  • My issue is with compatibility with a specific WordPress plugin, and I have listed all my installed plugins (and version info) above.
@jasonbahl
Copy link
Collaborator

possibly related: #2819

@jasonbahl
Copy link
Collaborator

ok, so fwiw, I was able to create a "private" post .

When I query for a list of posts as an authenticated user, I can see it:

CleanShot 2023-07-18 at 15 24 40

When I query as a non-authenticated user, I cannot see it.

CleanShot 2023-07-18 at 15 24 50

On initial exploration, this seems to be working as expected.

I'll keep looking into it to see if I can reproduce with some other conditions.

@brnteka
Copy link
Author

brnteka commented Jul 18, 2023

I test my queries with curl (since the user from whom I make requests does not have access to the admin dashboard tools) and when i query private post directly it actually returns it.

This works:

curl -g -X POST -H "Content-Type: application/json" -H "Authorization: Bearer <TOKEN>" -d '{"query":"query GetPostsEdges { post( id: 5, idType: DATABASE_ID ) { id databaseId title } }"}' http://<WEBSITE>/graphql

And this doesn't return the private post, when i try to fetch all posts at once:

curl -g -X POST -H "Content-Type: application/json" -H "Authorization: Bearer <TOKEN>" -d '{"query":"query GetPostsEdges { posts(where: { stati: [PUBLISH, PRIVATE]}) { edges { node { id title date } } } }"}' http://<WEBSITE>/graphql

@jasonbahl jasonbahl added the Type: Bug Something isn't working label Aug 2, 2023
@jasonbahl jasonbahl added the Status: 🚀 Actionable Issues that have been curated, have enough info to take action, and are ready to be worked on label Sep 13, 2023
@jasonbahl
Copy link
Collaborator

Ok, so it looks like the issue might be the path at which the posts are being resolved.

When querying a post directly it is able to be resolved. But when filtering a connection of posts we're not able to see the expected node in the results.

I believe the information in #2819 is relevant to this issue as I believe the stati/status filters are most likely the culprit of the bug here.

I'm not ready to close this as a duplicate of #2819 yet, but they should probably be investigated further at the same time.

@jasonbahl jasonbahl added the Component: Connections Issues related to connections label Sep 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Connections Issues related to connections Status: 🚀 Actionable Issues that have been curated, have enough info to take action, and are ready to be worked on Type: Bug Something isn't working
Projects
Status: 🎯Actionable
Development

No branches or pull requests

2 participants