• Stars
    star
    631
  • Rank 71,222 (Top 2 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 13 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Audit for Doctrine Entities

EntityAuditBundle

This extension for Doctrine 2 is inspired by Hibernate Envers and allows full versioning of entities and their associations.

Latest Stable Version Latest Unstable Version License

Total Downloads Monthly Downloads Daily Downloads

Branch Github Actions Code Coverage
1.x Test Coverage Status
2.x. Test Coverage Status

Support

For general support and questions, please use StackOverflow.

If you think you found a bug or you have a feature idea to propose, feel free to open an issue after looking at the contributing guide.

License

This package is available under the LGPL license.

How does it work?

There are a bunch of different approaches to auditing or versioning of database tables. This extension creates a mirroring table for each audited entitys table that is suffixed with "_audit". Besides all the columns of the audited entity there are two additional fields:

  • rev - Contains the global revision number generated from a "revisions" table.
  • revtype - Contains one of 'INS', 'UPD' or 'DEL' as an information to which type of database operation caused this revision log entry.

The global revision table contains an id, timestamp, username and change comment field.

With this approach it is possible to version an application with its changes to associations at the particular points in time.

This extension hooks into the SchemaTool generation process so that it will automatically create the necessary DDL statements for your audited entities.

Installation

Installing the bundle

Simply run assuming you have composer:

$ composer require sonata-project/entity-audit-bundle

Enable the bundle

Finally, enable the bundle in the kernel:

// config/bundles.php

return [
    //...
    SimpleThings\EntityAudit\SimpleThingsEntityAuditBundle::class => ['all' => true],
    //...
];

Configuration

Load extension "simple_things_entity_audit" and specify the audited entities

# config/packages/entity_audit.yaml

simple_things_entity_audit:
    audited_entities:
        - MyBundle\Entity\MyEntity
        - MyBundle\Entity\MyEntity2

If you need to exclude some entity properties from triggering a revision use:

# config/packages/entity_audit.yaml

simple_things_entity_audit:
    global_ignore_columns:
        - created_at
        - updated_at

In order to work with other connection or entity manager than "default", use these settings:

# config/packages/entity_audit.yaml

simple_things_entity_audit:
    connection: custom
    entity_manager: custom

Creating new tables

Call the command below to see the new tables in the update schema queue.

./bin/console doctrine:schema:update --dump-sql

Installation (Standalone)

For standalone usage you have to pass the entity class names to be audited to the MetadataFactory instance and configure the two event listeners.

use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\Common\EventManager;
use SimpleThings\EntityAudit\AuditConfiguration;
use SimpleThings\EntityAudit\AuditManager;
use SimpleThings\EntityAudit\Tests\ArticleAudit;
use SimpleThings\EntityAudit\Tests\UserAudit;

$auditConfig = new AuditConfiguration();
$auditConfig->setAuditedEntityClasses([ArticleAudit::class, UserAudit::class]);
$auditConfig->setGlobalIgnoreColumns(['created_at', 'updated_at']);

$eventManager = new EventManager();
$auditManager = new AuditManager($auditConfig);
$auditManager->registerEvents($eventManager);

$config = new Configuration();
// $config ...
$connection = [];
$entityManager = EntityManager::create($connection, $config, $eventManager);

Usage

Querying the auditing information is done using a SimpleThings\EntityAudit\AuditReader instance.

use SimpleThings\EntityAudit\AuditReader;

class DefaultController extends Controller
{
    public function indexAction(AuditReader $auditReader)
    {
    }
}

In a standalone application you can create the audit reader from the audit manager:

$auditReader = $auditManager->createAuditReader($entityManager);

Find entity state at a particular revision

This command also returns the state of the entity at the given revision, even if the last change to that entity was made in a revision before the given one:

$articleAudit = $auditReader->find(
    SimpleThings\EntityAudit\Tests\ArticleAudit::class,
    $id = 1,
    $rev = 10
);

Instances created through AuditReader#find() are NOT injected into the EntityManagers UnitOfWork, they need to be merged into the EntityManager if it should be reattached to the persistence context in that old version.

Find Revision History of an audited entity

$revisions = $auditReader->findRevisions(
    SimpleThings\EntityAudit\Tests\ArticleAudit::class,
    $id = 1
);

A revision has the following API:

class Revision
{
    public function getRev();
    public function getTimestamp();
    public function getUsername();
}

Find Changed Entities at a specific revision

$changedEntities = $auditReader->findEntitiesChangedAtRevision(10);

A changed entity has the API:

class ChangedEntity
{
    public function getClassName();
    public function getId();
    public function getRevisionType();
    public function getEntity();
}

Find Current Revision of an audited Entity

$revision = $auditReader->getCurrentRevision(
    'SimpleThings\EntityAudit\Tests\ArticleAudit',
    $id = 3
);

Setting the Current Username

Each revision automatically saves the username that changes it. For this to work, the username must be resolved.

In the Symfony web context the username is resolved from the one in the current security context token.

You can override this with your own behaviour by configuring the username_callable service in the bundle configuration. Your custom service must be a callable and should return a string or null.

# config/packages/entity_audit.yaml

simple_things_entity_audit:
    service:
        username_callable: acme.username_callable

In a standalone app or Symfony command you can set an username callable to a specific value using the AuditConfiguration.

$auditConfig = new \SimpleThings\EntityAudit\AuditConfiguration();
$auditConfig->setUsernameCallable(function () {
	$username = //your customer logic
    return username;
});

Viewing auditing

A default Symfony controller is provided that gives basic viewing capabilities of audited data.

To use the controller, import the routing (don't forget to secure the prefix you set so that only appropriate users can get access)

# config/routes.yaml

simple_things_entity_audit:
    resource: "@SimpleThingsEntityAuditBundle/Resources/config/routing/audit.xml"
    prefix: /audit

This provides you with a few different routes:

  • simple_things_entity_audit_home - Displays a paginated list of revisions, their timestamps and the user who performed the revision
  • simple_things_entity_audit_viewrevision - Displays the classes that were modified in a specific revision
  • simple_things_entity_audit_viewentity - Displays the revisions where the specified entity was modified
  • simple_things_entity_audit_viewentity_detail - Displays the data for the specified entity at the specified revision
  • simple_things_entity_audit_compare - Allows you to compare the changes of an entity between 2 revisions

TODOS

  • Currently only works with auto-increment databases
  • Proper metadata mapping is necessary, allow to disable versioning for fields and associations.
  • It does NOT work with Joined-Table-Inheritance (Single Table Inheritance should work, but not tested)

More Repositories

1

SonataAdminBundle

The missing Symfony Admin Generator
PHP
2,096
star
2

SonataMediaBundle

Symfony SonataMediaBundle
PHP
441
star
3

SonataDoctrineORMAdminBundle

Integrate Doctrine ORM into the SonataAdminBundle
PHP
437
star
4

exporter

Lightweight Exporter library
PHP
432
star
5

SonataBlockBundle

Symfony SonataBlockBundle
PHP
411
star
6

cache

[Deprecated] Cache library
PHP
331
star
7

SonataUserBundle

Symfony SonataUserBundle
PHP
331
star
8

SonataCoreBundle

[deprecated] SonataCoreBundle
PHP
320
star
9

sandbox

[Abandoned] Sonata Project's sandbox
PHP
269
star
10

sonata-doctrine-extensions

Doctrine2 behavioral extensions
PHP
264
star
11

SonataIntlBundle

Symfony SonataIntlBundle
PHP
214
star
12

SonataPageBundle

This bundle provides a Site and Page management through container and block services
PHP
206
star
13

SonataEasyExtendsBundle

[deprecated] Prototype to easily share entities across Bundle and Application
PHP
169
star
14

ecommerce

[Abandoned] E-Commerce solution provided by Sonata
PHP
164
star
15

SonataNewsBundle

[Abandoned] Symfony SonataNewsBundle
PHP
152
star
16

SonataNotificationBundle

[Abandoned] Symfony SonataNotificationBundle
PHP
139
star
17

SonataSeoBundle

Symfony SonataSeoBundle
PHP
130
star
18

form-extensions

Symfony form extensions
PHP
94
star
19

SonataClassificationBundle

Symfony SonataClassificationBundle
PHP
88
star
20

twig-extensions

Sonata twig extensions
PHP
82
star
21

SonataFormatterBundle

Symfony SonataFormatterBundle
PHP
79
star
22

SonataTranslationBundle

SonataTranslationBundle
PHP
76
star
23

SonataCacheBundle

[Abandoned] This bundle provides caching services
PHP
69
star
24

SonataDoctrineMongoDBAdminBundle

Symfony Sonata / Integrate Doctrine MongoDB ODM into the SonataAdminBundle
PHP
65
star
25

dev-kit

Development kit of the Sonata-Project
PHP
42
star
26

SonatajQueryBundle

[deprecated] contains jQuery/UI librairies
PHP
41
star
27

SonataDoctrinePhpcrAdminBundle

[Abandoned] Symfony Sonata / Integrate Doctrine PHPCR into the SonataAdminBundle
PHP
34
star
28

SonataGoutteBundle

[deprecated] GoutteBundle, a thin wrapper around Goutte (a simple PHP Web Scraper)
PHP
32
star
29

SonataTimelineBundle

[Abandoned] Integrates SpyTimelineBundle into Sonata
PHP
24
star
30

SonataAdminSearchBundle

[Abandoned] Implement Search Engine (ElasticSearch) inside Sonata Admin
PHP
19
star
31

SonataBluePrintBundle

[deprecated] Blueprint CSS framework integration into the Symfony2 Framework
CSS
17
star
32

SonataDashboardBundle

[Abandoned] Provides a Dashboard management through container and block services
PHP
17
star
33

SonataPropelAdminBundle

[deprecated] Integrates the Propel ORM into the AdminBundle
PHP
14
star
34

SonataCommentBundle

[Abandoned] Integrate the FOSCommentBundle in the Sonata Project
PHP
11
star
35

SonataArticleBundle

[Abandoned] Advanced article management
PHP
11
star
36

composer-archive-creator

[deprecated] Composer Archive Creator
PHP
10
star
37

sonata-capistrano

[deprecated] Add dedicated symfony2 and sonata commands to capistrano
Ruby
2
star
38

media-orm-pack

[Abandoned]
2
star
39

SonataClassificationMediaBundle

[Abandoned] Symfony SonataClassificationMediaBundle
PHP
2
star
40

SonataMarkItUpBundle

[deprecated] Integrates the javascript markItUp lib into your Symfony2 Project
JavaScript
1
star