2017 © Pedro Peláez
 

cakephp-plugin audit-log

CakePHP Audit Plugin

image

imsamurai/audit-log

CakePHP Audit Plugin

  • Friday, February 6, 2015
  • by imsamurai
  • Repository
  • 4 Watchers
  • 2 Stars
  • 13 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 72 Forks
  • 5 Open issues
  • 7 Versions
  • 0 % Grown

The README.md

API Documentation

Check out Audit log API Documentation, (*1)

Abstract

Build Status Coverage Status Latest Stable Version Total Downloads Latest Unstable Version License, (*2)

A logging plugin for CakePHP. The included AuditableBehavior creates an audit history for each instance of a model to which it's attached., (*3)

The behavior tracks changes on two levels. It takes a snapshot of the fully hydrated object after a change is complete and it also records each individual change in the case of an update action., (*4)

Features

  • Support for CakePHP 2.0. Thanks, @jasonsnider.
  • Tracks object snapshots as well as individual property changes.
  • Allows each revision record to be attached to a source -- usually a user -- of responsibility for the change.
  • Allows developers to ignore changes to specified properties. Properties named created, updated and modified are ignored by default, but these values can be overwritten.
  • Handles changes to HABTM associations.
  • Fully compatible with the PolymorphicBehavior.
  • Does not require or rely on the existence of explicit models revisions (AuditLog) and deltas (AuditLogDeltas).

Installation

CakePHP >= 2.0

As an Archive

  1. Download and extract the source to app/Plugin/AuditLog.

As a Submodule

  1. $ git submodule add git://github.com/imsamurai/CakePHP-Audit-Log-Plugin.git <path_to>/app/Plugin/AuditLog
  2. $ git submodule init
  3. $ git submodule update

Via composer

  1. add into your composer "require": { "imsamurai/audit-log": "1.1.*", }
  2. composer update

To create tables you can use schema shell. To create tables execute:, (*5)

cd <path_to>/app/
chmod +x ./Console/cake
./Console/cake schema create -p AuditLog

CakePHP 1.3.x

For use with CakePHP 1.3.x, be sure to use code from the 1.3 branch and follow the instructions in that README file (NOT MAINTAINED)., (*6)

Next Steps

  1. Run the install.sql file on your CakePHP application database or use schema. This will create the audits and audit_deltas tables that will store each object's relevant change history.
  2. Add plugin into bootstrap.php, (*7)

    CakePlugin::load('AuditLog', array('bootstrap' => true));
    
  3. Create a currentUser() method, if desired., (*8)

    The AuditableBehavior optionally allows each changeset to be "owned" by a "source" -- typically the user responsible for the change. Since user and authentication models vary widely, the behavior supports a callback method that should return the value to be stored as the source of the change, if any., (*9)

    The currentUser() method must be available to every model that cares to track a source of changes, so I recommend that a copy of CakePHP's app_model.php file be created and the method added there. Keep it DRY, right?, (*10)

    The behavior expects the currentUser() method to return an associative array with an id key. Continuing from the example above, the following code might appear in the AppModel:, (*11)

    public function currentUser() {
      return AuthComponent::user();
    }
    
  4. Attach the behavior to any desired model and configure.

Usage

AuditableBehavior

Applying the AuditableBehavior to a model is essentially the same as applying any other CakePHP behavior. The behavior does offer a few configuration options:, (*12)

`ignore`
An array of property names to be ignored when records are created in the deltas table.
`habtm`
An array of models that have a HABTM relationship with the acting model and whose changes should be monitored with the model. If the HABTM model is auditable in its own right, don't include it here. This option is for related models whose changes are _only_ tracked relative to the acting model.

AuditHelper

  • user(user) render user name with link to user profile (see config AuditLog.User)
  • listBlock(conditions, ajax) render widget with compact table of audit logs depends on conditions and ajax. Second parameter means that widget will be loaded by ajax or request action (default is ajax). This method simply invoke action AuditController::index with list=1 and conditions (also count=<count> if it set).

