Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

101 lines
3.3KB

  1. <?php
  2. namespace Lc\CaracoleBundle\EventSubscriber\User;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\CaracoleBundle\Resolver\MerchantResolver;
  5. use Lc\SovBundle\Definition\RolesDefinition;
  6. use Lc\SovBundle\Model\User\UserInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpKernel\Event\RequestEvent;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. use Symfony\Component\Routing\RouterInterface;
  13. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  14. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  15. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  16. use Symfony\Component\Security\Http\SecurityEvents;
  17. class UserRolesEventSubscriber implements EventSubscriberInterface
  18. {
  19. protected $em;
  20. protected $tokenStorage;
  21. protected $router;
  22. protected $merchantResolver;
  23. public function __construct(
  24. TokenStorageInterface $tokenStorage,
  25. EntityManagerInterface $entityManager,
  26. RouterInterface $router,
  27. MerchantResolver $merchantResolver
  28. ) {
  29. $this->em = $entityManager;
  30. $this->tokenStorage = $tokenStorage;
  31. $this->router = $router;
  32. $this->merchantResolver = $merchantResolver;
  33. }
  34. public static function getSubscribedEvents()
  35. {
  36. return [
  37. KernelEvents::REQUEST => ['setUserRolesFromKernelRequest'],
  38. SecurityEvents::INTERACTIVE_LOGIN => ['setUserRolesAuthenticationSuccess'],
  39. ];
  40. }
  41. public function setUserRolesFromKernelRequest(RequestEvent $event)
  42. {
  43. if (!$event->isMainRequest()) {
  44. return;
  45. }
  46. if ($this->setUserRoles($event->getRequest())) {
  47. $response = new RedirectResponse($this->router->generate('app_admin_dashboard'));
  48. $event->setResponse($response);
  49. }
  50. }
  51. public function setUserRolesAuthenticationSuccess(InteractiveLoginEvent $interactiveLoginEvent)
  52. {
  53. $this->setUserRoles($interactiveLoginEvent->getRequest());
  54. }
  55. public function setUserRoles(Request $request): bool
  56. {
  57. if ($this->tokenStorage && $this->tokenStorage->getToken()) {
  58. $token = $this->tokenStorage->getToken();
  59. $sessionUser = $token->getUser();
  60. if ($sessionUser instanceof UserInterface) {
  61. $userMerchant = $this->merchantResolver->getUserMerchant($sessionUser);
  62. if ($userMerchant) {
  63. $roles = $userMerchant->getRoles();
  64. } else {
  65. $roles = [RolesDefinition::ROLE_USER];
  66. }
  67. if ($roles != $sessionUser->getRoles()) {
  68. $sessionUser->setRoles($roles);
  69. $this->em->update($sessionUser);
  70. $this->em->flush();
  71. $token = new UsernamePasswordToken(
  72. $sessionUser,
  73. null,
  74. 'main',
  75. $sessionUser->getRoles()
  76. );
  77. $this->tokenStorage->setToken($token);
  78. return true;
  79. }
  80. }
  81. }
  82. return false;
  83. }
  84. }