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
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions core/state-providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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


Receiving a user in your provider to allow for filtering of entities can be achieved by adding the `Security` class in your constructor;

```php
<?php

namespace App\State;

use ApiPlatform\Metadata\CollectionOperationInterface;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
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

{
public function __construct(
private readonly BlogPostRepository $blogPostRepository,
private readonly Security $security
) {
}

public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
if ($operation instanceof CollectionOperationInterface) {
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.

}
}
```

## 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).
Expand Down