Created
May 10, 2015 18:02
-
-
Save AlmogBaku/37ad4dca8f3bcaecad16 to your computer and use it in GitHub Desktop.
Symfony ParamConverter for lookup user by `me`
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 | |
/** | |
* @author Almog Baku | |
* [email protected] | |
* http://www.GoDisco.net/ | |
* | |
* 10/05/15 18:43 | |
*/ | |
namespace Rimoto\SubscriberBundle\Request\ParamConverter; | |
use Doctrine\Common\Persistence\ManagerRegistry; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; | |
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\DoctrineParamConverter; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; | |
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | |
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException; | |
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; | |
class UserParamConverter extends DoctrineParamConverter | |
{ | |
/** @var TokenStorage */ | |
private $storage; | |
/** | |
* Constructor | |
* @param TokenStorage $storage | |
* @param ManagerRegistry $registry | |
*/ | |
public function __construct(ManagerRegistry $registry = null, TokenStorage $storage) | |
{ | |
parent::__construct($registry); | |
$this->storage = $storage; | |
} | |
/** | |
* {@inheritdoc} | |
* | |
* @throws \LogicException When unable to guess how to get a Doctrine instance from the request information | |
* @throws NotFoundHttpException When object not found | |
*/ | |
public function apply(Request $request, ParamConverter $configuration) | |
{ | |
$name = $configuration->getName(); | |
$options = $this->getOptions($configuration); | |
$id = $this->getIdentifier($request, $options, $name); | |
if(is_string($id) && strtolower($id) === "me") { | |
$token = $this->storage->getToken(); | |
if ($token === null) { | |
throw new BadRequestHttpException('Token not found.'); | |
} | |
$user = $token->getUser(); | |
if (!is_object($user)) { | |
throw new UnauthorizedHttpException("`me` is not valid for anonymous"); | |
} | |
$request->attributes->set($configuration->getName(), $user); | |
return true; | |
} | |
return parent::apply($request, $configuration); | |
} | |
/** | |
* @param ParamConverter $configuration | |
* @return bool | |
*/ | |
public function supports(ParamConverter $configuration) | |
{ | |
if (null === $this->registry || !count($this->registry->getManagers())) { | |
return false; | |
} | |
// Check, if option class was set in configuration | |
if (null === $configuration->getClass()) { | |
return false; | |
} | |
// Get actual entity manager for class | |
$em = $this->registry->getManagerForClass($configuration->getClass()); | |
// Check, if class name is what we need | |
$class = $em->getClassMetadata($configuration->getClass())->getName(); | |
$interfaces = class_implements($class); | |
return in_array('Symfony\Component\Security\Core\User\UserInterface', $interfaces); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment