From 60d92b5e0d16bf0fda8c48566564316720418319 Mon Sep 17 00:00:00 2001 From: Rob Quist Date: Wed, 3 May 2023 11:32:09 +0200 Subject: [PATCH] Add information on how to get a user in a provider 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. --- core/state-providers.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/core/state-providers.md b/core/state-providers.md index 77e4f0a841d..8a7708a3201 100644 --- a/core/state-providers.md +++ b/core/state-providers.md @@ -125,6 +125,40 @@ use App\State\BlogPostProvider; class BlogPost {} ``` +## Getting the user in a provider + +Receiving a user in your provider to allow for filtering of entities can be achieved by adding the `Security` class in your constructor; + +```php +blogPostRepository->findAllForUser($this->security->getToken()->getUser()); + } + + return $this->blogPostRepository->find($uriVariables['id']); + } +} +``` + ## Hooking into the Built-In State Provider If you want to execute custom business logic before or after retrieving data, this can be achieved by [decorating](https://symfony.com/doc/current/service_container/service_decoration.html) the built-in state providers or using [composition](https://en.wikipedia.org/wiki/Object_composition).