AuditController

For filter by date you can use all formats (single or range) that constructor of DateTime can understand. For example:, (*13)

  • 2014/11/05 TO 2014/11/06 - will be range from 05.11.2014 00:00:00 to 06.11.2014 00:00:00
  • 2014/11/05 01:02:20 TO 2014/11/06 15:02:21 - from 05.11.2014 01:02:20 to 06.11.2014 15:02:21
  • 05.11.2014 01:02:20 to 06.11.2014 15:02:21 - from 05.11.2014 01:02:20 to 06.11.2014 15:02:21
  • 2014/11/05 - from 05.11.2014 00:00:00 to 05.11.2014 23:59:59
  • 2014/11/05 03:40:21 - date must be equals to 05.11.2014 03:40:21

Hint: you can use bootstrap daterange picker for this field., (*14)

Syntax

# Simple syntax accepting default options
class Task extends AppModel {
    public $actsAs = array( 'AuditLog.Auditable' );

    # 
    # Additional model code.
    #
}

# Syntax with explicit options
class Task extends AppModel {
    public $actsAs = array(
        'AuditLog.Auditable' => array(
            'ignore' => array( 'active', 'name', 'updated' ),
            'habtm'  => array( 'Type', 'Project' )
        )
    );

    # 
    # Additional model code.
    #
}

Limitations

  • The master branch is not backwards compatible with CakePHP <=1.3.x. If you need compatibility with these version please install the code from the 1.3 branch and follow the instructions in that README.

License

This code is licensed under the MIT license., (*15)

Notes

Feel free to submit bug reports or suggest improvements in a ticket or fork this project and improve upon it yourself. Contributions welcome., (*16)

Changelog

1.1.1

  • Add link view all in widget

1.1.0

  • Add to composer and travis.
  • Change source_id to user_id field in db.
  • Handle case of saving AuditDelta for array value in field (actual saving for model is provided by https://github.com/imsamurai/cakephp-serializable-behaviour)
  • Default view for list of audit records with pagination and filter (for nice style you need bootstrap 2)
  • Default view for concrete audit record and it deltas
  • Helper that has ajax/requestAction widget for display short list of audit records and format user email with link to user view in your application
  • Some additional parameters you can see in plugin bootstrap.php

The Versions

06/02 2015

dev-master

9999999-dev https://github.com/imsamurai/CakePHP-Audit-Log-Plugin

CakePHP Audit Plugin

  Sources   Download

MIT

The Requires

 

plugin log cakephp audit

06/02 2015

1.1.42

1.1.42.0 https://github.com/imsamurai/CakePHP-Audit-Log-Plugin

CakePHP Audit Plugin

  Sources   Download

MIT

The Requires

 

plugin log cakephp audit

27/01 2015

114.x-dev

114.9999999.9999999.9999999-dev https://github.com/imsamurai/CakePHP-Audit-Log-Plugin

CakePHP Audit Plugin

  Sources   Download

MIT

The Requires

 

plugin log cakephp audit

27/01 2015

1.1.41

1.1.41.0 https://github.com/imsamurai/CakePHP-Audit-Log-Plugin

CakePHP Audit Plugin

  Sources   Download

MIT

The Requires

 

plugin log cakephp audit

25/01 2015

1.1.4

1.1.4.0 https://github.com/imsamurai/CakePHP-Audit-Log-Plugin

CakePHP Audit Plugin

  Sources   Download

MIT

The Requires

 

plugin log cakephp audit

28/10 2014

1.1.1

1.1.1.0 https://github.com/imsamurai/CakePHP-Audit-Log-Plugin

CakePHP Audit Plugin

  Sources   Download

MIT

The Requires

 

plugin log cakephp audit

27/10 2014

1.1.0

1.1.0.0 https://github.com/imsamurai/CakePHP-Audit-Log-Plugin

CakePHP Audit Plugin

  Sources   Download

MIT

The Requires

 

plugin log cakephp audit