Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 55177a8

Browse files
committed
AuditDriver: ElasticSearch
1 parent 7d5dd5d commit 55177a8

File tree

11 files changed

+944
-0
lines changed

11 files changed

+944
-0
lines changed

CONTRIBUTING.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Contributing
2+
3+
Contributions are always welcome, but to keep things organized, keep in mind the following rules.
4+
5+
# Bug Reports
6+
7+
When reporting a bug in the Laravel Auditing Filesystem driver package, use the [issue tracker](https://github.com/iconscout/laravel-auditing-elasticsearch/issues) of the main repository.
8+
9+
# Pull Requests
10+
11+
Fixing a bug, correcting a typo or adding a new feature?
12+
13+
Just remember that all pull requests should be done against the `master` branch.

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
### The MIT License (MIT)
2+
3+
Copyright (c) 2018 Iconscout.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
This driver provides the ability to save your model audits in elasticsearch.
2+
3+
### Installation
4+
5+
This driver requires that you are using `owen-it/laravel-auditing: ^6.0`. Provided this is fulfilled,
6+
you can install the driver like so:
7+
8+
```
9+
composer require iconscout/laravel-auditing-elasticsearch
10+
```
11+
12+
### Setup
13+
14+
You need to add the following config entries in config/audit.php if you need to change the default behaviour of the driver.
15+
The `queue` key of the config file should look like so:
16+
17+
```
18+
...
19+
'queue' => env('AUDIT_QUEUE', true),
20+
...
21+
```
22+
23+
OR
24+
25+
```
26+
...
27+
'queue' => env('AUDIT_QUEUE', [
28+
'queue' => 'default',
29+
'connection' => 'redis'
30+
]),
31+
...
32+
```
33+
34+
The `driver` key of the config file should look like so:
35+
36+
```
37+
...
38+
'driver' => Iconscout\Auditing\Drivers\ElasticSearch::class,
39+
...
40+
```
41+
42+
The `drivers` key of the config file should look like so:
43+
44+
```
45+
...
46+
'drivers' => [
47+
'database' => [
48+
'table' => 'audits',
49+
'connection' => null,
50+
],
51+
'es' => [
52+
'client' => [
53+
'hosts' => [
54+
env('AUDIT_HOST', 'localhost:9200')
55+
]
56+
],
57+
'index' => env('AUDIT_INDEX', 'laravel_auditing'),
58+
'type' => env('AUDIT_TYPE', 'audits')
59+
],
60+
],
61+
...
62+
```
63+
64+
### Usage
65+
66+
You can use the driver in any Auditable model like so:
67+
68+
```
69+
<?php
70+
namespace App\Models;
71+
72+
use Iconscout\Auditing\Drivers\ElasticSearch;
73+
use Illuminate\Database\Eloquent\Model;
74+
use OwenIt\Auditing\Auditable;
75+
use OwenIt\Auditing\Contracts\Auditable as AuditableContract;
76+
77+
class SomeModel extends Model implements AuditableContract
78+
{
79+
use Auditable;
80+
81+
/**
82+
* ElasticSearch Audit Driver
83+
*
84+
* @var Iconscout\Auditing\Drivers\ElasticSearch
85+
*/
86+
protected $auditDriver = ElasticSearch::class;
87+
88+
// ...
89+
}
90+
```
91+
92+
More information on using customer drivers with owen-it/laravel-auditing can be found on their [homepage](http://laravel-auditing.com/docs/6.0/audit-drivers)

composer.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "iconscout/laravel-auditing-elasticsearch",
3+
"description": "A elasticsearch driver for the owen-it/laravel-auditing package. Allows storage of the audits in elasticsearch.",
4+
"keywords": [
5+
"accountability",
6+
"audit",
7+
"auditing",
8+
"changes",
9+
"eloquent",
10+
"history",
11+
"log",
12+
"logging",
13+
"observer",
14+
"laravel",
15+
"lumen",
16+
"record",
17+
"revision",
18+
"tracking",
19+
"elasticsearch",
20+
"es"
21+
],
22+
"type": "package",
23+
"license": "MIT",
24+
"support": {
25+
"issues": "https://github.com/iconscout/laravel-auditing-elasticsearch/issues",
26+
"source": "https://github.com/iconscout/laravel-auditing-elasticsearch"
27+
},
28+
"authors": [
29+
{
30+
"name": "Arpan Rank",
31+
"email": "arpan@iconscout.com"
32+
}
33+
],
34+
"require": {
35+
"php": ">=7.0",
36+
"owen-it/laravel-auditing": "^6.0",
37+
"elasticsearch/elasticsearch": "6.*"
38+
},
39+
"autoload": {
40+
"psr-4": {
41+
"Iconscout\\Auditing\\": "src/"
42+
}
43+
}
44+
}

src/Console/DeleteCommand.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Laravel Auditing package.
5+
*
6+
* @author Arpan Rank <arpan@iconscout.com>
7+
* @copyright 2018
8+
*
9+
* For the full copyright and license information,
10+
* please view the LICENSE.md file that was distributed
11+
* with this source code.
12+
*/
13+
14+
namespace Iconscout\Auditing\Console;
15+
16+
use Illuminate\Console\Command;
17+
18+
use Illuminate\Support\Facades\Config;
19+
use Iconscout\Auditing\Drivers\ElasticSearch;
20+
21+
class DeleteCommand extends Command
22+
{
23+
/**
24+
* The name and signature of the console command.
25+
*
26+
* @var string
27+
*/
28+
protected $signature = 'audit:es-delete';
29+
30+
/**
31+
* The console command description.
32+
*
33+
* @var string
34+
*/
35+
protected $description = "Delete all of the model's records from the index";
36+
37+
/**
38+
* Execute the console command.
39+
*
40+
* @return mixed
41+
*/
42+
public function handle(ElasticSearch $elasticsearch)
43+
{
44+
$index = Config::get('audit.drivers.es.index', 'laravel_auditing');
45+
46+
if ($elasticsearch->existsIndex()) {
47+
48+
$elasticsearch->deleteIndex();
49+
$this->info("The {$index} index was deleted!");
50+
51+
} else {
52+
$this->info("The {$index} index doesn't exist!");
53+
}
54+
}
55+
}

src/Console/IndexCommand.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Laravel Auditing package.
5+
*
6+
* @author Arpan Rank <arpan@iconscout.com>
7+
* @copyright 2018
8+
*
9+
* For the full copyright and license information,
10+
* please view the LICENSE.md file that was distributed
11+
* with this source code.
12+
*/
13+
14+
namespace Iconscout\Auditing\Console;
15+
16+
use Illuminate\Console\Command;
17+
18+
use Illuminate\Support\Facades\Config;
19+
use Iconscout\Auditing\Drivers\ElasticSearch;
20+
21+
class IndexCommand extends Command
22+
{
23+
/**
24+
* The name and signature of the console command.
25+
*
26+
* @var string
27+
*/
28+
protected $signature = 'audit:es-index';
29+
30+
/**
31+
* The console command description.
32+
*
33+
* @var string
34+
*/
35+
protected $description = "Index all of the model's records into the search index";
36+
37+
/**
38+
* Execute the console command.
39+
*
40+
* @return mixed
41+
*/
42+
public function handle(ElasticSearch $elasticsearch)
43+
{
44+
$index = Config::get('audit.drivers.es.index', 'laravel_auditing');
45+
46+
if ($elasticsearch->existsIndex() === false) {
47+
48+
$elasticsearch->createIndex();
49+
$this->info("The {$index} index was created!");
50+
51+
$elasticsearch->updateAliases();
52+
$this->info("The {$index}_write alias for the {$index} index was created!");
53+
54+
$elasticsearch->putMapping();
55+
$this->info("The {$index} mapping was updated!");
56+
57+
} else {
58+
$this->info("The {$index} index already exist!");
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)