Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- possibility to define audited entities #24

Merged
merged 2 commits into from
Oct 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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