-
Notifications
You must be signed in to change notification settings - Fork 91
/
LogoutExceptionSubscriber.php
118 lines (102 loc) · 3.86 KB
/
LogoutExceptionSubscriber.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
declare(strict_types=1);
namespace Shopsys\FrameworkBundle\Component\Error;
use Shopsys\FrameworkBundle\Component\Domain\Domain;
use Shopsys\FrameworkBundle\Component\FlashMessage\FlashBagProvider;
use Shopsys\FrameworkBundle\Component\FlashMessage\FlashMessage;
use Shopsys\FrameworkBundle\Model\Customer\User\CurrentCustomerUser;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Exception\LogoutException;
class LogoutExceptionSubscriber implements EventSubscriberInterface
{
/**
* @var \Shopsys\FrameworkBundle\Component\FlashMessage\FlashBagProvider
*/
protected FlashBagProvider $flashBagProvider;
/**
* @var \Shopsys\FrameworkBundle\Model\Customer\User\CurrentCustomerUser
*/
protected $currentCustomerUser;
/**
* @var \Symfony\Component\Routing\RouterInterface
*/
protected $router;
/**
* @var \Shopsys\FrameworkBundle\Component\Domain\Domain
*/
protected $domain;
/**
* @param \Shopsys\FrameworkBundle\Component\FlashMessage\FlashBagProvider $flashBagProvider
* @param \Shopsys\FrameworkBundle\Model\Customer\User\CurrentCustomerUser $currentCustomerUser
* @param \Symfony\Component\Routing\RouterInterface $router
* @param \Shopsys\FrameworkBundle\Component\Domain\Domain $domain
*/
public function __construct(
FlashBagProvider $flashBagProvider,
CurrentCustomerUser $currentCustomerUser,
RouterInterface $router,
Domain $domain
) {
$this->flashBagProvider = $flashBagProvider;
$this->currentCustomerUser = $currentCustomerUser;
$this->router = $router;
$this->domain = $domain;
}
/**
* {@inheritDoc}
*/
public static function getSubscribedEvents()
{
return [
KernelEvents::EXCEPTION => ['onKernelException'],
];
}
/**
* @param \Symfony\Component\HttpKernel\Event\ExceptionEvent $event
*/
public function onKernelException(ExceptionEvent $event): void
{
if (
$event->getThrowable() instanceof LogoutException
|| $event->getThrowable()->getPrevious() instanceof LogoutException
) {
if ($this->currentCustomerUser->findCurrentCustomerUser() !== null) {
$domainId = $this->currentCustomerUser->findCurrentCustomerUser()->getDomainId();
$locale = $this->domain->getDomainConfigById($domainId)->getLocale();
$this->flashBagProvider->getFlashBag()->add(
FlashMessage::KEY_ERROR,
t(
'There was an error during logout attempt. If you really want to sign out, please try it again.',
[],
'messages',
$locale
)
);
}
$redirectUrl = $this->getSafeUrlToRedirect($event->getRequest()->headers->get('referer'));
$event->setResponse(new RedirectResponse($redirectUrl));
}
}
/**
* @param string|null $url
* @return string
*/
protected function getSafeUrlToRedirect(?string $url): string
{
if ($url !== null) {
$urlParse = parse_url($url);
$domainUrl = $this->domain->getUrl();
$domainUrlParse = parse_url($domainUrl);
$parsedUrl = $urlParse['scheme'] . $urlParse['host'];
$parsedDomainUrl = $domainUrlParse['scheme'] . $domainUrlParse['host'];
if ($parsedUrl === $parsedDomainUrl) {
return $url;
}
}
return $this->router->generate('front_homepage');
}
}