1. Go to this page and download the library: Download mapik/audit-log library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
class ArticlesTable extends Table
{
public function initialize(array $config = [])
{
$this->setDisplayField('article_name');
...
$this->addBehavior('AuditLog.AuditLog');
}
}
class ArticlesTable extends Table
{
public function initialize(array $config = [])
{
$this->setDisplayField('article_name');
...
$this->addBehavior('AuditLog.AuditLog', [
'blacklist' => ['created', 'modified', 'another_field_name']
]);
}
}
class ArticlesTable extends Table
{
public function initialize(array $config = [])
{
$this->setDisplayField('article_name');
...
$this->addBehavior('AuditLog.AuditLog', [
'whitelist' => ['title', 'description', 'author_id']
]);
}
}
public function initialize(array $config = [])
{
$this->setDisplayField('article_name');
...
$this->addBehavior('AuditLog.AuditLog', [
'blacklist' => ['customer_id', 'product_id'],
'foreignKeys' => [
'Categories' => 'name', // foreign key Model => human-friendly field name
'ProductStatuses' => 'status',
],
'unsetAssociatedEntityFieldsNotDirtyByFieldName' => [
'associated_table_name' => 'field_name_in_associated_table'
]
]);
}
use AuditLog\Meta\RequestMetadata;
...
class AppController extends Controller
{
public function beforeFilter(Event $event)
{
...
$eventManager = $this->loadModel()->eventManager();
$identity = $this->request->getAttribute('identity');
if ($identity != null) {
$eventManager->on(
new RequestMetadata($this->request, [
'username' => $identity['username'],
'customer_id' => $identity['customer_id'],
])
);
}
}
}
use AuditLog\Meta\RequestMetadata;
use Cake\Event\EventManager;
...
class AppController extends Controller
{
public function beforeFilter(Event $event)
{
...
$identity = $this->request->getAttribute('identity');
if ($identity != null) {
EventManager::instance()->on(
new RequestMetadata($this->request, [
'username' => $identity['username'],
'customer_id' => $identity['customer_id'],
])
);
}
}
}
use AuditLog\Meta\ApplicationMetadata;
use Cake\Event\EventManager;
EventManager::instance()->on(new ApplicationMetadata('my_blog_app', [
'server' => $theServerID,
'extra' => $somExtraInformation,
'moon_phase' => $currentMoonPhase
]));
EventManager::instance()->on('AuditLog.beforeLog', function ($event, array $logs) {
foreach ($logs as $log) {
$log->setMetaInfo($log->getMetaInfo() + ['extra' => 'This is extra data to be stored']);
}
});
namespace App\Model\Audit;
use Cake\Utility\Text;
use SplObjectStorage;
class AuditLog
{
protected $_auditQueue;
protected $_auditTransaction;
public function __construct()
{
$this->_auditQueue = new SplObjectStorage;
$this->_auditTransaction = Text::uuid();
}
public function toSaveOptions()
{
return [
'_auditQueue' => $this->_auditQueue,
'_auditTransaction' => $this->_auditTransaction
];
}
}
use App\Model\Audit\AuditLog;
use Cake\Event\Event;