Skip to content

Commit

Permalink
- possibility to define audited entities
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztofruszczynski committed Oct 13, 2017
1 parent e48698a commit 030ad25
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
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 030ad25

Please sign in to comment.