Skip to content

Commit

Permalink
Merge pull request DATA-DOG#10 from andreasferber/master
Browse files Browse the repository at this point in the history
Option to disable audit for some entities, custom labeler fix/extension
  • Loading branch information
l3pp4rd authored Jun 10, 2016
2 parents e9cf889 + 53e10f3 commit 003873a
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 1 deletion.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ symfony application.
**audit** entities will be mapped automatically if you run schema update or similar.
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
key in your `config.yml`, for example:

data_dog_audit:
unaudited_entities:
- AppBundle\Entity\NoAuditForThis

## Screenshots

All paginated audit log:
Expand Down
36 changes: 36 additions & 0 deletions src/DataDog/AuditBundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace DataDog\AuditBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
* Configuration for DataDog/AuditBundle
*/
class Configuration implements ConfigurationInterface
{

/**
* {@inheritDoc}
* @see \Symfony\Component\Config\Definition\ConfigurationInterface::getConfigTreeBuilder()
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('data_dog_audit');

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

return $treeBuilder;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ class DataDogAuditExtension extends Extension
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$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->addMethodCall('addUnauditedEntities', array($config['unaudited_entities']));
}
}
}
46 changes: 45 additions & 1 deletion src/DataDog/AuditBundle/EventSubscriber/AuditSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class AuditSubscriber implements EventSubscriber
*/
protected $securityTokenStorage;

private $unauditedEntities = [];

private $inserted = []; // [$source, $changeset]
private $updated = []; // [$source, $changeset]
private $removed = []; // [$source, $id]
Expand All @@ -56,6 +58,24 @@ public function getLabeler()
return $this->labeler;
}

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

public function getUnauditedEntities()
{
return array_keys($this->unauditedEntities);
}

private function isEntityUnaudited($entity)
{
return isset($this->unauditedEntities[get_class($entity)]);
}

public function onFlush(OnFlushEventArgs $args)
{
$em = $args->getEntityManager();
Expand All @@ -73,33 +93,57 @@ public function onFlush(OnFlushEventArgs $args)
$em->getConnection()->getConfiguration()->setSQLLogger($new);

foreach ($uow->getScheduledEntityUpdates() as $entity) {
if ($this->isEntityUnaudited($entity)) {
continue;
}
$this->updated[] = [$entity, $uow->getEntityChangeSet($entity)];
}
foreach ($uow->getScheduledEntityInsertions() as $entity) {
if ($this->isEntityUnaudited($entity)) {
continue;
}
$this->inserted[] = [$entity, $ch = $uow->getEntityChangeSet($entity)];
}
foreach ($uow->getScheduledEntityDeletions() as $entity) {
if ($this->isEntityUnaudited($entity)) {
continue;
}
$uow->initializeObject($entity);
$this->removed[] = [$entity, $this->id($em, $entity)];
}
foreach ($uow->getScheduledCollectionUpdates() as $collection) {
if ($this->isEntityUnaudited($collection->getOwner())) {
continue;
}
$mapping = $collection->getMapping();
if (!$mapping['isOwningSide'] || $mapping['type'] !== ClassMetadataInfo::MANY_TO_MANY) {
continue; // ignore inverse side or one to many relations
}
foreach ($collection->getInsertDiff() as $entity) {
if ($this->isEntityUnaudited($entity)) {
continue;
}
$this->associated[] = [$collection->getOwner(), $entity, $mapping];
}
foreach ($collection->getDeleteDiff() as $entity) {
if ($this->isEntityUnaudited($entity)) {
continue;
}
$this->dissociated[] = [$collection->getOwner(), $entity, $this->id($em, $entity), $mapping];
}
}
foreach ($uow->getScheduledCollectionDeletions() as $collection) {
if ($this->isEntityUnaudited($collection->getOwner())) {
continue;
}
$mapping = $collection->getMapping();
if (!$mapping['isOwningSide'] || $mapping['type'] !== ClassMetadataInfo::MANY_TO_MANY) {
continue; // ignore inverse side or one to many relations
}
foreach ($collection->toArray() as $entity) {
if ($this->isEntityUnaudited($entity)) {
continue;
}
$this->dissociated[] = [$collection->getOwner(), $entity, $this->id($em, $entity), $mapping];
}
}
Expand Down Expand Up @@ -335,7 +379,7 @@ private function typ($className)
private function label(EntityManager $em, $entity)
{
if (is_callable($this->labeler)) {
return $this->labeler($entity);
return call_user_func($this->labeler, $entity);
}
$meta = $em->getClassMetadata(get_class($entity));
switch (true) {
Expand Down

0 comments on commit 003873a

Please sign in to comment.