Skip to content

Commit

Permalink
Merge pull request #24 from krzysztofruszczynski/auditedEntities
Browse files Browse the repository at this point in the history
- possibility to define audited entities
  • Loading branch information
l3pp4rd authored Oct 17, 2017
2 parents e48698a + 8f3f0a4 commit eaaf4e8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,23 @@ And all the database changes will be reflected in the audit log afterwards.
### Unaudited Entities

Sometimes, you might not want to create audit log entries for particular entities.
You can achieve this by listing those entities under the `unaudired_entities` configuuration
You can achieve this by listing those entities under the `unaudited_entities` configuration
key in your `config.yml`, for example:

data_dog_audit:
unaudited_entities:
- AppBundle\Entity\NoAuditForThis

### Specify Audited Entities

Sometimes, it is also possible, that you want to create audit log entries only for particular entities. You can achieve it quite similar to unaudited entities. You can list them under the `audited_entities` configuration key in your `config.yml`, for example:

data_dog_audit:
audited_entities:
- AppBundle\Entity\AuditForThis

You can specify either audited or unaudited entities. If both are specified, only audited entities would be taken into account.

## Screenshots

All paginated audit log:
Expand Down
10 changes: 10 additions & 0 deletions src/DataDog/AuditBundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ public function getConfigTreeBuilder()
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('data_dog_audit');

$rootNode
->children()
->arrayNode('audited_entities')
->canBeUnset()
->performNoDeepMerging()
->prototype('scalar')->end()
->end()
->end()
;

$rootNode
->children()
->arrayNode('unaudited_entities')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ public function load(array $configs, ContainerBuilder $container)
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');

if (isset($config['unaudited_entities'])) {
$auditSubscriber = $container->getDefinition('datadog.event_subscriber.audit');
$auditSubscriber = $container->getDefinition('datadog.event_subscriber.audit');

if (isset($config['audited_entities'])) {
$auditSubscriber->addMethodCall('addAuditedEntities', array($config['audited_entities']));
} else if (isset($config['unaudited_entities'])) {
$auditSubscriber->addMethodCall('addUnauditedEntities', array($config['unaudited_entities']));
}
}
Expand Down
18 changes: 17 additions & 1 deletion src/DataDog/AuditBundle/EventSubscriber/AuditSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class AuditSubscriber implements EventSubscriber
*/
protected $securityTokenStorage;

private $auditedEntities = [];
private $unauditedEntities = [];

private $inserted = []; // [$source, $changeset]
Expand Down Expand Up @@ -61,6 +62,14 @@ public function getLabeler()
return $this->labeler;
}

public function addAuditedEntities(array $auditedEntities)
{
// use entity names as array keys for easier lookup
foreach ($auditedEntities as $auditedEntity) {
$this->auditedEntities[$auditedEntity] = true;
}
}

public function addUnauditedEntities(array $unauditedEntities)
{
// use entity names as array keys for easier lookup
Expand All @@ -76,7 +85,14 @@ public function getUnauditedEntities()

private function isEntityUnaudited($entity)
{
return isset($this->unauditedEntities[get_class($entity)]);
if (!empty($this->auditedEntities)) {
// only selected entities are audited
$isEntityUnaudited = !isset($this->auditedEntities[get_class($entity)]);
} else {
$isEntityUnaudited = isset($this->unauditedEntities[get_class($entity)]);
}

return $isEntityUnaudited;
}

public function onFlush(OnFlushEventArgs $args)
Expand Down

0 comments on commit eaaf4e8

Please sign in to comment.