Skip to content

Commit

Permalink
Add the ability to filter audit logs by extra indices defined in the …
Browse files Browse the repository at this point in the history
…configuration
  • Loading branch information
WhatsOn authored and Alarich committed May 9, 2022
1 parent 3136391 commit 0ac343b
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/Provider/Doctrine/DoctrineProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function persist(LifecycleEvent $event): void
unset($payload['table'], $payload['entity']);

$fields = array_combine(array_keys($payload), array_map(function ($x) {return ":{$x}"; }, array_keys($payload)));
\assert(is_array($fields)); // helps PHPStan
\assert(\is_array($fields)); // helps PHPStan

$query = sprintf(
'INSERT INTO %s (%s) VALUES (%s)',
Expand Down
13 changes: 11 additions & 2 deletions src/Provider/Doctrine/Persistence/Reader/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use DH\Auditor\Exception\InvalidArgumentException;
use DH\Auditor\Model\Entry;
use DH\Auditor\Provider\ConfigurationInterface;
use DH\Auditor\Provider\Doctrine\Configuration;
use DH\Auditor\Provider\Doctrine\Persistence\Helper\SchemaHelper;
use DH\Auditor\Provider\Doctrine\Persistence\Reader\Filter\DateRangeFilter;
use DH\Auditor\Provider\Doctrine\Persistence\Reader\Filter\FilterInterface;
Expand Down Expand Up @@ -37,14 +39,21 @@ class Query

private string $table;

/**
* @var Configuration
*/
private ConfigurationInterface $configuration;

private int $offset = 0;

private int $limit = 0;

public function __construct(string $table, Connection $connection)
public function __construct(string $table, Connection $connection, ConfigurationInterface $configuration)
{
$this->connection = $connection;
$this->table = $table;
\assert($configuration instanceof Configuration);
$this->configuration = $configuration;

foreach ($this->getSupportedFilters() as $filterType) {
$this->filters[$filterType] = [];
Expand Down Expand Up @@ -149,7 +158,7 @@ public function limit(int $limit, int $offset = 0): self

public function getSupportedFilters(): array
{
return array_keys(SchemaHelper::getAuditTableIndices('fake'));
return array_keys($this->configuration->getAllIndices('fake'));
}

public function getFilters(): array
Expand Down
17 changes: 16 additions & 1 deletion src/Provider/Doctrine/Persistence/Reader/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ public function createQuery(string $entity, array $options = []): Query
/** @var StorageService $storageService */
$storageService = $this->provider->getStorageServiceForEntity($entity);

$query = new Query($this->getEntityAuditTableName($entity), $storageService->getEntityManager()->getConnection());
$query = new Query(
$this->getEntityAuditTableName($entity),
$storageService->getEntityManager()->getConnection(),
$this->provider->getConfiguration()
);
$query
->addOrderBy(Query::CREATED_AT, 'DESC')
->addOrderBy(Query::ID, 'DESC')
Expand Down Expand Up @@ -83,6 +87,12 @@ public function createQuery(string $entity, array $options = []): Query
$query->addFilter(new SimpleFilter(Query::DISCRIMINATOR, $entity));
}

foreach ($this->provider->getConfiguration()->getExtraIndices() as $indexedField => $extraIndexConfig) {
if (null !== $config[$indexedField]) {
$query->addFilter($indexedField, $config[$indexedField]);
}
}

return $query;
}

Expand All @@ -107,6 +117,11 @@ public function configureOptions(OptionsResolver $resolver): void
->setAllowedValues('page', static fn ($value) => null === $value || $value >= 1)
->setAllowedValues('page_size', static fn ($value) => null === $value || $value >= 1)
;

foreach ($this->provider->getConfiguration()->getExtraIndices() as $indexedField => $extraIndexConfig) {
$resolver->setDefault($indexedField, null);
$resolver->setAllowedTypes($indexedField, ['null', 'int', 'string', 'array']);
}
}

/**
Expand Down
27 changes: 13 additions & 14 deletions tests/Provider/Doctrine/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace DH\Auditor\Tests\Provider\Doctrine;

use DH\Auditor\Provider\Doctrine\Auditing\Annotation\Security;
use DH\Auditor\Provider\Doctrine\Persistence\Helper\SchemaHelper;
use DH\Auditor\Tests\Provider\Doctrine\Fixtures\Entity\Annotation\AuditableButUnauditedEntity;
use DH\Auditor\Tests\Provider\Doctrine\Fixtures\Entity\Annotation\AuditedEntity;
use DH\Auditor\Tests\Provider\Doctrine\Fixtures\Entity\Standard\Blog\Comment;
Expand Down Expand Up @@ -129,17 +128,17 @@ public function testGetExtraFields(): void
{
$extraFields = [
'example_int_field' => [
'type' => 'integer',
'type' => 'integer',
'options' => [
'notnull' => true
]
'notnull' => true,
],
],
'example_string_field' => [
'type' => 'string',
'type' => 'string',
'options' => [
'notnull' => false,
'length' => 50
]
'length' => 50,
],
],
];

Expand All @@ -155,8 +154,8 @@ public function testGetExtraIndices(): void
$extraIndices = [
'example_default_index' => null,
'example_configured_index' => [
'type' => 'primary',
'name_prefix' => 'another_prefix'
'type' => 'primary',
'name_prefix' => 'another_prefix',
],
];

Expand All @@ -172,20 +171,20 @@ public function testPrepareExtraIndices(): void
$extraIndicesConfig = [
'example_default_index' => null,
'example_configured_index' => [
'type' => 'primary',
'name_prefix' => 'another_prefix'
'type' => 'primary',
'name_prefix' => 'another_prefix',
],
];
$tableName = 'test_table';

$extraIndicesExpected = [
'example_default_index' => [
'type' => 'index',
'name' => 'example_default_index_' . md5($tableName) . '_idx',
'name' => 'example_default_index_'.md5($tableName).'_idx',
],
'example_configured_index' => [
'type' => 'primary',
'name' => 'another_prefix_' . md5($tableName) . '_idx',
'type' => 'primary',
'name' => 'another_prefix_'.md5($tableName).'_idx',
],
];

Expand Down
Loading

0 comments on commit 0ac343b

Please sign in to comment.