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 information on how to get a user in a provider #1751

Draft
wants to merge 1 commit into
base: 3.1
Choose a base branch
from

Conversation

RobQuistNL
Copy link

As this is the most basic usecases for using a provider ever (showing entities that only relate to the current user), adding it to the docs would have saved me a lot of time.

There's way more docs to be updated - nothing seems to even remotely indicate some kind of ACL / User related data filtering.

Isn't it one of the most basic usecases ever? Where you want someone to only see their own data?

This usecase doesn't seem to be documented anywhere. It doesn't seem doable by using the annotations (albeit there's security and IRI overrides everywhere) it would never just shows this users' own stuff.

As this is the most basic usecases for using a provider ever (showing entities that only relate to the current user), adding it to the docs would have saved me a lot of time.
@@ -125,6 +125,40 @@ use App\State\BlogPostProvider;
class BlogPost {}
```

## Getting the user in a provider
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## Getting the user in a provider
## Getting The Currently Connected User in a Provider

use App\Repository\BlogPostRepository;
use Symfony\Bundle\SecurityBundle\Security;

class BlogPostProvider implements ProviderInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class BlogPostProvider implements ProviderInterface
final class BlogPostProvider implements ProviderInterface

return $this->blogPostRepository->findAllForUser($this->security->getToken()->getUser());
}

return $this->blogPostRepository->find($uriVariables['id']);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe should this be removed as this doesn't look related?

Also, you should set the Vary HTTP header (on the ApiResource attribute) to prevent any cache pollution issue.

@dunglas
Copy link
Member

dunglas commented May 3, 2023

This is only useful if you don't use Doctrine (or at least the built-in Doctrine provider), otherwise you should use an extension instead: https://api-platform.com/docs/core/extensions/

Could you make it clear in this page? Thanks for improving our docs!

@RobQuistNL
Copy link
Author

RobQuistNL commented May 3, 2023

🤯 it's called extensions...... (I am using Doctrine)

Okay, I had no idea. I found "filters" and "subresources" and "state providers" but I did not look at "extensions" for this type of filtering / ownership logic... (extensions, in my mind, are "extending" API-Platform. Not "Extending the automatically generated queries in ORM's" / "Query extensions")

Thanks for the quick review, I'll run through this stuff again and edit the docs.

@RobQuistNL RobQuistNL marked this pull request as draft May 3, 2023 23:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants