Skip to content

Instantly share code, notes, and snippets.

@soyuka
Last active August 10, 2023 21:49
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soyuka/c25aa66728439eec56419a5a20295592 to your computer and use it in GitHub Desktop.
Save soyuka/c25aa66728439eec56419a5a20295592 to your computer and use it in GitHub Desktop.
Me route ApiPlatform
<?php
namespace App\Identifier;
use App\Entity\User;
use Ramsey\Uuid\UuidInterface;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
final class UserIdentifierNormalizer implements DenormalizerInterface
{
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = null, array $context = [])
{
$user = $this->security->getUser();
if (null === $user || !$user instanceof User) {
throw new AccessDeniedHttpException();
}
// @TODO: return the object once https://github.com/api-platform/core/pull/3578 gets merged
/* @phpstan-ignore-next-line */
return (string) $user->getId();
}
/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null)
{
return 'me' === $data && UuidInterface::class === $type;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment