Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace AcmeBundle\Services;
- use Doctrine\ORM\EntityManager;
- use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
- use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
- use Symfony\Component\Security\Core\SecurityContext;
- class RoleParamConverter implements ParamConverterInterface
- {
- /**
- * @var SecurityContext
- */
- private $securityContext;
- /**
- * @var EntityManager
- */
- private $entityManager;
- function __construct(SecurityContext $securityContext, EntityManager $entityManager)
- {
- $this->securityContext = $securityContext;
- $this->entityManager = $entityManager;
- }
- /**
- * Stores the object in the request.
- *
- * @param Request $request The request
- * @param ParamConverter $configuration Contains the name, class and options of the object
- *
- * @return bool True if the object has been successfully set, else false
- */
- public function apply(Request $request, ParamConverter $configuration)
- {
- /** @var TokenInterface $token */
- $token = $this->securityContext->getToken();
- if ( $token == NULL ) {
- return NULL;
- }
- /** @var User $user */
- $user = $token->getUser();
- /** @var ProfileRepository|TeacherRepository $repo */
- $repo = $this->entityManager->getRepository($configuration->getClass());
- $object = $repo->findByUser($user);
- $request->attributes->set($configuration->getName(), $object);
- return TRUE;
- }
- /**
- * Checks if the object is supported.
- *
- * @param ParamConverter $configuration Should be an instance of ParamConverter
- *
- * @return bool True if the object is supported, else false
- */
- public function supports(ParamConverter $configuration)
- {
- return in_array($configuration->getClass(), array("AcmeBundle:MyClass1", "AcmeBundle:MyClass2"));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement