From 0658c6ef9bc93bbcdd5143d0152d2bd65a980401 Mon Sep 17 00:00:00 2001 From: Karlis Suvi <45097959+ks129@users.noreply.github.com> Date: Mon, 23 Sep 2024 09:36:41 +0300 Subject: [PATCH 1/3] Add customer ID to the customer activity list --- views/templates/admin/tabs/customerActivity.tpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/views/templates/admin/tabs/customerActivity.tpl b/views/templates/admin/tabs/customerActivity.tpl index c4075404..34969c29 100644 --- a/views/templates/admin/tabs/customerActivity.tpl +++ b/views/templates/admin/tabs/customerActivity.tpl @@ -36,7 +36,7 @@ {foreach from=$logs item=log} - {$log.client_name|escape:'htmlall':'UTF-8'} + {$log.client_name|escape:'htmlall':'UTF-8'} (ID: {$log.id_customer|escape:'htmlall':'UTF-8'}) {if $log.request_type eq 1} {l s='Consent confirmation' d='Modules.Psgdpr.Admin'} {/if} From c01a6e93d2ac2958051ada70983c17f07410f26d Mon Sep 17 00:00:00 2001 From: Karlis Suvi <45097959+ks129@users.noreply.github.com> Date: Mon, 23 Sep 2024 13:27:48 +0300 Subject: [PATCH 2/3] Implement anonymization of customer activity logs --- config/services.yml | 1 + src/Repository/LoggerRepository.php | 28 +++++++++++++++++++ .../Strategy/BackResponderByCustomerId.php | 4 ++- src/Service/CustomerService.php | 27 +++++++++++++++++- 4 files changed, 58 insertions(+), 2 deletions(-) diff --git a/config/services.yml b/config/services.yml index 605250bb..d6fe475f 100644 --- a/config/services.yml +++ b/config/services.yml @@ -36,6 +36,7 @@ services: - "@prestashop.core.query_bus" - "@prestashop.adapter.group.provider.default_groups_provider" - "@hashing" + - "@PrestaShop\\Module\\Psgdpr\\Repository\\LoggerRepository" PrestaShop\Module\Psgdpr\Service\Export\Strategy\ExportToCsv: class: 'PrestaShop\Module\Psgdpr\Service\Export\Strategy\ExportToCsv' diff --git a/src/Repository/LoggerRepository.php b/src/Repository/LoggerRepository.php index 37101b77..7d75c958 100644 --- a/src/Repository/LoggerRepository.php +++ b/src/Repository/LoggerRepository.php @@ -23,6 +23,7 @@ use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Persistence\ManagerRegistry; use PrestaShop\Module\Psgdpr\Entity\PsgdprLog; +use PrestaShop\PrestaShop\Core\Domain\Customer\ValueObject\CustomerId; class LoggerRepository extends ServiceEntityRepository { @@ -58,4 +59,31 @@ public function findAll(): array return $result->fetchAllAssociative(); } + + /** + * Anonymize customer activity logs by customer ID. + * + * @param CustomerId $customerIdToAnonymize + * @param CustomerId $anonymousCustomerId + * @param string $anonymousCustomerName + * + * @return bool + */ + public function anonymizeLogsByCustomerId( + CustomerId $customerIdToAnonymize + ): bool + { + $queryBuilder = $this->getEntityManager()->getConnection()->createQueryBuilder(); + $queryBuilder + ->update(_DB_PREFIX_ . 'psgdpr_log', 'l') + ->set('l.id_guest', '0') + ->set('l.client_name', $queryBuilder->expr()->literal('Anonymous')) + ->where('l.id_customer = :customerId') + ->setParameter('customerId', $customerIdToAnonymize->getValue()) + ; + + $queryBuilder->execute(); + + return true; + } } diff --git a/src/Service/BackResponder/Strategy/BackResponderByCustomerId.php b/src/Service/BackResponder/Strategy/BackResponderByCustomerId.php index 2db7b198..5923cdef 100644 --- a/src/Service/BackResponder/Strategy/BackResponderByCustomerId.php +++ b/src/Service/BackResponder/Strategy/BackResponderByCustomerId.php @@ -64,8 +64,10 @@ public function delete(string $data): Response $this->customerService->deleteCustomerDataFromPrestashop($customerId); $this->customerService->deleteCustomerDataFromModules(strval($customerId->getValue())); + $this->customerService->deleteCustomerDataFromGDPRModule($customerId); - $this->loggerService->createLog($customerId->getValue(), LoggerService::REQUEST_TYPE_DELETE, 0, 0, $customerData); + // Store customer ID as 0 to avoid connecting previously anonymized records with customer's name by ID + $this->loggerService->createLog(0, LoggerService::REQUEST_TYPE_DELETE, 0, 0, $customerData); return new JsonResponse(['message' => 'delete completed']); } diff --git a/src/Service/CustomerService.php b/src/Service/CustomerService.php index c3288ecf..fac0a9dd 100644 --- a/src/Service/CustomerService.php +++ b/src/Service/CustomerService.php @@ -27,6 +27,7 @@ use PrestaShop\Module\Psgdpr\Repository\CartRepository; use PrestaShop\Module\Psgdpr\Repository\CartRuleRepository; use PrestaShop\Module\Psgdpr\Repository\CustomerRepository; +use PrestaShop\Module\Psgdpr\Repository\LoggerRepository; use PrestaShop\PrestaShop\Core\CommandBus\CommandBusInterface; use PrestaShop\PrestaShop\Core\Crypto\Hashing; use PrestaShop\PrestaShop\Core\Domain\Address\Command\AddCustomerAddressCommand; @@ -90,6 +91,11 @@ class CustomerService */ private $hashing; + /** + * @var LoggerRepository + */ + private $loggerRepository; + /** * CustomerService constructor. * @@ -102,6 +108,7 @@ class CustomerService * @param CommandBusInterface $queryBus * @param DefaultGroupsProviderInterface $defaultGroupProvider * @param Hashing $hashing + * @param LoggerRepository $loggerRepository * * @return void */ @@ -114,7 +121,8 @@ public function __construct( CommandBusInterface $commandBus, CommandBusInterface $queryBus, DefaultGroupsProviderInterface $defaultGroupProvider, - Hashing $hashing + Hashing $hashing, + LoggerRepository $loggerRepository ) { $this->module = $module; $this->context = $context; @@ -125,6 +133,7 @@ public function __construct( $this->queryBus = $queryBus; $this->defaultGroupProvider = $defaultGroupProvider; $this->hashing = $hashing; + $this->loggerRepository = $loggerRepository; } /** @@ -180,6 +189,22 @@ public function deleteCustomerDataFromModules($data) } } + /** + * Delete customer data from psgdpr module. + * + * @param CustomerId $customerId + * + * @throws DeleteException + */ + public function deleteCustomerDataFromGDPRModule(CustomerId $customerId) + { + try { + $this->loggerRepository->anonymizeLogsByCustomerId($customerId); + } catch (\Exception $e) { + throw new DeleteException($e->getMessage()); + } + } + /** * Find or create an anonymous customer * From dd2a323984151bfc65eebad8fffddd455bb3d577 Mon Sep 17 00:00:00 2001 From: Karlis Suvi <45097959+ks129@users.noreply.github.com> Date: Mon, 23 Sep 2024 13:29:13 +0300 Subject: [PATCH 3/3] Only show non-0 customer IDs in activity log --- views/templates/admin/tabs/customerActivity.tpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/views/templates/admin/tabs/customerActivity.tpl b/views/templates/admin/tabs/customerActivity.tpl index 34969c29..5b53430c 100644 --- a/views/templates/admin/tabs/customerActivity.tpl +++ b/views/templates/admin/tabs/customerActivity.tpl @@ -36,7 +36,7 @@ {foreach from=$logs item=log} - {$log.client_name|escape:'htmlall':'UTF-8'} (ID: {$log.id_customer|escape:'htmlall':'UTF-8'}) + {$log.client_name|escape:'htmlall':'UTF-8'}{if $log.id_customer neq 0} (ID: {$log.id_customer|escape:'htmlall':'UTF-8'}){/if} {if $log.request_type eq 1} {l s='Consent confirmation' d='Modules.Psgdpr.Admin'} {/if}