From 5a7d84469c272987f9cbbea8517810402fc48695 Mon Sep 17 00:00:00 2001 From: Eduardo Weiland Date: Thu, 17 Jan 2019 15:28:26 -0200 Subject: [PATCH] Add option to disable audit for a specific entity at runtime (#31) * Add option to disable audit for a specific entity at runtime * Use fluent methods for enabling and disabling audit * Document how to disable auditing in runtime --- README.md | 30 +++++++++++++++ .../AuditConfiguration.php | 37 ++++++++++++++++++- .../DependencyInjection/Configuration.php | 3 ++ 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 53fd944f..9f07e722 100644 --- a/README.md +++ b/README.md @@ -246,6 +246,36 @@ services: class: App\CustomUserProvider ```` +### Disable auditing in runtime + +If you have a situation where you need to disable audit logging for some specific operation (like an automated +process), you can do this by injecting the `dh_doctrine_audit.configuration` service in your class. + +To disable auditing for an entity, use: + +````php +$auditConfiguration->disableAuditFor(MyAuditedEntity1::class); +```` + +To enable auditing afterwards, use: + +````php +$auditConfiguration->enableAuditFor(MyAuditedEntity1::class); +```` + +You can also have an entity that is not audited by default and only enable auditing when you need it. To do so, add +this to your configuration file: + +````yml +dh_doctrine_audit: + entities: + MyBundle\Entity\MyAuditedEntity1: + enabled: false +```` + +This will create the audit table for this entity, but will only save audit entries when explicitly enabled as shown +above. + Usage ===== diff --git a/src/DoctrineAuditBundle/AuditConfiguration.php b/src/DoctrineAuditBundle/AuditConfiguration.php index 0e618929..fbcba578 100644 --- a/src/DoctrineAuditBundle/AuditConfiguration.php +++ b/src/DoctrineAuditBundle/AuditConfiguration.php @@ -64,7 +64,10 @@ public function __construct(array $config, UserProviderInterface $userProvider, public function isAudited($entity): bool { if (!empty($this->entities)) { - foreach (array_keys($this->entities) as $auditedEntity) { + foreach ($this->entities as $auditedEntity => $entityOptions) { + if (isset($entityOptions['enabled']) && !$entityOptions['enabled']) { + continue; + } if (\is_object($entity) && $entity instanceof $auditedEntity) { return true; } @@ -137,6 +140,38 @@ public function getEntities(): array return $this->entities; } + /** + * Enables auditing for a specific entity. + * + * @param string $entity Entity class name + * + * @return $this + */ + public function enableAuditFor(string $entity): self + { + if (isset($this->entities[$entity])) { + $this->entities[$entity]['enabled'] = true; + } + + return $this; + } + + /** + * Disables auditing for a specific entity. + * + * @param string $entity Entity class name + * + * @return $this + */ + public function disableAuditFor(string $entity): self + { + if (isset($this->entities[$entity])) { + $this->entities[$entity]['enabled'] = false; + } + + return $this; + } + /** * Get the value of userProvider. * diff --git a/src/DoctrineAuditBundle/DependencyInjection/Configuration.php b/src/DoctrineAuditBundle/DependencyInjection/Configuration.php index 091ec965..b2d326f3 100644 --- a/src/DoctrineAuditBundle/DependencyInjection/Configuration.php +++ b/src/DoctrineAuditBundle/DependencyInjection/Configuration.php @@ -38,6 +38,9 @@ public function getConfigTreeBuilder() ->canBeUnset() ->prototype('scalar')->end() ->end() + ->booleanNode('enabled') + ->defaultTrue() + ->end() ->end() ->end() ->end()