diff --git a/README.md b/README.md index a0bf65a..031a4d6 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,6 @@ The `drivers` key of the config file should look like so: ] ], 'index' => env('AUDIT_INDEX', 'laravel_auditing'), - 'type' => env('AUDIT_TYPE', 'audits') ], ], ... diff --git a/composer.json b/composer.json index a50bb14..24d9dde 100644 --- a/composer.json +++ b/composer.json @@ -24,24 +24,24 @@ { "packagist.org": false, "type": "vcs", - "url": "https://github.com/rankarpan/laravel-auditing" + "url": "https://github.com/owen-it/laravel-auditing" } ], "license": "MIT", "support": { - "issues": "https://github.com/iconscout/laravel-auditing-elasticsearch/issues", - "source": "https://github.com/iconscout/laravel-auditing-elasticsearch" + "issues": "https://github.com/kirschbaum-development/laravel-auditing-elasticsearch/issues", + "source": "https://github.com/kirschbaum-development/laravel-auditing-elasticsearch" }, "authors": [ { - "name": "Arpan Rank", - "email": "arpan@iconscout.com" + "name": "Kirschbaum Development", + "email": "wilker@kirschbaumdevelopment.com" } ], "require": { - "php": ">=7.0", - "owen-it/laravel-auditing": "dev-master", - "elasticsearch/elasticsearch": "6.*" + "php": "^8.2", + "owen-it/laravel-auditing": "^13.6", + "elasticsearch/elasticsearch": "^8.0" }, "autoload": { "psr-4": { diff --git a/src/Drivers/ElasticSearch.php b/src/Drivers/ElasticSearch.php index c8dfcc6..276a804 100644 --- a/src/Drivers/ElasticSearch.php +++ b/src/Drivers/ElasticSearch.php @@ -14,7 +14,7 @@ namespace Iconscout\Auditing\Drivers; use Carbon\Carbon; -use Elasticsearch\ClientBuilder; +use Elastic\Elasticsearch\ClientBuilder; use Illuminate\Support\Facades\Config; use Iconscout\Auditing\Jobs\AuditIndexQueuedModels; use Iconscout\Auditing\Jobs\AuditDeleteQueuedModels; @@ -36,11 +36,6 @@ class ElasticSearch implements AuditDriver */ protected $index = null; - /** - * @var string - */ - protected $type = null; - /** * ElasticSearch constructor. */ @@ -48,7 +43,6 @@ public function __construct() { $this->client = ClientBuilder::create()->setHosts(Config::get('audit.drivers.es.client.hosts', ['localhost:9200']))->build(); $this->index = Config::get('audit.drivers.es.index', 'laravel_auditing'); - $this->type = Config::get('audit.drivers.es.type', 'audits'); } /** @@ -60,10 +54,11 @@ public function __construct() */ public function audit(Auditable $model): Audit { + $implementation = Config::get('audit.implementation', AuditModel::class); - + $this->storeAudit($model->toAudit()); - + return new $implementation; } @@ -90,15 +85,15 @@ public function storeAudit($model) if (Config::get('audit.queue', false)) { return $this->indexQueueAuditDocument($model); } - + return $this->indexAuditDocument($model); } public function indexQueueAuditDocument($model) { dispatch((new AuditIndexQueuedModels($model)) - ->onQueue($this->syncWithSearchUsingQueue()) - ->onConnection($this->syncWithSearchUsing())); + ->onQueue($this->syncWithSearchUsingQueue()) + ->onConnection($this->syncWithSearchUsing())); return true; } @@ -108,15 +103,15 @@ public function destroyAudit($model) if (Config::get('audit.queue', false)) { return $this->deleteQueueAuditDocument($model); } - + return $this->deleteAuditDocument($model); } public function deleteQueueAuditDocument($model) { dispatch((new AuditDeleteQueuedModels($model)) - ->onQueue($this->syncWithSearchUsingQueue()) - ->onConnection($this->syncWithSearchUsing())); + ->onQueue($this->syncWithSearchUsingQueue()) + ->onConnection($this->syncWithSearchUsing())); return true; } @@ -145,14 +140,14 @@ public function indexAuditDocument($model) { $params = [ 'index' => $this->index, - 'type' => $this->type, - 'id' => Uuid::uuid4(), + 'id' => (string) Uuid::uuid4(), 'body' => $model ]; try { return $this->client->index($params); - } catch (\Exception $e) {} + } catch (\Exception $e) { + } } public function searchAuditDocument($model) @@ -161,7 +156,6 @@ public function searchAuditDocument($model) $params = [ 'index' => $this->index, - 'type' => $this->type, 'size' => 10000 - $skip, 'from' => $skip, 'body' => [ @@ -199,18 +193,16 @@ public function deleteAuditDocument($model) if (count($audits)) { $audit_ids = array_column($audits, '_id'); - + foreach ($audit_ids as $audit_id) { $params['body'][] = [ 'delete' => [ '_index' => $this->index, - '_type' => $this->type, '_id' => $audit_id ] ]; - } - + return (bool) $this->client->bulk($params); } @@ -239,7 +231,7 @@ public function updateAliases() [ 'add' => [ 'index' => $this->index, - 'alias' => $this->index.'_write' + 'alias' => $this->index . '_write' ] ] ] @@ -254,7 +246,7 @@ public function deleteIndex() 'index' => $this->index ]; - return $this->client->indices()->delete($deleteParams); + return $this->client->indices()->delete($deleteParams)->asBool(); } public function existsIndex() @@ -263,74 +255,66 @@ public function existsIndex() 'index' => $this->index ]; - return $this->client->indices()->exists($params); + return $this->client->indices()->exists($params)->asBool(); } public function putMapping() { $params = [ 'index' => $this->index, - 'type' => $this->type, 'body' => [ - $this->type => [ - '_source' => [ - 'enabled' => true + '_source' => [ + 'enabled' => true + ], + 'properties' => [ + 'event' => [ + 'type' => 'keyword' ], - 'properties' => [ - 'event' => [ - 'type' => 'string', - 'index' => 'not_analyzed' - ], - 'auditable_type' => [ - 'type' => 'string', - 'index' => 'not_analyzed' - ], - 'ip_address' => [ - 'type' => 'string', - 'index' => 'not_analyzed' - ], - 'url' => [ - 'type' => 'string', - 'index' => 'not_analyzed' - ], - 'user_agent' => [ - 'type' => 'string', - 'index' => 'not_analyzed' - ], - 'created_at' => [ - 'type' => 'date', - 'format' => 'yyyy-MM-dd HH:mm:ss' - ], - 'new_values' => [ - 'properties' => [ - 'created_at' => [ - 'type' => 'date', - 'format' => 'yyyy-MM-dd HH:mm:ss' - ], - 'updated_at' => [ - 'type' => 'date', - 'format' => 'yyyy-MM-dd HH:mm:ss' - ], - 'deleted_at' => [ - 'type' => 'date', - 'format' => 'yyyy-MM-dd HH:mm:ss' - ] + 'auditable_type' => [ + 'type' => 'keyword' + ], + 'ip_address' => [ + 'type' => 'keyword' + ], + 'url' => [ + 'type' => 'keyword' + ], + 'user_agent' => [ + 'type' => 'keyword' + ], + 'created_at' => [ + 'type' => 'date', + 'format' => 'yyyy-MM-dd HH:mm:ss' + ], + 'new_values' => [ + 'properties' => [ + 'created_at' => [ + 'type' => 'date', + 'format' => 'yyyy-MM-dd HH:mm:ss' + ], + 'updated_at' => [ + 'type' => 'date', + 'format' => 'yyyy-MM-dd HH:mm:ss' + ], + 'deleted_at' => [ + 'type' => 'date', + 'format' => 'yyyy-MM-dd HH:mm:ss' ] - ], - 'old_values' => [ - 'properties' => [ - 'created_at' => [ - 'type' => 'date', - 'format' => 'yyyy-MM-dd HH:mm:ss' - ], - 'updated_at' => [ - 'type' => 'date', - 'format' => 'yyyy-MM-dd HH:mm:ss' - ], - 'deleted_at' => [ - 'type' => 'date', - 'format' => 'yyyy-MM-dd HH:mm:ss' - ] + ] + ], + 'old_values' => [ + 'properties' => [ + 'created_at' => [ + 'type' => 'date', + 'format' => 'yyyy-MM-dd HH:mm:ss' + ], + 'updated_at' => [ + 'type' => 'date', + 'format' => 'yyyy-MM-dd HH:mm:ss' + ], + 'deleted_at' => [ + 'type' => 'date', + 'format' => 'yyyy-MM-dd HH:mm:ss' ] ] ] diff --git a/src/Traits/ElasticSearchAuditable.php b/src/Traits/ElasticSearchAuditable.php index 8dc0c9a..b989677 100644 --- a/src/Traits/ElasticSearchAuditable.php +++ b/src/Traits/ElasticSearchAuditable.php @@ -13,7 +13,7 @@ namespace Iconscout\Auditing\Traits; -use Elasticsearch\ClientBuilder; +use Elastic\Elasticsearch\ClientBuilder; use Illuminate\Support\Facades\Config; use Illuminate\Database\Eloquent\Collection; @@ -23,14 +23,12 @@ public function esAudits($page = 1, $perPage = 10, $sort = 'latest') { $client = ClientBuilder::create()->setHosts(Config::get('audit.drivers.es.client.hosts', ['localhost:9200']))->build(); $index = Config::get('audit.drivers.es.index', 'laravel_auditing'); - $type = Config::get('audit.drivers.es.type', 'audits'); - + $from = ($page - 1) * $perPage; $order = $sort === 'latest' ? 'desc' : 'asc'; $params = [ 'index' => $index, - 'type' => $type, 'size' => $perPage, 'from' => $from, 'body' => [ @@ -83,4 +81,4 @@ public function getEsAuditsAttribute() { return $this->esAudits(); } -} \ No newline at end of file +} diff --git a/src/config/audit.php b/src/config/audit.php index 4691d16..a253e63 100644 --- a/src/config/audit.php +++ b/src/config/audit.php @@ -149,7 +149,6 @@ ] ], 'index' => env('AUDIT_INDEX', 'laravel_auditing'), - 'type' => env('AUDIT_TYPE', 'audits') ] ],