rajmundtoth0 / laravel-auditing-elasticsearch-driver
An Elasticsearch driver for the owen-it/laravel-auditing package.
Installs: 8 508
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 1
Open Issues: 0
Type:package
Requires
- php: >=8.2
- elasticsearch/elasticsearch: ^8.0
- owen-it/laravel-auditing: ^13.0
- php-http/httplug: ^2.4
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.57
- larastan/larastan: *
- nyholm/psr7: ^1.8
- orchestra/testbench: ^8.21|^9.0
- php-http/mock-client: ^1.6
- phpstan/phpstan: ^1.10
- phpstan/phpstan-phpunit: *
- phpunit/phpunit: ^11.0
README
Laravel Auditing Elasticsearch Driver
This is a community elasticsearch driver for Laravel Auditing.
Contents
Requirements
Installation
composer require rajmundtoth0/laravel-auditing-elasticsearch-driver
Config
The driver
key of the config file should look like so:
...
'driver' => rajmundtoth0\AuditDriver\Services\ElasticsearchAuditService::class,
...
The drivers
key of the config file should look like so:
...
'drivers' => [
'database' => [
'table' => 'audits',
'connection' => null,
],
'elastic' => [
'hosts' => [
env('AUDIT_HOST', 'https://0.0.0.0:9200')
],
'userName' => env('ELASTIC_AUDIT_USER', 'elastic'),
'password' => env('ELASTIC_AUDIT_PASSWORD', 'a_very_strong_password'),
'useBasicAuth' => env('AUDIT_BASIC_AUTH', true),
'useCaCert' => env('AUDIT_USE_CERT', true),
'certPath' => env('AUDIT_CERT_PATH', false),
'index' => env('AUDIT_INDEX', 'laravel_auditing'),
'type' => env('AUDIT_TYPE', 'audits'),
],
'queue' => [
'enabled' => env('AUDIT_QUEUE_ENABLED', false),
'connection' => env('AUDIT_QUEUE_CONNECTION', false),
'name' => env('AUDIT_QUEUE_NAME', 'audits'),
],
],
...
Setup
Run the following artisan command after installation to create the Elasticsearch index:
php artisan es-audit-log:setup
Usage
The following structure ensures to store the audits in Elasticsearch:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use OwenIt\Auditing\Auditable;
use OwenIt\Auditing\Contracts\Auditable as AuditableContract;
class SomeModel extends Model implements AuditableContract
{
use Auditable;
// ...
}
And provides the following ways to retrieve the logs related to the given model:
$someModel->audit_log
$someModel->elasticsearchAuditLog()
Located in the ElasticsearchAuditable
trait.
trait ElasticSearchAuditable
{
/**
* @return Collection<int, mixed>
*/
public function elasticsearchAuditLog(int $page = 1, int $pageSize = 10, string $sort = 'desc'): Collection
{
/** @var ElasticsearchAuditService */
$elasticsearchAuditService = resolve(ElasticsearchAuditService::class);
$result = $elasticsearchAuditService->searchAuditDocument(
model: $this,
pageSize: $pageSize,
from: --$page * $pageSize,
sort: $sort,
);
return collect($result->asArray());
}
/**
* @return Collection<int, mixed>
*/
public function getAuditLogAttribute(): Collection
{
return $this->elasticsearchAuditLog();
}
}
Queue: When the queue configuration is set, the driver will dispatch a job to index each document.
Contribution
Pull requests has to be opened against the master
branch.