1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Shopware\Core\Checkout\Customer\SalesChannel; |
4
|
|
|
|
5
|
|
|
use OpenApi\Annotations as OA; |
6
|
|
|
use Shopware\Core\Checkout\Cart\SalesChannel\CartService; |
7
|
|
|
use Shopware\Core\Checkout\Customer\Event\CustomerLogoutEvent; |
8
|
|
|
use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException; |
9
|
|
|
use Shopware\Core\Framework\Routing\Annotation\LoginRequired; |
10
|
|
|
use Shopware\Core\Framework\Routing\Annotation\RouteScope; |
11
|
|
|
use Shopware\Core\Framework\Routing\Annotation\Since; |
12
|
|
|
use Shopware\Core\Framework\Util\Random; |
13
|
|
|
use Shopware\Core\Framework\Validation\DataBag\RequestDataBag; |
14
|
|
|
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextPersister; |
15
|
|
|
use Shopware\Core\System\SalesChannel\ContextTokenResponse; |
16
|
|
|
use Shopware\Core\System\SalesChannel\SalesChannelContext; |
17
|
|
|
use Shopware\Core\System\SystemConfig\SystemConfigService; |
18
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
19
|
|
|
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @RouteScope(scopes={"store-api"}) |
23
|
|
|
*/ |
24
|
|
|
class LogoutRoute extends AbstractLogoutRoute |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var SalesChannelContextPersister |
28
|
|
|
*/ |
29
|
|
|
private $contextPersister; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var EventDispatcherInterface |
33
|
|
|
*/ |
34
|
|
|
private $eventDispatcher; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var SystemConfigService |
38
|
|
|
*/ |
39
|
|
|
private $systemConfig; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var CartService |
43
|
|
|
*/ |
44
|
|
|
private $cartService; |
45
|
|
|
|
46
|
|
|
public function __construct( |
47
|
|
|
SalesChannelContextPersister $contextPersister, |
48
|
|
|
EventDispatcherInterface $eventDispatcher, |
49
|
|
|
SystemConfigService $systemConfig, |
50
|
|
|
CartService $cartService |
51
|
|
|
) { |
52
|
|
|
$this->contextPersister = $contextPersister; |
53
|
|
|
$this->eventDispatcher = $eventDispatcher; |
54
|
|
|
$this->systemConfig = $systemConfig; |
55
|
|
|
$this->cartService = $cartService; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getDecorated(): AbstractLogoutRoute |
59
|
|
|
{ |
60
|
|
|
throw new DecorationPatternException(self::class); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @Since("6.2.0.0") |
65
|
|
|
* @OA\Post( |
66
|
|
|
* path="/account/logout", |
67
|
|
|
* summary="Logouts current loggedin customer", |
68
|
|
|
* operationId="logoutCustomer", |
69
|
|
|
* tags={"Store API", "Account"}, |
70
|
|
|
* @OA\Response( |
71
|
|
|
* response="200", |
72
|
|
|
* description="" |
73
|
|
|
* ) |
74
|
|
|
* ) |
75
|
|
|
* @LoginRequired() |
76
|
|
|
* @Route(path="/store-api/v{version}/account/logout", name="store-api.account.logout", methods={"POST"}) |
77
|
|
|
*/ |
78
|
|
|
public function logout(SalesChannelContext $context, ?RequestDataBag $data = null): ContextTokenResponse |
79
|
|
|
{ |
80
|
|
|
if ($this->shouldDelete($context)) { |
81
|
|
|
$this->cartService->deleteCart($context); |
82
|
|
|
$this->contextPersister->delete($context->getToken()); |
83
|
|
|
|
84
|
|
|
$event = new CustomerLogoutEvent($context, $context->getCustomer()); |
|
|
|
|
85
|
|
|
$this->eventDispatcher->dispatch($event); |
86
|
|
|
|
87
|
|
|
return new ContextTokenResponse($context->getToken()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$newToken = Random::getAlphanumericString(32); |
91
|
|
|
if ($data && (bool) $data->get('replace-token')) { |
92
|
|
|
$newToken = $this->contextPersister->replace($context->getToken(), $context); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$context->assign([ |
96
|
|
|
'token' => $newToken, |
97
|
|
|
]); |
98
|
|
|
|
99
|
|
|
$event = new CustomerLogoutEvent($context, $context->getCustomer()); |
100
|
|
|
$this->eventDispatcher->dispatch($event); |
101
|
|
|
|
102
|
|
|
return new ContextTokenResponse($context->getToken()); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
private function shouldDelete(SalesChannelContext $context): bool |
106
|
|
|
{ |
107
|
|
|
$config = $this->systemConfig->get('core.loginRegistration.invalidateSessionOnLogOut', $context->getSalesChannelId()); |
108
|
|
|
|
109
|
|
|
if ($config) { |
110
|
|
|
return true; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if ($context->getCustomer() === null) { |
114
|
|
|
return true; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $context->getCustomer()->getGuest(); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|