Skip to content

Commit

Permalink
Add option to disable audit for a specific entity at runtime (#31)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
eduardoweiland authored and DamienHarper committed Jan 17, 2019
1 parent 1a78a3c commit 5a7d844
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
=====

Expand Down
37 changes: 36 additions & 1 deletion src/DoctrineAuditBundle/AuditConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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.
*
Expand Down
3 changes: 3 additions & 0 deletions src/DoctrineAuditBundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public function getConfigTreeBuilder()
->canBeUnset()
->prototype('scalar')->end()
->end()
->booleanNode('enabled')
->defaultTrue()
->end()
->end()
->end()
->end()
Expand Down

0 comments on commit 5a7d844

Please sign in to comment.