dev-master
9999999-dev http://github.com/iamthom/Audit-LogLogging Plugin for CakePHP
MIT
The Requires
- php >=5.3.0
log logging audit auditlog
Logging Plugin for CakePHP
A logging plugin for CakePHP. The included AuditableBehavior
creates an audit history for each instance of a model to which it's attached., (*1)
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., (*2)
created
, updated
and modified
are ignored by default, but these values can be overwritten.PolymorphicBehavior
.AuditLog
) and deltas (AuditLogDeltas
).app/Plugin/AuditLog
.$ git submodule add git://github.com/robwilkerson/CakePHP-Audit-Log-Plugin.git <path_to>/app/Plugin/AuditLog
$ git submodule init
$ git submodule update
To create tables you can use schema shell. To create tables execute:, (*3)
cd <path_to>/app/ chmod +x ./Console/cake ./Console/cake schema create -p AuditLog
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., (*4)
install.sql
file on your CakePHP application database. This will create the audits
and audit_deltas
tables that will store each object's relevant change history.Create a currentUser()
method, if desired., (*5)
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., (*6)
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?, (*7)
Storing the changeset source can be a little tricky if the core Auth
component is being used since user data isn't readily available at the model layer where behaviors lie. One option is to forward that data from the controller. One means of doing this is to include the following code in AppController::beforeFilter()
:, (*8)
if( !empty( $this->data ) && empty( $this->data[$this->Auth->userModel] ) ) { $this->data[$this->Auth->userModel] = $this->currentUser(); }
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
:, (*9)
protected function currentUser() { $user = $this->Auth->user(); return $user[$this->Auth->userModel]; # Return the complete user array }
Attach the behavior to any desired model and configure., (*10)
Applying the AuditableBehavior
to a model is essentially the same as applying any other CakePHP behavior. The behavior does offer a few configuration options:, (*11)
# 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. # }
1.3
branch and follow the instructions in that README. This code is licensed under the MIT license., (*12)
Feel free to submit bug reports or suggest improvements in a ticket or fork this project and improve upon it yourself. Contributions welcome., (*13)
Logging Plugin for CakePHP
MIT
log logging audit auditlog