Skip to content

Commit

Permalink
WIP fix scopable config
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-kl1 committed Aug 3, 2024
1 parent 41784af commit 22d90fd
Show file tree
Hide file tree
Showing 45 changed files with 327 additions and 449 deletions.
23 changes: 9 additions & 14 deletions Console/Command/EraseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@

namespace Opengento\Gdpr\Console\Command;

use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\App\Area;
use Magento\Framework\App\State;
use Magento\Framework\Console\Cli;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Registry;
use Opengento\Gdpr\Api\Data\EraseEntityInterface;
use Opengento\Gdpr\Api\EraseEntityManagementInterface;
use Opengento\Gdpr\Api\EraseEntityRepositoryInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -30,8 +28,6 @@ public function __construct(
private State $appState,
private Registry $registry,
private EraseEntityManagementInterface $eraseManagement,
private EraseEntityRepositoryInterface $eraseEntityRepository,
private SearchCriteriaBuilder $searchCriteriaBuilder,
string $name = 'gdpr:entity:erase'
) {
parent::__construct($name);
Expand Down Expand Up @@ -71,17 +67,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$entityIds = $input->getArgument(self::INPUT_ARGUMENT_ENTITY_ID);
$entityType = $input->getArgument(self::INPUT_ARGUMENT_ENTITY_TYPE);

try {
$this->searchCriteriaBuilder->addFilter(EraseEntityInterface::ENTITY_ID, $entityIds, 'in');
$this->searchCriteriaBuilder->addFilter(EraseEntityInterface::ENTITY_TYPE, $entityType);
$eraseEntityList = $this->eraseEntityRepository->getList($this->searchCriteriaBuilder->create());
foreach ($eraseEntityList->getItems() as $eraseEntity) {
$this->eraseManagement->process($eraseEntity);
$progressBar = new ProgressBar($output, count($entityIds));
$progressBar->start();

$output->writeln(
'<info>Entity\'s (' . $entityType . ') with ID "' . $eraseEntity->getEntityId() . '" has been erased.</info>'
);
try {
foreach ($entityIds as $entityId) {
$this->eraseManagement->process($this->eraseManagement->create($entityId, $entityType));
$progressBar->advance();
}
$progressBar->finish();
$output->writeln('<info>Entities has been erased.</info>');
} catch (LocalizedException $e) {
$output->writeln('<error>' . $e->getMessage() . '</error>');
$returnCode = Cli::RETURN_FAILURE;
Expand Down
38 changes: 15 additions & 23 deletions Console/Command/ExportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,25 @@
namespace Opengento\Gdpr\Console\Command;

use Exception;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\App\Area;
use Magento\Framework\App\State;
use Magento\Framework\Console\Cli;
use Magento\Framework\Exception\LocalizedException;
use Opengento\Gdpr\Api\Data\ExportEntityInterface;
use Opengento\Gdpr\Api\ExportEntityManagementInterface;
use Opengento\Gdpr\Api\ExportEntityRepositoryInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class ExportCommand extends Command
{
private const INPUT_ARGUMENT_ENTITY_ID = 'entity_id';
private const INPUT_ARGUMENT_ENTITY_TYPE = 'entity_type';
private const INPUT_OPTION_FILENAME = 'filename';

public function __construct(
private State $appState,
private ExportEntityManagementInterface $exportEntityManagement,
private ExportEntityRepositoryInterface $exportEntityRepository,
private SearchCriteriaBuilder $searchCriteriaBuilder,
string $name = 'gdpr:entity:export'
) {
parent::__construct($name);
Expand All @@ -53,13 +47,6 @@ protected function configure(): void
self::INPUT_ARGUMENT_ENTITY_ID,
InputArgument::REQUIRED + InputArgument::IS_ARRAY,
'Entity ID'
),
new InputOption(
self::INPUT_OPTION_FILENAME,
'-f',
InputOption::VALUE_OPTIONAL,
'Export file name',
'personal_data'
)
]);
}
Expand All @@ -75,18 +62,23 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$resultCode = Cli::RETURN_SUCCESS;
$entityIds = $input->getArgument(self::INPUT_ARGUMENT_ENTITY_ID);
$entityType = $input->getArgument(self::INPUT_ARGUMENT_ENTITY_TYPE);
$fileName = $input->getOption(self::INPUT_OPTION_FILENAME);

$progressBar = new ProgressBar($output, count($entityIds));
$progressBar->start();

$files = [];

try {
$this->searchCriteriaBuilder->addFilter(ExportEntityInterface::ENTITY_ID, $entityIds, 'in');
$this->searchCriteriaBuilder->addFilter(ExportEntityInterface::ENTITY_TYPE, $entityType);
$exportEntityList = $this->exportEntityRepository->getList($this->searchCriteriaBuilder->create());
foreach ($exportEntityList->getItems() as $exportEntity) {
foreach ($entityIds as $entityId) {
$exportEntity = $this->exportEntityManagement->create($entityId, $entityType);
$this->exportEntityManagement->export($exportEntity);

$output->writeln(
'<info>Entity\'s related data have been exported to: ' . $exportEntity->getFilePath() . '.</info>'
);
$files[] = $exportEntity->getFilePath();
$progressBar->advance();
}
$progressBar->finish();
$output->writeln('<info>Entities data have been exported to:</info>');
foreach ($files as $file) {
$output->writeln($file);
}
} catch (Exception $e) {
$output->writeln('<error>' . $e->getMessage() . '</error>');
Expand Down
2 changes: 1 addition & 1 deletion Controller/Guest/Download.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function __construct(

protected function isAllowed(): bool
{
return parent::isAllowed() && $this->config->isExportEnabled();
return $this->config->isExportEnabled();
}

protected function executeAction(): ResultInterface|ResponseInterface|Redirect
Expand Down
2 changes: 1 addition & 1 deletion Controller/Guest/Erase.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __construct(

protected function isAllowed(): bool
{
return parent::isAllowed() && $this->config->isErasureEnabled();
return $this->config->isErasureEnabled();
}

protected function executeAction(): Redirect
Expand Down
2 changes: 1 addition & 1 deletion Controller/Guest/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(

protected function isAllowed(): bool
{
return parent::isAllowed() && $this->config->isExportEnabled();
return $this->config->isExportEnabled();
}

protected function executeAction(): Redirect
Expand Down
2 changes: 1 addition & 1 deletion Controller/Guest/UndoErase.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __construct(

protected function isAllowed(): bool
{
return parent::isAllowed() && $this->config->isErasureEnabled();
return $this->config->isErasureEnabled();
}

protected function executeAction(): Redirect
Expand Down
2 changes: 1 addition & 1 deletion Controller/Privacy/Download.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function __construct(

protected function isAllowed(): bool
{
return parent::isAllowed() && $this->config->isExportEnabled();
return $this->config->isExportEnabled();
}

protected function executeAction(): ResultInterface|ResponseInterface|Redirect
Expand Down
2 changes: 1 addition & 1 deletion Controller/Privacy/Erase.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct(

protected function isAllowed(): bool
{
return parent::isAllowed() && $this->config->isErasureEnabled();
return $this->config->isErasureEnabled();
}

protected function executeAction(): Page|Redirect
Expand Down
2 changes: 1 addition & 1 deletion Controller/Privacy/ErasePost.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct(

protected function isAllowed(): bool
{
return parent::isAllowed() && $this->config->isErasureEnabled();
return $this->config->isErasureEnabled();
}

protected function executeAction(): Redirect
Expand Down
2 changes: 1 addition & 1 deletion Controller/Privacy/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(

protected function isAllowed(): bool
{
return parent::isAllowed() && $this->config->isExportEnabled();
return $this->config->isExportEnabled();
}

protected function executeAction(): Redirect
Expand Down
2 changes: 1 addition & 1 deletion Controller/Privacy/UndoErase.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __construct(

protected function isAllowed(): bool
{
return parent::isAllowed() && $this->config->isErasureEnabled();
return $this->config->isErasureEnabled();
}

protected function executeAction(): Redirect
Expand Down
25 changes: 10 additions & 15 deletions Cron/EraseEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Opengento\Gdpr\Api\Data\EraseEntitySearchResultsInterface;
use Opengento\Gdpr\Api\EraseEntityManagementInterface;
use Opengento\Gdpr\Api\EraseEntityRepositoryInterface;
use Opengento\Gdpr\Model\Config;
use Psr\Log\LoggerInterface;

/**
Expand All @@ -27,7 +26,6 @@ class EraseEntity
{
public function __construct(
private LoggerInterface $logger,
private Config $config,
private Registry $registry,
private EraseEntityManagementInterface $eraseManagement,
private EraseEntityRepositoryInterface $eraseRepository,
Expand All @@ -37,24 +35,27 @@ public function __construct(

public function execute(): void
{
if ($this->config->isModuleEnabled() && $this->config->isErasureEnabled()) {
$oldValue = $this->registry->registry('isSecureArea');
$this->registry->register('isSecureArea', true, true);
$oldValue = $this->registry->registry('isSecureArea');
$this->registry->register('isSecureArea', true, true);

try {
foreach ($this->retrieveEraseEntityList()->getItems() as $eraseEntity) {
try {
$this->eraseManagement->process($eraseEntity);
} catch (Exception $e) {
$this->logger->error($e->getMessage(), $e->getTrace());
$this->logger->error($e->getMessage(), ['exception' => $e]);
}
}

$this->registry->register('isSecureArea', $oldValue, true);
} catch (LocalizedException $e) {
$this->logger->error($e->getLogMessage(), ['exception' => $e]);
}

$this->registry->register('isSecureArea', $oldValue, true);
}

/**
* @return EraseEntitySearchResultsInterface
* @throws LocalizedException
*/
private function retrieveEraseEntityList(): SearchResultsInterface
{
Expand All @@ -74,12 +75,6 @@ private function retrieveEraseEntityList(): SearchResultsInterface
'in'
);

try {
$eraseCustomerList = $this->eraseRepository->getList($this->criteriaBuilder->create());
} catch (LocalizedException) {
$eraseCustomerList = [];
}

return $eraseCustomerList;
return $this->eraseRepository->getList($this->criteriaBuilder->create());
}
}
47 changes: 12 additions & 35 deletions Cron/EraseEntityScheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@

namespace Opengento\Gdpr\Cron;

use DateTime;
use Exception;
use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;
use Opengento\Gdpr\Model\Config;
use Opengento\Gdpr\Model\Entity\EntityTypeList;
use Opengento\Gdpr\Model\Erase\EraseEntityScheduler as EraseEntitySchedulerService;
Expand All @@ -24,44 +19,26 @@
*/
class EraseEntityScheduler
{
private const CONFIG_PATH_ERASURE_MAX_AGE = 'gdpr/erasure/entity_max_age';

public function __construct(
private LoggerInterface $logger,
private ScopeConfigInterface $scopeConfig,
private Config $config,
private EraseEntitySchedulerService $eraseEntityScheduler,
private FilterBuilder $filterBuilder,
private EntityTypeList $entityTypeList
) {
}
private EntityTypeList $entityTypeList,
private StoreManagerInterface $storeManager
) {}

public function execute(): void
{
if ($this->config->isModuleEnabled() && $this->config->isErasureEnabled()) {
try {
$this->scheduleEntitiesErasure();
} catch (Exception $e) {
$this->logger->error($e->getMessage(), $e->getTrace());
foreach ($this->storeManager->getWebsites() as $website) {
$websiteId = $website->getId();
if ($this->config->isErasureEnabled($websiteId)) {
try {
$this->eraseEntityScheduler->schedule($this->entityTypeList->getEntityTypes(), $website);
} catch (Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
}
}
}
}

/**
* @throws CouldNotSaveException
* @throws LocalizedException
* @throws Exception
*/
private function scheduleEntitiesErasure(): void
{
$this->filterBuilder->setField('created_at');
$this->filterBuilder->setValue(new DateTime('-' . $this->resolveErasureMaxAge() . 'days'));
$this->filterBuilder->setConditionType('lteq');
$this->eraseEntityScheduler->schedule($this->entityTypeList->getEntityTypes(), $this->filterBuilder->create());
}

private function resolveErasureMaxAge(): int
{
return (int)$this->scopeConfig->getValue(self::CONFIG_PATH_ERASURE_MAX_AGE, ScopeInterface::SCOPE_STORE);
}
}
Loading

0 comments on commit 22d90fd

Please sign in to comment.