1. Go to this page and download the library: Download lorenzo/audit-stash 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/ */
lorenzo / audit-stash example snippets
'Datasources' => [
'auditlog_elastic' => [
'className' => 'Cake\ElasticSearch\Datasource\Connection',
'driver' => 'Cake\ElasticSearch\Datasource\Connection',
'host' => '127.0.0.1', // server where elasticsearch is running
'port' => 9200
],
...
]
class ArticlesTable extends Table
{
public function initialize(array $config = []): void
{
...
$this->addBehavior('AuditStash.AuditLog');
}
}
class ArticlesTable extends Table
{
public function initialize(array $config = []): void
{
...
$this->addBehavior('AuditStash.AuditLog', [
'blacklist' => ['created', 'modified', 'another_field_name']
]);
}
}
public function initialize(array $config = []): void
{
...
$this->addBehavior('AuditStash.AuditLog', [
'whitelist' => ['title', 'description', 'author_id']
]);
}
public function initialize(array $config = []): void
{
...
$this->addBehavior('AuditStash.AuditLog', [
'sensitive' => ['body']
]);
}
use AuditStash\Meta\RequestMetadata;
...
class AppController extends Controller
{
public function beforeFilter(EventInterface $event)
{
...
$eventManager = $this->fetchTable()->getEventManager();
$eventManager->on(
new RequestMetadata(
request: $this->getRequest(),
user: $this->getRequest()->getAttribute('identity')?->getIdentifier()
)
);
}
}
use AuditStash\Meta\RequestMetadata;
use Cake\Event\EventManager;
...
class AppController extends Controller
{
public function beforeFilter(EventInterface $event)
{
...
EventManager::instance()->on(
new RequestMetadata(
request: $this->getRequest(),
user: $this->getRequest()->getAttribute('identity')?->getIdentifier()
)
);
}
}
use AuditStash\Meta\ApplicationMetadata;
use Cake\Event\EventManager;
EventManager::instance()->on(new ApplicationMetadata('my_blog_app', [
'server' => $theServerID,
'extra' => $somExtraInformation,
'moon_phase' => $currentMoonPhase
]));
EventManager::instance()->on('AuditStash.beforeLog', function (EventInterface $event, array $logs): void {
foreach ($logs as $log) {
$log->setMetaInfo($log->getMetaInfo() + ['extra' => 'This is extra data to be stored']);
}
});