From 31ff4b9f1f89f55f7681f3d1d0d498f449a9ebf9 Mon Sep 17 00:00:00 2001 From: Flurrybox Date: Thu, 15 Aug 2019 12:41:49 +0300 Subject: [PATCH] Fix customer deletion --- Cron/Schedule.php | 28 ++++------------------------ Privacy/Delete/Customer.php | 14 +++++++++++++- composer.json | 2 +- 3 files changed, 18 insertions(+), 26 deletions(-) diff --git a/Cron/Schedule.php b/Cron/Schedule.php index adf21a2..1f877d7 100644 --- a/Cron/Schedule.php +++ b/Cron/Schedule.php @@ -20,9 +20,7 @@ use Exception; use Flurrybox\EnhancedPrivacy\Api\CustomerManagementInterface; -use Flurrybox\EnhancedPrivacy\Api\Data\ReasonInterfaceFactory; use Flurrybox\EnhancedPrivacy\Api\Data\ScheduleInterface; -use Flurrybox\EnhancedPrivacy\Api\ReasonRepositoryInterface; use Flurrybox\EnhancedPrivacy\Api\ScheduleRepositoryInterface; use Flurrybox\EnhancedPrivacy\Helper\Data as PrivacyHelper; use Magento\Customer\Api\CustomerRepositoryInterface; @@ -69,16 +67,6 @@ class Schedule */ protected $customerRepository; - /** - * @var ReasonInterfaceFactory - */ - protected $reasonFactory; - - /** - * @var ReasonRepositoryInterface - */ - protected $reasonRepository; - /** * @var ResourceConnection */ @@ -103,8 +91,6 @@ class Schedule * @param SearchCriteriaBuilder $criteriaBuilder * @param CustomerManagementInterface $customerManagement * @param CustomerRepositoryInterface $customerRepository - * @param ReasonInterfaceFactory $reasonFactory - * @param ReasonRepositoryInterface $reasonRepository * @param ResourceConnection $resourceConnection * @param LoggerInterface $logger * @param ManagerInterface $eventManager @@ -116,8 +102,6 @@ public function __construct( SearchCriteriaBuilder $criteriaBuilder, CustomerManagementInterface $customerManagement, CustomerRepositoryInterface $customerRepository, - ReasonInterfaceFactory $reasonFactory, - ReasonRepositoryInterface $reasonRepository, ResourceConnection $resourceConnection, LoggerInterface $logger, ManagerInterface $eventManager @@ -128,8 +112,6 @@ public function __construct( $this->criteriaBuilder = $criteriaBuilder; $this->customerManagement = $customerManagement; $this->customerRepository = $customerRepository; - $this->reasonFactory = $reasonFactory; - $this->reasonRepository = $reasonRepository; $this->resourceConnection = $resourceConnection; $this->logger = $logger; $this->eventManager = $eventManager; @@ -165,11 +147,9 @@ public function execute() $this->processCustomer($schedule, $customer); - $reason = $this->reasonFactory->create(); - $reason->setReason($schedule->getReason()); - - $this->reasonRepository->save($reason); $this->scheduleRepository->delete($schedule); + + $this->resourceConnection->getConnection()->commit(); } catch (Exception $e) { $this->resourceConnection->getConnection()->rollBack(); $this->logger->error($e->getMessage()); @@ -189,11 +169,11 @@ public function execute() protected function processCustomer(ScheduleInterface $schedule, CustomerInterface $customer) { switch ($schedule->getType()) { - case ScheduleInterface::TYPE_DELETE: + case PrivacyHelper::SCHEDULE_TYPE_DELETE: $this->customerManagement->deleteCustomer($customer); break; - case ScheduleInterface::TYPE_ANONYMIZE: + case PrivacyHelper::SCHEDULE_TYPE_ANONYMIZE: $this->customerManagement->anonymizeCustomer($customer); break; diff --git a/Privacy/Delete/Customer.php b/Privacy/Delete/Customer.php index 9baef7a..e72a2fe 100644 --- a/Privacy/Delete/Customer.php +++ b/Privacy/Delete/Customer.php @@ -27,6 +27,7 @@ use Magento\Eav\Model\Entity\Attribute\Source\Boolean; use Magento\Framework\Encryption\EncryptorInterface; use Magento\Framework\Math\Random; +use Magento\Framework\Registry; /** * Process customer data. @@ -48,21 +49,29 @@ class Customer implements DataDeleteInterface */ protected $random; + /** + * @var Registry + */ + protected $registry; + /** * CustomerData constructor. * * @param CustomerRepositoryInterface $customerRepository * @param EncryptorInterface $encryptor * @param Random $random + * @param Registry $registry */ public function __construct( CustomerRepositoryInterface $customerRepository, EncryptorInterface $encryptor, - Random $random + Random $random, + Registry $registry ) { $this->customerRepository = $customerRepository; $this->encryptor = $encryptor; $this->random = $random; + $this->registry = $registry; } /** @@ -76,6 +85,9 @@ public function __construct( */ public function delete(CustomerInterface $customer) { + /** @see \Magento\Framework\Model\ActionValidator\RemoveAction::isAllowed() **/ + $this->registry->register('isSecureArea', true); + $this->customerRepository->deleteById($customer->getId()); } diff --git a/composer.json b/composer.json index 785723d..78c6281 100755 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "flurrybox/enhanced-privacy", "description": "Magento 2 module to make your store GDPR compatible", "type": "magento2-module", - "version": "2.0.0", + "version": "2.1.0", "authors": [ { "name": "Flurrybox",