Skip to content

Commit db7c51f

Browse files
committed
Merge branch 'master' into magento2.4.x
2 parents e7af87a + 3361b1e commit db7c51f

File tree

9 files changed

+150
-164
lines changed

9 files changed

+150
-164
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: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
/**
25+
* @var CollectionFactory
26+
*/
27+
private $collectionFactory;
28+
29+
/**
30+
* @var Session
31+
*/
32+
private $session;
33+
34+
/**
35+
* @var CookieManagerInterface
36+
*/
37+
private $cookieManager;
38+
39+
/**
40+
* @var CookieMetadataFactory
41+
*/
42+
private $cookieMetadataFactory;
43+
44+
/**
45+
* @var ManagerInterface
46+
*/
47+
private $messageManager;
48+
49+
/**
50+
* @var LoggerInterface
51+
*/
52+
private $logger;
53+
54+
public function __construct(
55+
CollectionFactory $collectionFactory,
56+
Session $session,
57+
CookieManagerInterface $cookieManager,
58+
CookieMetadataFactory $cookieMetadataFactory,
59+
ManagerInterface $messageManager,
60+
LoggerInterface $logger
61+
) {
62+
$this->collectionFactory = $collectionFactory;
63+
$this->session = $session;
64+
$this->cookieManager = $cookieManager;
65+
$this->cookieMetadataFactory = $cookieMetadataFactory;
66+
$this->messageManager = $messageManager;
67+
$this->logger = $logger;
68+
}
69+
70+
public function aroundExecute(ActionInterface $action, callable $proceed, ...$arguments)
71+
{
72+
if ($this->session->isLoggedIn() && $this->isErased()) {
73+
$this->messageManager->addNoticeMessage(
74+
new Phrase('Your account have been erased and you have signed out.')
75+
);
76+
$this->logout();
77+
78+
if ($action instanceof AccountInterface) {
79+
return $this->session->authenticate();
80+
}
81+
}
82+
83+
return $proceed(...$arguments);
84+
}
85+
86+
private function logout(): void
87+
{
88+
$this->session->logout();
89+
$metadata = $this->cookieMetadataFactory->createCookieMetadata();
90+
$metadata->setPath('/');
91+
92+
try {
93+
$this->cookieManager->deleteCookie('mage-cache-sessid', $metadata);
94+
} catch (LocalizedException $e) {
95+
$this->logger->error($e->getLogMessage(), $e->getTrace());
96+
}
97+
}
98+
99+
private function isErased(): bool
100+
{
101+
$collection = $this->collectionFactory->create();
102+
$collection->addFieldToFilter(EraseEntityInterface::ENTITY_ID, $this->session->getCustomerId());
103+
$collection->addFieldToFilter(EraseEntityInterface::ENTITY_TYPE, 'customer');
104+
$collection->addFieldToFilter(EraseEntityInterface::STATE, EraseEntityInterface::STATE_COMPLETE);
105+
106+
return (bool) $collection->getSize();
107+
}
108+
}

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/de_DE.csv

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"Impossible to process the erasure: %1","Unmöglich, die Löschung zu verarbeiten: %1"
4545
"An export entity already exists for the entity type ""%1"" with ID ""%2"".","Für den Entitätstyp ""%1"" mit der ID ""%2"" existiert bereits eine Exportentität."
4646
"State ""%1"" does not exists.","Der Zustand ""%1"" ist nicht vorhanden."
47+
"Your account have been erased and you have signed out.","Ihr Konto wurde gelöscht und Sie haben sich abgemeldet."
4748
"* Required Fields","* Erforderliche Felder"
4849
"Confirm password to continue","Passwort bestätigen, um fortzufahren"
4950
"Password","Kennwort"
@@ -96,7 +97,7 @@
9697
"Erasure","Löschung"
9798
"It will enable the erase action to the storefront.","Es wird die Löschaktion bis zur Storefront ermöglicht."
9899
"Erasure Delay","Verzögerung der Löschung"
99-
"Erasure delay in minute before the execution by the cron.","Löschverzögerung in Minuten vor der Ausführung durch den Cron."
100+
"Erasure delay in minute before the execution by the cron. From 60 to 43800.","Löschverzögerung in Minuten vor der Ausführung durch den Cron. Von 60 bis 43800."
100101
"Erase Entity Cron Schedule","Cron-Zeitplan für das Löschen der Entität "
101102
"Entities Lifetime","Entitäten Lebensdauer"
102103
"The time is in days.","Die Zeit wird in Tagen angegeben."

0 commit comments

Comments
 (0)