Advertisement
Guest User

RoleParamConverter

a guest
Jan 29th, 2015
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.37 KB | None | 0 0
  1. <?php
  2.     namespace AcmeBundle\Services;
  3.    
  4.     use Doctrine\ORM\EntityManager;
  5.     use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  6.     use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
  7.     use Symfony\Component\HttpFoundation\Request;
  8.     use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9.     use Symfony\Component\Security\Core\SecurityContext;
  10.    
  11.     class RoleParamConverter implements ParamConverterInterface
  12.     {
  13.         /**
  14.          * @var SecurityContext
  15.          */
  16.         private $securityContext;
  17.        
  18.         /**
  19.          * @var EntityManager
  20.          */
  21.         private $entityManager;
  22.        
  23.         function __construct(SecurityContext $securityContext, EntityManager $entityManager)
  24.         {
  25.             $this->securityContext = $securityContext;
  26.             $this->entityManager = $entityManager;
  27.         }
  28.         /**
  29.          * Stores the object in the request.
  30.          *
  31.          * @param Request        $request       The request
  32.          * @param ParamConverter $configuration Contains the name, class and options of the object
  33.          *
  34.          * @return bool    True if the object has been successfully set, else false
  35.          */
  36.         public function apply(Request $request, ParamConverter $configuration)
  37.         {
  38.             /** @var TokenInterface $token */
  39.             $token = $this->securityContext->getToken();
  40.             if ( $token == NULL ) {
  41.                 return NULL;
  42.             }
  43.             /** @var User $user */
  44.             $user = $token->getUser();
  45.            
  46.             /** @var ProfileRepository|TeacherRepository $repo */
  47.         $repo = $this->entityManager->getRepository($configuration->getClass());
  48.            
  49.             $object = $repo->findByUser($user);
  50.            
  51.             $request->attributes->set($configuration->getName(), $object);
  52.            
  53.             return TRUE;
  54.         }
  55.         /**
  56.          * Checks if the object is supported.
  57.          *
  58.          * @param ParamConverter $configuration Should be an instance of ParamConverter
  59.          *
  60.          * @return bool    True if the object is supported, else false
  61.          */
  62.         public function supports(ParamConverter $configuration)
  63.         {
  64.             return in_array($configuration->getClass(), array("AcmeBundle:MyClass1", "AcmeBundle:MyClass2"));
  65.         }
  66.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement