Last active
August 10, 2023 21:49
-
-
Save soyuka/c25aa66728439eec56419a5a20295592 to your computer and use it in GitHub Desktop.
Me route ApiPlatform
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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