simplethings/EntityAuditBundle


Doctrine2 Extension to Audit Entities through versioning tables.

http://www.simplethings.de

License: LGPL-2.1

Language: PHP

Keywords: audit, doctrine, doctrine-extension, doctrine-orm, revisions, standalone, symfony, symfony-bundle


EntityAudit Extension for Doctrine2

Master 1.0 Branch
Build Status Build Status
documentation documentation

WARNING: Master isn't stable yet and it might not be working! Please use version ^1.0 and this documentation: https://github.com/simplethings/EntityAudit/blob/1.0/README.md

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

Is this library still maintained?

Maybe? - please discuss and support us in the linked issue

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 (Standalone)

Installing the lib/bundle

Simply run assuming you have installed composer.phar or composer binary:

$ composer require simplethings/entity-audit-bundle

For standalone usage you have to pass the EntityManager.

use Doctrine\ORM\EntityManager;
use SimpleThings\EntityAudit\AuditManager;

$config = new \Doctrine\ORM\Configuration();
// $config ...
$conn = array();
$em = EntityManager::create($conn, $config, $evm);

$auditManager = AuditManager::create($em);

Installation (In Symfony2 Application)

Enable the bundle

Enable the bundle in the kernel:

// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        //...
        new SimpleThings\EntityAudit\SimpleThingsEntityAuditBundle(),
        //...
    );
    return $bundles;
}

Configuration

You can configure the audited tables.

app/config/config.yml
simple_things_entity_audit:
    entity_manager: default
    table_prefix: ''
    table_suffix: _audit
    revision_field_name: rev
    revision_type_field_name: revtype
    revision_table_name: revisions
    revision_id_field_type: integer

Creating new tables

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

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

Usage

Define auditable entities

You need add Auditable annotation for the entities which you want to auditable.

use Doctrine\ORM\Mapping as ORM;
use SimpleThings\EntityAudit\Mapping\Annotation as Audit;

/**
 * @ORM\Entity()
 * @Audit\Auditable()
 */
class Page {
 //...
}

You can also ignore fields in an specific entity.

class Page {

    /**
     * @ORM\Column(type="string")
     * @Audit\Ignore()
     */
    private $ignoreMe;

}

Use AuditReader

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

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

$auditReader = $auditManager->createAuditReader();

In Symfony2 the AuditReader is registered as the service "simplethings_entityaudit.reader":

class DefaultController extends Controller
{
    public function indexAction()
    {
        $auditReader = $this->container->get('simplethings_entityaudit.manager')->createAuditReader();
    }
}

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',
    $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',
    $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 Symfony2 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.

app/config/config.yml
simple_things_entity_audit:
    service:
        username_callable: acme.username_callable

In a standalone app or Symfony command you can 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 Symfony2 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)

app/config/routing.yml
simple_things_entity_audit:
    resource: "@SimpleThingsEntityAuditBundle/Resources/config/routing.yml"
    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

Supported DB

  • MySQL / MariaDB
  • PostgesSQL
  • SQLite

We can only really support the databases if we can test them via Travis.

Contributing

Please before commiting, run this command ./vendor/bin/php-cs-fixer fix --verbose to normalize the coding style.

If you already have the fixer locally you can run php-cs-fixer fix ..

Project Statistics

Sourcerank 12
Repository Size 517 KB
Stars 516
Forks 214
Watchers 36
Open issues 61
Dependencies 10
Contributors 53
Tags 22
Created
Last updated
Last pushed

Top Contributors See all

David Badura Benjamin Eberlei Andrew Tch Ben Davies Simon Mönch Tim Nagel Tobias Olry David Mininger Peter Sullivan SENECHAL Rodrigo Prado Quentin Favrie Tom Corrigan Tomáš Votruba Bob van de Vijver Dan Burzynski Vladimir Ali Tauquir Siddiqui Javier Jiménez Álvarez Markus Welter

Packages Referencing this Repo

simplethings/entity-audit-bundle
Audit for Doctrine Entities
Latest release v1.0.9 - Updated - 516 stars

Recent Tags See all

v1.0.9 April 10, 2018
v1.0.8 April 04, 2018
v1.0.7 March 26, 2018
v1.0.6 July 24, 2017
v1.0.5 May 26, 2017
v1.0.4 April 19, 2017
v1.0.3 April 19, 2017
v1.0.2 January 30, 2017
v1.0.1 January 11, 2017
v1.0.0 January 05, 2017
v0.9.2 August 22, 2016
0.9.1 March 03, 2016
0.9.0 January 06, 2016
v0.8 October 16, 2015
v0.7 November 10, 2014

Interesting Forks See all

jtreminio/EntityAudit
Doctrine2 Extension to Audit Entities through versioning tables.
PHP - LGPL-2.1 - Last pushed - 2 stars - 1 forks
artursvonda/EntityAudit
Doctrine2 Extension to Audit Entities through versioning tables.
PHP - LGPL-2.1 - Last pushed - 1 stars
comstar/EntityAudit
Doctrine2 Extension to Audit Entities through versioning tables. Forked from the original simplet...
PHP - LGPL-2.1 - Last pushed - 1 stars
SIGMA-IT/EntityAudit
Doctrine2 Extension to Audit Entities through versioning tables.
PHP - LGPL-2.1 - Updated - 1 stars
owsy/EntityAudit
Doctrine2 Extension to Audit Entities through versioning tables.
PHP - Last pushed - 1 stars

Something wrong with this page? Make a suggestion

Last synced: 2018-09-07 08:57:19 UTC

Login to resync this repository