Skip to content

Commit

Permalink
Merge pull request mautic#12682 from kuzmany/maintenance-audit-log_m5
Browse files Browse the repository at this point in the history
Add maintenance result to audit log
  • Loading branch information
escopecz authored Feb 8, 2024
2 parents 9db28e3 + c98222e commit 2ed2244
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
44 changes: 38 additions & 6 deletions app/bundles/CoreBundle/Command/CleanupMaintenanceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
use Mautic\CoreBundle\CoreEvents;
use Mautic\CoreBundle\Event\MaintenanceEvent;
use Mautic\CoreBundle\Helper\CoreParametersHelper;
use Mautic\CoreBundle\Helper\IpLookupHelper;
use Mautic\CoreBundle\Helper\PathsHelper;
use Mautic\CoreBundle\Model\AuditLogModel;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -19,18 +21,22 @@
*/
class CleanupMaintenanceCommand extends ModeratedCommand
{
public const NAME = 'mautic:maintenance:cleanup';

public function __construct(
private TranslatorInterface $translator,
private EventDispatcherInterface $dispatcher,
PathsHelper $pathsHelper,
CoreParametersHelper $coreParametersHelper
CoreParametersHelper $coreParametersHelper,
private AuditLogModel $auditLogModel,
private IpLookupHelper $ipLookupHelper
) {
parent::__construct($pathsHelper, $coreParametersHelper);
}

protected function configure()
protected function configure(): void
{
$this->setName('mautic:maintenance:cleanup')
$this->setName(self::NAME)
->setDefinition(
[
new InputOption(
Expand Down Expand Up @@ -67,9 +73,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if (!$this->checkRunStatus($input, $output)) {
return \Symfony\Component\Console\Command\Command::SUCCESS;
}

$daysOld = $input->getOption('days-old');
$dryRun = $input->getOption('dry-run');
$dryRun = (bool) $input->getOption('dry-run');
$noInteraction = $input->getOption('no-interaction');
$gdpr = $input->getOption('gdpr');
if (empty($daysOld) && empty($gdpr)) {
Expand All @@ -78,7 +83,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

if (!empty($gdpr)) {
// to fullfil GDPR, you must delete inactive user data older than 3years
// to fullfil GDPR, you must delete inactive user data older than 3 years
$daysOld = 365 * 3;
}

Expand Down Expand Up @@ -120,11 +125,38 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$output->writeln($query);
}
}
// store to audit log
$this->storeToAuditLog($stats, $dryRun, $input->getOptions());

$this->completeRun();

return \Symfony\Component\Console\Command\Command::SUCCESS;
}

/**
* @param array<int|string> $stats
* @param array<string|bool|int|float|array<int|string>|null> $options
*/
protected function storeToAuditLog(array $stats, bool $dryRun, array $options): void
{
$notEmptyStats = array_filter($stats);
if (!$dryRun && count($notEmptyStats)) {
$log = [
'userName' => 'system',
'userId' => 0,
'bundle' => 'core',
'object' => 'maintenance',
'objectId' => 0,
'action' => 'cleanup',
'details' => [
'options' => array_filter($options),
'stats' => $notEmptyStats,
],
'ipAddress' => $this->ipLookupHelper->getIpAddressFromRequest(),
];
$this->auditLogModel->writeToLog($log);
}
}

protected static $defaultDescription = 'Updates the Mautic application';
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,14 @@ public function testCleanupMaintenanceCommand(): void
$this->testSymfonyCommand('mautic:maintenance:cleanup', ['--days-old' => 180, '--no-interaction' => true]);

$this->assertNull(static::getContainer()->get('mautic.lead.model.lead')->getEntity($contactId));

// get last row sql query from audit_log table
$sql = 'SELECT * FROM '.MAUTIC_TABLE_PREFIX.'audit_log ORDER BY id DESC LIMIT 1';
$stmt = $this->em->getConnection()->prepare($sql);
$result = $stmt->executeQuery()->fetchAssociative();
$this->assertEquals('core', $result['bundle']);
$this->assertEquals('maintenance', $result['object']);
$this->assertEquals('cleanup', $result['action']);
$this->assertEquals('a:2:{s:7:"options";a:4:{s:8:"days-old";i:180;s:9:"lock_mode";s:3:"pid";s:14:"no-interaction";b:1;s:3:"env";s:4:"test";}s:5:"stats";a:1:{s:8:"Visitors";i:1;}}', $result['details']);
}
}

0 comments on commit 2ed2244

Please sign in to comment.