Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show customer ID and anonymize data in the Customer Activity Log #233

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
28 changes: 28 additions & 0 deletions src/Repository/LoggerRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
Expand Down
27 changes: 26 additions & 1 deletion src/Service/CustomerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -90,6 +91,11 @@ class CustomerService
*/
private $hashing;

/**
* @var LoggerRepository
*/
private $loggerRepository;

/**
* CustomerService constructor.
*
Expand All @@ -102,6 +108,7 @@ class CustomerService
* @param CommandBusInterface $queryBus
* @param DefaultGroupsProviderInterface $defaultGroupProvider
* @param Hashing $hashing
* @param LoggerRepository $loggerRepository
*
* @return void
*/
Expand All @@ -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;
Expand All @@ -125,6 +133,7 @@ public function __construct(
$this->queryBus = $queryBus;
$this->defaultGroupProvider = $defaultGroupProvider;
$this->hashing = $hashing;
$this->loggerRepository = $loggerRepository;
}

/**
Expand Down Expand Up @@ -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
*
Expand Down
2 changes: 1 addition & 1 deletion views/templates/admin/tabs/customerActivity.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<tbody>
{foreach from=$logs item=log}
<tr>
<td class="text-center">{$log.client_name|escape:'htmlall':'UTF-8'}</td>
<td class="text-center">{$log.client_name|escape:'htmlall':'UTF-8'}{if $log.id_customer neq 0} (ID: {$log.id_customer|escape:'htmlall':'UTF-8'}){/if}</td>
{if $log.request_type eq 1}
<td class="text-center">{l s='Consent confirmation' d='Modules.Psgdpr.Admin'}</td>
{/if}
Expand Down