vendor/nellapp/sdk-bundle/src/EventSubscriber/AbstractForceUserFromSetupSubscriber.php line 41

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the sdk-bundle-source package.
  4. *
  5. * (c) Benjamin Georgeault
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Nellapp\Bundle\SDKBundle\EventSubscriber;
  11. use Nellapp\Bundle\SDKBundle\Routing\UrlGeneratorInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpFoundation\RedirectResponse;
  14. use Symfony\Component\HttpKernel\Event\RequestEvent;
  15. use Symfony\Component\HttpKernel\KernelEvents;
  16. use Symfony\Component\Security\Core\Security;
  17. use Symfony\Component\Security\Core\User\UserInterface;
  18. /**
  19. * Class AbstractForceUserFromSetupSubscriber
  20. *
  21. * @author Benjamin Georgeault
  22. */
  23. abstract class AbstractForceUserFromSetupSubscriber implements EventSubscriberInterface
  24. {
  25. public function __construct(
  26. private Security $security,
  27. private UrlGeneratorInterface $urlGenerator,
  28. ) {}
  29. public static function getSubscribedEvents(): array
  30. {
  31. return [
  32. KernelEvents::REQUEST => '__invoke',
  33. ];
  34. }
  35. public function __invoke(RequestEvent $event): void
  36. {
  37. if (!$event->isMainRequest()) {
  38. return;
  39. }
  40. if (
  41. 'security.firewall.map.context.dev' === $event->getRequest()->attributes->get('_firewall_context')
  42. || (null === $user = $this->security->getUser())
  43. || $this->preventDo($event, $user)
  44. || !$this->hasUserFromChannel($user)
  45. ) {
  46. return;
  47. }
  48. $event->setResponse(new RedirectResponse($this->urlGenerator->generate('core', 'app_account_edit')));
  49. }
  50. protected function preventDo(RequestEvent $event, UserInterface $user): bool
  51. {
  52. return false;
  53. }
  54. abstract protected function hasUserFromChannel(UserInterface $user): bool;
  55. }