Skip to content

Commit 8ae8f47

Browse files
committed
logout after erasure
1 parent 6b30d85 commit 8ae8f47

File tree

6 files changed

+138
-161
lines changed

6 files changed

+138
-161
lines changed

Model/Customer/Anonymize/AccountBlocker.php

Lines changed: 0 additions & 129 deletions
This file was deleted.

Model/Customer/Anonymize/Processor/CustomerDataProcessor.php

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111
use Magento\Customer\Model\CustomerRegistry;
1212
use Magento\Framework\Api\SearchCriteriaBuilder;
1313
use Magento\Framework\App\Config\ScopeConfigInterface;
14+
use Magento\Framework\Exception\InputException;
1415
use Magento\Framework\Exception\LocalizedException;
1516
use Magento\Framework\Exception\NoSuchEntityException;
17+
use Magento\Framework\Exception\State\InputMismatchException;
1618
use Magento\Sales\Api\Data\OrderInterface;
19+
use Magento\Sales\Api\Data\OrderSearchResultInterface;
1720
use Magento\Sales\Api\OrderRepositoryInterface;
1821
use Magento\Store\Model\ScopeInterface;
19-
use Opengento\Gdpr\Model\Customer\Anonymize\AccountBlocker;
2022
use Opengento\Gdpr\Service\Anonymize\AnonymizerInterface;
2123
use Opengento\Gdpr\Service\Erase\ProcessorInterface;
2224

@@ -29,11 +31,6 @@ final class CustomerDataProcessor implements ProcessorInterface
2931
*/
3032
private $anonymizer;
3133

32-
/**
33-
* @var AccountBlocker
34-
*/
35-
private $accountBlocker;
36-
3734
/**
3835
* @var CustomerRepositoryInterface
3936
*/
@@ -61,15 +58,13 @@ final class CustomerDataProcessor implements ProcessorInterface
6158

