rajmundtoth0/laravel-auditing-elasticsearch-driver

An Elasticsearch driver for the owen-it/laravel-auditing package.

1.2.1 2024-10-08 05:12 UTC

This package is auto-updated.

Last update: 2024-11-10 17:27:50 UTC


README

Version codecov PHPStan Build Codacy Badge PHP Version Require License Total Downloads

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.