You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 line
3.5KB

  1. <?php
  2. namespace Lc\CaracoleBundle\EventSubscriber\User;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\CaracoleBundle\Doctrine\Extension\FilterMerchantInterface;
  5. use Lc\CaracoleBundle\Doctrine\Extension\FilterMultipleMerchantsInterface;
  6. use Lc\CaracoleBundle\Doctrine\Extension\FilterSectionInterface;
  7. use Lc\CaracoleBundle\Model\User\UserMerchantInterface;
  8. use Lc\CaracoleBundle\Resolver\MerchantResolver;
  9. use Lc\CaracoleBundle\Resolver\SectionResolver;
  10. use Lc\SovBundle\Event\EntityManager\EntityManagerEvent;
  11. use Lc\SovBundle\Model\User\UserInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  14. use Symfony\Component\HttpKernel\Event\KernelEvent;
  15. use Symfony\Component\HttpKernel\Event\RequestEvent;
  16. use Symfony\Component\HttpKernel\KernelEvents;
  17. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  18. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  19. use Symfony\Component\Security\Core\Security;
  20. use Symfony\Component\Security\Guard\Token\PostAuthenticationGuardToken;
  21. class UserRolesEventSubscriber implements EventSubscriberInterface
  22. {
  23. protected $em;
  24. protected $tokenStorage;
  25. protected $security;
  26. protected $merchantResolver;
  27. protected $sectionResolver;
  28. public function __construct(
  29. TokenStorageInterface $tokenStorage,
  30. EntityManagerInterface $entityManager,
  31. Security $security,
  32. MerchantResolver $merchantResolver,
  33. SectionResolver $sectionResolver
  34. ) {
  35. $this->em = $entityManager;
  36. $this->tokenStorage = $tokenStorage;
  37. $this->security = $tokenStorage;
  38. $this->merchantResolver = $merchantResolver;
  39. $this->sectionResolver = $sectionResolver;
  40. }
  41. public static function getSubscribedEvents()
  42. {
  43. return [
  44. KernelEvents::REQUEST => ['setUserRoles'],
  45. ];
  46. }
  47. public function setUserRoles(RequestEvent $event)
  48. {
  49. if (!$event->isMasterRequest()) {
  50. return;
  51. }
  52. if ($this->tokenStorage && $this->tokenStorage->getToken()) {
  53. $token = $this->tokenStorage->getToken();
  54. $sessionUser = $token->getUser();
  55. if($sessionUser instanceof UserInterface) {
  56. $roles = $this->merchantResolver->getUserMerchant($sessionUser)->getRoles();
  57. if($roles != $sessionUser->getRoles()) {
  58. $sessionUser->setRoles($roles);
  59. $this->em->update($sessionUser);
  60. $this->em->flush();
  61. $token = new \Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken(
  62. $sessionUser,
  63. null,
  64. 'main',
  65. $sessionUser->getRoles()
  66. );
  67. $this->security->setToken($token);
  68. }
  69. }
  70. /*
  71. // This check can be just `is_object` like in symfony core
  72. // we're explicit about the class used
  73. if ($sessionUser instanceof UserInterface) {
  74. /* if ($this->merchantResolver->getCurrent()->getId() == 2) {
  75. $sessionUser->setRoles(array('ROLE_ADMIN', 'ROLE_BEST_USER'));
  76. }
  77. $this->tokenStorage->setToken(
  78. new PostAuthenticationGuardToken($sessionUser, 'main', $sessionUser->getRoles())
  79. );
  80. }*/
  81. }
  82. }
  83. }