From 3597dc2aa5aedb53cf98f2e070f71622a11d23f1 Mon Sep 17 00:00:00 2001 From: Thomas Klein Date: Sat, 24 Aug 2024 19:51:17 +0200 Subject: [PATCH] Fix multiple errors + remove unused types + fix admin actions --- .../Form/Field/AttributesAnonymizers.php | 3 +- Controller/Adminhtml/Guest/Erase.php | 41 +++++++++++------- Controller/Adminhtml/Guest/Export.php | 41 +++++++++++------- Controller/Adminhtml/Privacy/Erase.php | 40 +++++++++++------- Controller/Adminhtml/Privacy/Export.php | 42 ++++++++++++------- .../Config/Source/VirtualEntityAttributes.php | 37 ---------------- Model/Erase/NotifyEraseEntityManagement.php | 3 +- etc/adminhtml/di.xml | 5 --- etc/adminhtml/system/export.xml | 21 ++++++++++ 9 files changed, 132 insertions(+), 101 deletions(-) delete mode 100644 Model/Config/Source/VirtualEntityAttributes.php diff --git a/Block/Adminhtml/Config/Form/Field/AttributesAnonymizers.php b/Block/Adminhtml/Config/Form/Field/AttributesAnonymizers.php index 6e98899b..380419f7 100644 --- a/Block/Adminhtml/Config/Form/Field/AttributesAnonymizers.php +++ b/Block/Adminhtml/Config/Form/Field/AttributesAnonymizers.php @@ -33,7 +33,7 @@ public function getAnonymizersSelectRenderer(): Select ); } - return $this->getData('anonymizers_select_renderer'); + return $this->_getData('anonymizers_select_renderer'); } /** @@ -53,6 +53,7 @@ protected function _prepareToRender(): void 'anonymizer', [ 'label' => new Phrase('Anonymizer'), + 'class' => 'required-entry', 'renderer' => $this->getAnonymizersSelectRenderer(), ] ); diff --git a/Controller/Adminhtml/Guest/Erase.php b/Controller/Adminhtml/Guest/Erase.php index 856fa8de..99e373bf 100644 --- a/Controller/Adminhtml/Guest/Erase.php +++ b/Controller/Adminhtml/Guest/Erase.php @@ -8,44 +8,42 @@ namespace Opengento\Gdpr\Controller\Adminhtml\Guest; use Exception; -use Magento\Backend\Model\View\Result\RedirectFactory; -use Magento\Framework\App\Action\HttpPostActionInterface; -use Magento\Framework\App\RequestInterface; +use Magento\Backend\App\Action; +use Magento\Backend\App\Action\Context; use Magento\Framework\App\ResponseInterface; use Magento\Framework\Controller\ResultInterface; +use Magento\Framework\Exception\CouldNotSaveException; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Exception\NoSuchEntityException; -use Magento\Framework\Message\ManagerInterface; use Magento\Framework\Phrase; use Magento\Sales\Api\OrderRepositoryInterface; use Magento\Store\Model\StoreManagerInterface; +use Opengento\Gdpr\Api\Data\EraseEntityInterface; use Opengento\Gdpr\Api\EraseEntityManagementInterface; use Opengento\Gdpr\Api\EraseEntityRepositoryInterface; use Opengento\Gdpr\Model\Config; -class Erase implements HttpPostActionInterface +class Erase extends Action { public const ADMIN_RESOURCE = 'Opengento_Gdpr::order_erase'; public function __construct( - private RequestInterface $request, - private ManagerInterface $messageManager, + Context $context, private StoreManagerInterface $storeManager, private OrderRepositoryInterface $orderRepository, private EraseEntityManagementInterface $eraseEntityManagement, private EraseEntityRepositoryInterface $eraseEntityRepository, - private Config $config, - private RedirectFactory $redirectFactory, - ) {} + private Config $config + ) { + parent::__construct($context); + } public function execute(): ResultInterface|ResponseInterface { try { - $orderId = (int)$this->request->getParam('id'); + $orderId = (int)$this->getRequest()->getParam('id'); if ($this->isOrderErasureEnabled($orderId)) { - $this->eraseEntityManagement->process( - $this->eraseEntityRepository->getByEntity($orderId, 'order') - ); + $this->eraseEntityManagement->process($this->fetchEntity($orderId)); $this->messageManager->addSuccessMessage(new Phrase('You erased the order.')); } } catch (LocalizedException $e) { @@ -54,7 +52,7 @@ public function execute(): ResultInterface|ResponseInterface $this->messageManager->addExceptionMessage($e, new Phrase('An error occurred on the server.')); } - return $this->redirectFactory->create()->setPath('sales/order/index'); + return $this->resultRedirectFactory->create()->setPath('sales/order/index'); } /** @@ -66,4 +64,17 @@ private function isOrderErasureEnabled(int $orderId): bool $this->storeManager->getStore($this->orderRepository->get($orderId)->getStoreId())->getWebsiteId() ); } + + /** + * @throws CouldNotSaveException + * @throws LocalizedException + */ + private function fetchEntity(int $orderId): EraseEntityInterface + { + try { + return $this->eraseEntityRepository->getByEntity($orderId, 'order'); + } catch (NoSuchEntityException) { + return $this->eraseEntityManagement->create($orderId, 'order'); + } + } } diff --git a/Controller/Adminhtml/Guest/Export.php b/Controller/Adminhtml/Guest/Export.php index e141606a..37e07fc3 100644 --- a/Controller/Adminhtml/Guest/Export.php +++ b/Controller/Adminhtml/Guest/Export.php @@ -8,47 +8,46 @@ namespace Opengento\Gdpr\Controller\Adminhtml\Guest; use Exception; -use Magento\Backend\Model\View\Result\RedirectFactory; -use Magento\Framework\App\Action\HttpGetActionInterface; +use Magento\Backend\App\Action; +use Magento\Backend\App\Action\Context; use Magento\Framework\App\Filesystem\DirectoryList; -use Magento\Framework\App\RequestInterface; use Magento\Framework\App\Response\Http\FileFactory; use Magento\Framework\App\ResponseInterface; use Magento\Framework\Controller\ResultInterface; +use Magento\Framework\Exception\AlreadyExistsException; +use Magento\Framework\Exception\CouldNotSaveException; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Exception\NoSuchEntityException; -use Magento\Framework\Message\ManagerInterface; use Magento\Framework\Phrase; use Magento\Sales\Api\OrderRepositoryInterface; use Magento\Store\Model\StoreManagerInterface; +use Opengento\Gdpr\Api\Data\ExportEntityInterface; use Opengento\Gdpr\Api\ExportEntityManagementInterface; use Opengento\Gdpr\Api\ExportEntityRepositoryInterface; use Opengento\Gdpr\Model\Config; -class Export implements HttpGetActionInterface +class Export extends Action { public const ADMIN_RESOURCE = 'Opengento_Gdpr::order_export'; public function __construct( - private RequestInterface $request, - private ManagerInterface $messageManager, + Context $context, private StoreManagerInterface $storeManager, private OrderRepositoryInterface $orderRepository, private ExportEntityManagementInterface $exportEntityManagement, private ExportEntityRepositoryInterface $exportEntityRepository, private Config $config, - private RedirectFactory $redirectFactory, private FileFactory $fileFactory - ) {} + ) { + parent::__construct($context); + } public function execute(): ResultInterface|ResponseInterface { try { - $orderId = (int)$this->request->getParam('id'); + $orderId = (int)$this->getRequest()->getParam('id'); if ($this->isOrderExportEnabled($orderId)) { - $exportEntity = $this->exportEntityManagement->export( - $this->exportEntityRepository->getByEntity($orderId, 'order') - ); + $exportEntity = $this->exportEntityManagement->export($this->fetchEntity($orderId)); return $this->fileFactory->create( 'guest_privacy_data_' . $exportEntity->getEntityId() . '.zip', @@ -65,7 +64,7 @@ public function execute(): ResultInterface|ResponseInterface $this->messageManager->addExceptionMessage($e, new Phrase('An error occurred on the server.')); } - return $this->redirectFactory->create()->setRefererOrBaseUrl(); + return $this->resultRedirectFactory->create()->setRefererOrBaseUrl(); } /** @@ -77,4 +76,18 @@ private function isOrderExportEnabled(int $orderId): bool $this->storeManager->getStore($this->orderRepository->get($orderId)->getStoreId())->getWebsiteId() ); } + + /** + * @throws AlreadyExistsException + * @throws CouldNotSaveException + * @throws LocalizedException + */ + private function fetchEntity(int $orderId): ExportEntityInterface + { + try { + return $this->exportEntityRepository->getByEntity($orderId, 'order'); + } catch (NoSuchEntityException) { + return $this->exportEntityManagement->create($orderId, 'order'); + } + } } diff --git a/Controller/Adminhtml/Privacy/Erase.php b/Controller/Adminhtml/Privacy/Erase.php index 2b0bed06..e0a8d97b 100644 --- a/Controller/Adminhtml/Privacy/Erase.php +++ b/Controller/Adminhtml/Privacy/Erase.php @@ -8,41 +8,40 @@ namespace Opengento\Gdpr\Controller\Adminhtml\Privacy; use Exception; -use Magento\Backend\Model\View\Result\RedirectFactory; +use Magento\Backend\App\Action; +use Magento\Backend\App\Action\Context; use Magento\Customer\Api\CustomerRepositoryInterface; -use Magento\Framework\App\Action\HttpPostActionInterface; -use Magento\Framework\App\RequestInterface; use Magento\Framework\App\ResponseInterface; use Magento\Framework\Controller\ResultInterface; +use Magento\Framework\Exception\CouldNotSaveException; use Magento\Framework\Exception\LocalizedException; -use Magento\Framework\Message\ManagerInterface; +use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\Phrase; +use Opengento\Gdpr\Api\Data\EraseEntityInterface; use Opengento\Gdpr\Api\EraseEntityManagementInterface; use Opengento\Gdpr\Api\EraseEntityRepositoryInterface; use Opengento\Gdpr\Model\Config; -class Erase implements HttpPostActionInterface +class Erase extends Action { public const ADMIN_RESOURCE = 'Opengento_Gdpr::customer_erase'; public function __construct( - private RequestInterface $request, - private ManagerInterface $messageManager, + Context $context, private CustomerRepositoryInterface $customerRepository, private EraseEntityManagementInterface $eraseEntityManagement, private EraseEntityRepositoryInterface $eraseEntityRepository, - private RedirectFactory $redirectFactory, private Config $config - ) {} + ) { + parent::__construct($context); + } public function execute(): ResultInterface|ResponseInterface { try { - $customerId = (int)$this->request->getParam('id'); + $customerId = (int)$this->getRequest()->getParam('id'); if ($this->config->isErasureEnabled($this->customerRepository->getById($customerId)->getWebsiteId())) { - $this->eraseEntityManagement->process( - $this->eraseEntityRepository->getByEntity($customerId, 'customer') - ); + $this->eraseEntityManagement->process($this->fetchEntity($customerId)); $this->messageManager->addSuccessMessage(new Phrase('You erased the customer.')); } } catch (LocalizedException $e) { @@ -51,6 +50,19 @@ public function execute(): ResultInterface|ResponseInterface $this->messageManager->addExceptionMessage($e, new Phrase('An error occurred on the server.')); } - return $this->redirectFactory->create()->setPath('customer/index'); + return $this->resultRedirectFactory->create()->setPath('customer/index'); + } + + /** + * @throws CouldNotSaveException + * @throws LocalizedException + */ + private function fetchEntity(int $customerId): EraseEntityInterface + { + try { + return $this->eraseEntityRepository->getByEntity($customerId, 'customer'); + } catch (NoSuchEntityException) { + return $this->eraseEntityManagement->create($customerId, 'customer'); + } } } diff --git a/Controller/Adminhtml/Privacy/Export.php b/Controller/Adminhtml/Privacy/Export.php index 79e842a2..e0280916 100644 --- a/Controller/Adminhtml/Privacy/Export.php +++ b/Controller/Adminhtml/Privacy/Export.php @@ -8,44 +8,44 @@ namespace Opengento\Gdpr\Controller\Adminhtml\Privacy; use Exception; -use Magento\Backend\Model\View\Result\RedirectFactory; +use Magento\Backend\App\Action; +use Magento\Backend\App\Action\Context; use Magento\Customer\Api\CustomerRepositoryInterface; -use Magento\Framework\App\Action\HttpGetActionInterface; use Magento\Framework\App\Filesystem\DirectoryList; -use Magento\Framework\App\RequestInterface; use Magento\Framework\App\Response\Http\FileFactory; use Magento\Framework\App\ResponseInterface; use Magento\Framework\Controller\ResultInterface; +use Magento\Framework\Exception\AlreadyExistsException; +use Magento\Framework\Exception\CouldNotSaveException; use Magento\Framework\Exception\LocalizedException; -use Magento\Framework\Message\ManagerInterface; +use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\Phrase; +use Opengento\Gdpr\Api\Data\ExportEntityInterface; use Opengento\Gdpr\Api\ExportEntityManagementInterface; use Opengento\Gdpr\Api\ExportEntityRepositoryInterface; use Opengento\Gdpr\Model\Config; -class Export implements HttpGetActionInterface +class Export extends Action { public const ADMIN_RESOURCE = 'Opengento_Gdpr::customer_export'; public function __construct( - private RequestInterface $request, - private ManagerInterface $messageManager, + Context $context, private CustomerRepositoryInterface $customerRepository, private ExportEntityManagementInterface $exportEntityManagement, private ExportEntityRepositoryInterface $exportEntityRepository, - private RedirectFactory $redirectFactory, private Config $config, private FileFactory $fileFactory - ) {} + ) { + parent::__construct($context); + } public function execute(): ResultInterface|ResponseInterface { try { - $customerId = (int)$this->request->getParam('id'); + $customerId = (int)$this->getRequest()->getParam('id'); if ($this->config->isExportEnabled($this->customerRepository->getById($customerId)->getWebsiteId())) { - $exportEntity = $this->exportEntityManagement->export( - $this->exportEntityRepository->getByEntity($customerId, 'customer') - ); + $exportEntity = $this->exportEntityManagement->export($this->fetchEntity($customerId)); return $this->fileFactory->create( 'customer_privacy_data_' . $exportEntity->getEntityId() . '.zip', @@ -62,6 +62,20 @@ public function execute(): ResultInterface|ResponseInterface $this->messageManager->addExceptionMessage($e, new Phrase('An error occurred on the server.')); } - return $this->redirectFactory->create()->setRefererOrBaseUrl(); + return $this->resultRedirectFactory->create()->setRefererOrBaseUrl(); + } + + /** + * @throws AlreadyExistsException + * @throws CouldNotSaveException + * @throws LocalizedException + */ + private function fetchEntity(int $customerId): ExportEntityInterface + { + try { + return $this->exportEntityRepository->getByEntity($customerId, 'customer'); + } catch (NoSuchEntityException) { + return $this->exportEntityManagement->create($customerId, 'customer'); + } } } diff --git a/Model/Config/Source/VirtualEntityAttributes.php b/Model/Config/Source/VirtualEntityAttributes.php deleted file mode 100644 index 22fbfe60..00000000 --- a/Model/Config/Source/VirtualEntityAttributes.php +++ /dev/null @@ -1,37 +0,0 @@ -options ??= array_map( - static fn (string $attribute): array => ['value' => $attribute, 'label' => $attribute], - array_keys($this->attributeProvider->getAttributes($this->entityType)) - ); - } -} diff --git a/Model/Erase/NotifyEraseEntityManagement.php b/Model/Erase/NotifyEraseEntityManagement.php index be4d9598..448ac41f 100644 --- a/Model/Erase/NotifyEraseEntityManagement.php +++ b/Model/Erase/NotifyEraseEntityManagement.php @@ -29,9 +29,10 @@ public function create(int $entityId, string $entityType): EraseEntityInterface public function cancel(int $entityId, string $entityType): bool { + $eraseEntity = $this->eraseRepository->getByEntity($entityId, $entityType); $canceled = $this->eraseManagement->cancel($entityId, $entityType); if ($canceled) { - $this->notifierRepository->get($entityType, 'cancel')->notify($this->eraseRepository->getByEntity($entityId, $entityType)); + $this->notifierRepository->get($entityType, 'cancel')->notify($eraseEntity); } return $canceled; diff --git a/etc/adminhtml/di.xml b/etc/adminhtml/di.xml index 6f51b268..7b57f68d 100644 --- a/etc/adminhtml/di.xml +++ b/etc/adminhtml/di.xml @@ -7,11 +7,6 @@ --> - - - Magento\Framework\Model\EntitySnapshot\AttributeProvider - - Magento\Customer\Api\CustomerMetadataInterface diff --git a/etc/adminhtml/system/export.xml b/etc/adminhtml/system/export.xml index 94d9361a..7fca75cb 100644 --- a/etc/adminhtml/system/export.xml +++ b/etc/adminhtml/system/export.xml @@ -74,6 +74,9 @@ + + 1 + This attributes list will be exported when processed. @@ -108,6 +111,9 @@ + + 1 + This attributes list will be exported when processed. @@ -142,6 +148,9 @@ + + 1 + Comma-separated @@ -175,6 +184,9 @@ + + 1 + Comma-separated @@ -208,6 +220,9 @@ + + 1 + Comma-separated @@ -241,6 +256,9 @@ + + 1 + Comma-separated @@ -274,6 +292,9 @@ + + 1 + Comma-separated