6259
public function __construct(
6360
AnonymizerInterface $anonymizer,
64-
AccountBlocker $accountBlocker,
6561
CustomerRepositoryInterface $customerRepository,
6662
OrderRepositoryInterface $orderRepository,
6763
SearchCriteriaBuilder $criteriaBuilder,
6864
CustomerRegistry $customerRegistry,
6965
ScopeConfigInterface $scopeConfig
7066
) {
7167
$this->anonymizer = $anonymizer;
72-
$this->accountBlocker = $accountBlocker;
7368
$this->customerRepository = $customerRepository;
7469
$this->orderRepository = $orderRepository;
7570
$this->criteriaBuilder = $criteriaBuilder;
@@ -84,40 +79,45 @@ public function __construct(
8479
public function execute(int $customerId): bool
8580
{
8681
$isRemoved = false;
87-
try {
88-
if ($this->shouldRemoveCustomerWithoutOrders()) {
89-
$this->criteriaBuilder->addFilter(OrderInterface::CUSTOMER_ID, $customerId);
90-
$orderList = $this->orderRepository->getList($this->criteriaBuilder->create());
9182

92-
if (!$orderList->getTotalCount()) {
93-
$isRemoved = $this->customerRepository->deleteById($customerId);
94-
}
83+
try {
84+
if ($this->shouldRemoveCustomerWithoutOrders() && !$this->fetchOrdersList($customerId)->getTotalCount()) {
85+
$isRemoved = $this->customerRepository->deleteById($customerId);
9586
}
96-
97-
// Make sure, we don't work with cached customer data, because
98-
// saving cached customers may "de-anonymize" related data
99-
// like addresses
100-
$this->customerRegistry->remove($customerId);
101-
10287
if (!$isRemoved) {
103-
$this->accountBlocker->invalid($customerId);
104-
$this->customerRepository->save(
105-
$this->anonymizer->anonymize($this->customerRepository->getById($customerId))
106-
);
88+
$this->anonymizeCustomer($customerId);
10789
}
108-
10990
} catch (NoSuchEntityException $e) {
11091
return false;
11192
}
11293

11394
return true;
11495
}
11596

116-
private function shouldRemoveCustomerWithoutOrders(): bool
97+
private function fetchOrdersList(int $customerId): OrderSearchResultInterface
98+
{
99+
$this->criteriaBuilder->addFilter(OrderInterface::CUSTOMER_ID, $customerId);
100+
101+
return $this->orderRepository->getList($this->criteriaBuilder->create());
102+
}
103+
104+
/**
105+
* @param int $customerId
106+
* @throws LocalizedException
107+
* @throws NoSuchEntityException
108+
* @throws InputException
109+
* @throws InputMismatchException
110+
*/
111+
private function anonymizeCustomer(int $customerId): void
117112
{
118-
return $this->scopeConfig->isSetFlag(
119-
self::CONFIG_PATH_ERASURE_REMOVE_CUSTOMER,
120-
ScopeInterface::SCOPE_STORE
113+
$this->customerRegistry->remove($customerId);
114+
$this->customerRepository->save(
115+
$this->anonymizer->anonymize($this->customerRepository->getById($customerId))
121116
);
122117
}
118+
119+
private function shouldRemoveCustomerWithoutOrders(): bool
120+
{
121+
return $this->scopeConfig->isSetFlag(self::CONFIG_PATH_ERASURE_REMOVE_CUSTOMER, ScopeInterface::SCOPE_STORE);
122+
}
123123
}

Plugin/SessionChecker.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
/**
3+
* Copyright © OpenGento, All rights reserved.
4+
* See LICENSE bundled with this library for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Opengento\Gdpr\Plugin;
9+
10+
use Magento\Customer\Controller\AccountInterface;
11+
use Magento\Customer\Model\Session;
12+
use Magento\Framework\App\ActionInterface;
13+
use Magento\Framework\Exception\LocalizedException;
14+
use Magento\Framework\Message\ManagerInterface;
15+
use Magento\Framework\Phrase;
16+
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
17+
use Magento\Framework\Stdlib\CookieManagerInterface;
18+
use Opengento\Gdpr\Api\Data\EraseEntityInterface;
19+
use Opengento\Gdpr\Model\ResourceModel\EraseEntity\CollectionFactory;
20+
use Psr\Log\LoggerInterface;
21+
22+
final class SessionChecker
23+
{
24+
private $collectionFactory;
25+
26+
/**
27+
* @var Session
28+
*/
29+
private $session;
30+
31+
/**
32+
* @var CookieManagerInterface
33+
*/
34+
private $cookieManager;
35+
36+
/**
37+
* @var CookieMetadataFactory
38+
*/
39+
private $cookieMetadataFactory;
40+
41+
private $messageManager;
42+
43+
/**
44+
* @var LoggerInterface
45+
*/
46+
private $logger;
47+
48+
public function __construct(
49+
CollectionFactory $collectionFactory,
50+
Session $session,
51+
CookieManagerInterface $cookieManager,
52+
CookieMetadataFactory $cookieMetadataFactory,
53+
ManagerInterface $messageManager,
54+
LoggerInterface $logger
55+
) {
56+
$this->collectionFactory = $collectionFactory;
57+
$this->session = $session;
58+
$this->cookieManager = $cookieManager;
59+
$this->cookieMetadataFactory = $cookieMetadataFactory;
60+
$this->messageManager = $messageManager;
61+
$this->logger = $logger;
62+
}
63+
64+
public function aroundExecute(ActionInterface $action, callable $proceed, ...$arguments)
65+
{
66+
if ($this->session->isLoggedIn() && $this->isErased()) {
67+
$this->messageManager->addNoticeMessage(
68+
new Phrase('Your account have been erased and you have signed out.')
69+
);
70+
$this->logout();
71+
72+
if ($action instanceof AccountInterface) {
73+
return $this->session->authenticate();
74+
}
75+
}
76+
77+
return $proceed(...$arguments);
78+
}
79+
80+
private function logout(): void
81+
{
82+
$this->session->logout();
83+
$metadata = $this->cookieMetadataFactory->createCookieMetadata();
84+
$metadata->setPath('/');
85+
86+
try {
87+
$this->cookieManager->deleteCookie('mage-cache-sessid', $metadata);
88+
} catch (LocalizedException $e) {
89+
$this->logger->error($e->getLogMessage(), $e->getTrace());
90+
}
91+
}
92+
93+
private function isErased(): bool
94+
{
95+
$collection = $this->collectionFactory->create();
96+
$collection->addFieldToFilter(EraseEntityInterface::ENTITY_ID, $this->session->getCustomerId());
97+
$collection->addFieldToFilter(EraseEntityInterface::ENTITY_TYPE, 'customer');
98+
$collection->addFieldToFilter(EraseEntityInterface::STATE, EraseEntityInterface::STATE_COMPLETE);
99+
100+
return (bool) $collection->getSize();
101+
}
102+
}

etc/adminhtml/system/erasure.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
</field>
3333
<field id="delay" type="text" translate="label comment" showInDefault="1" showInWebsite="1" showInStore="1" sortOrder="30" canRestore="1">
3434
<label>Erasure Delay</label>
35-
<comment>Erasure delay in minute before the execution by the cron.</comment>
35+
<comment>Erasure delay in minute before the execution by the cron. From 60 to 43800.</comment>
3636
<validate>validate-number validate-number-range number-range-60-43800</validate>
3737
<depends>
3838
<field id="gdpr/erasure/enabled">1</field>

etc/frontend/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,7 @@
7878
<argument name="action" xsi:type="object">Opengento\Gdpr\Model\Action\ExportCreateAction</argument>
7979
</arguments>
8080
</type>
81+
<type name="Magento\Framework\App\ActionInterface">
82+
<plugin name="opengento_gdpr_customer_session_checker" type="Opengento\Gdpr\Plugin\SessionChecker" sortOrder="10"/>
83+
</type>
8184
</config>

i18n/en_US.csv

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Failed,Failed
4444
"Impossible to process the erasure: %1","Impossible to process the erasure: %1"
4545
"An export entity already exists for the entity type ""%1"" with ID ""%2"".","An export entity already exists for the entity type ""%1"" with ID ""%2""."
4646
"State ""%1"" does not exists.","State ""%1"" does not exists."
47+
"Your account have been erased and you have signed out.","Your account have been erased and you have signed out."
4748
"* Required Fields","* Required Fields"
4849
"Confirm password to continue","Confirm password to continue"
4950
Password,Password
@@ -96,7 +97,7 @@ Accept,Accept
9697
Erasure,Erasure
9798
"It will enable the erase action to the storefront.","It will enable the erase action to the storefront."
9899
"Erasure Delay","Erasure Delay"
99-
"Erasure delay in minute before the execution by the cron.","Erasure delay in minute before the execution by the cron."
100+
"Erasure delay in minute before the execution by the cron. From 60 to 43800.","Erasure delay in minute before the execution by the cron. From 60 to 43800."
100101
"Erase Entity Cron Schedule","Erase Entity Cron Schedule"
101102
"Entities Lifetime","Entities Lifetime"
102103
"The time is in days.","The time is in days."

0 commit comments

Comments
 (0)