Skip to content

Commit 794b1dc

Browse files
committed
Add model auditing
1 parent 9025559 commit 794b1dc

File tree

5 files changed

+187
-1
lines changed

5 files changed

+187
-1
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"illuminate/support": "^6.0|^7.0|^8.0",
4242
"laracasts/flash": "^3.2",
4343
"livewire/livewire": "^1.0|^2.0",
44+
"owen-it/laravel-auditing": "^12.0",
4445
"pragmarx/countries": "^0.7.2",
4546
"spatie/laravel-permission": "^5.0",
4647
"venturedrake/laravel-encryptable": "^0.1"

config/audit.php

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
3+
return [
4+
5+
'enabled' => env('AUDITING_ENABLED', true),
6+
7+
/*
8+
|--------------------------------------------------------------------------
9+
| Audit Implementation
10+
|--------------------------------------------------------------------------
11+
|
12+
| Define which Audit model implementation should be used.
13+
|
14+
*/
15+
16+
'implementation' => OwenIt\Auditing\Models\Audit::class,
17+
18+
/*
19+
|--------------------------------------------------------------------------
20+
| User Morph prefix & Guards
21+
|--------------------------------------------------------------------------
22+
|
23+
| Define the morph prefix and authentication guards for the User resolver.
24+
|
25+
*/
26+
27+
'user' => [
28+
'morph_prefix' => 'user',
29+
'guards' => [
30+
'web',
31+
'api',
32+
],
33+
],
34+
35+
/*
36+
|--------------------------------------------------------------------------
37+
| Audit Resolvers
38+
|--------------------------------------------------------------------------
39+
|
40+
| Define the User, IP Address, User Agent and URL resolver implementations.
41+
|
42+
*/
43+
'resolver' => [
44+
'user' => OwenIt\Auditing\Resolvers\UserResolver::class,
45+
'ip_address' => OwenIt\Auditing\Resolvers\IpAddressResolver::class,
46+
'user_agent' => OwenIt\Auditing\Resolvers\UserAgentResolver::class,
47+
'url' => OwenIt\Auditing\Resolvers\UrlResolver::class,
48+
],
49+
50+
/*
51+
|--------------------------------------------------------------------------
52+
| Audit Events
53+
|--------------------------------------------------------------------------
54+
|
55+
| The Eloquent events that trigger an Audit.
56+
|
57+
*/
58+
59+
'events' => [
60+
'created',
61+
'updated',
62+
'deleted',
63+
'restored',
64+
],
65+
66+
/*
67+
|--------------------------------------------------------------------------
68+
| Strict Mode
69+
|--------------------------------------------------------------------------
70+
|
71+
| Enable the strict mode when auditing?
72+
|
73+
*/
74+
75+
'strict' => false,
76+
77+
/*
78+
|--------------------------------------------------------------------------
79+
| Audit Timestamps
80+
|--------------------------------------------------------------------------
81+
|
82+
| Should the created_at, updated_at and deleted_at timestamps be audited?
83+
|
84+
*/
85+
86+
'timestamps' => false,
87+
88+
/*
89+
|--------------------------------------------------------------------------
90+
| Audit Threshold
91+
|--------------------------------------------------------------------------
92+
|
93+
| Specify a threshold for the amount of Audit records a model can have.
94+
| Zero means no limit.
95+
|
96+
*/
97+
98+
'threshold' => 0,
99+
100+
/*
101+
|--------------------------------------------------------------------------
102+
| Audit Driver
103+
|--------------------------------------------------------------------------
104+
|
105+
| The default audit driver used to keep track of changes.
106+
|
107+
*/
108+
109+
'driver' => 'database',
110+
111+
/*
112+
|--------------------------------------------------------------------------
113+
| Audit Driver Configurations
114+
|--------------------------------------------------------------------------
115+
|
116+
| Available audit drivers and respective configurations.
117+
|
118+
*/
119+
120+
'drivers' => [
121+
'database' => [
122+
'table' => 'audits',
123+
'connection' => null,
124+
],
125+
],
126+
127+
/*
128+
|--------------------------------------------------------------------------
129+
| Audit Console
130+
|--------------------------------------------------------------------------
131+
|
132+
| Whether console events should be audited (eg. php artisan db:seed).
133+
|
134+
*/
135+
136+
'console' => true,
137+
];
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateAuditsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('audits', function (Blueprint $table) {
17+
$table->bigIncrements('id');
18+
$table->string('user_type')->nullable();
19+
$table->unsignedBigInteger('user_id')->nullable();
20+
$table->string('event');
21+
$table->morphs('auditable');
22+
$table->text('old_values')->nullable();
23+
$table->text('new_values')->nullable();
24+
$table->text('url')->nullable();
25+
$table->ipAddress('ip_address')->nullable();
26+
$table->string('user_agent', 1023)->nullable();
27+
$table->string('tags')->nullable();
28+
$table->timestamps();
29+
30+
$table->index(['user_id', 'user_type']);
31+
});
32+
}
33+
34+
/**
35+
* Reverse the migrations.
36+
*
37+
* @return void
38+
*/
39+
public function down()
40+
{
41+
Schema::drop('audits');
42+
}
43+
}

src/LaravelCrmServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ function ($perPage = 30, $page = null, $options = []) {
140140
if ($this->app->runningInConsole()) {
141141
$this->publishes([
142142
__DIR__ . '/../config/laravel-crm.php' => config_path('laravel-crm.php'),
143+
__DIR__ . '/../config/audit.php' => config_path('audit.php'),
143144
], 'config');
144145

145146
// Publishing the views.
@@ -178,6 +179,7 @@ function ($perPage = 30, $page = null, $options = []) {
178179
__DIR__ . '/../database/migrations/create_laravel_crm_contacts_table.php.stub' => $this->getMigrationFileName($filesystem, 'create_laravel_crm_contacts_table.php', 17),
179180
__DIR__ . '/../database/migrations/create_laravel_crm_contact_types_table.php.stub' => $this->getMigrationFileName($filesystem, 'create_laravel_crm_contact_types_table.php', 18),
180181
__DIR__ . '/../database/migrations/create_laravel_crm_contact_contact_type_table.php.stub' => $this->getMigrationFileName($filesystem, 'create_laravel_crm_contact_contact_type_table.php', 19),
182+
__DIR__ . '/../database/migrations/create_audits_table.php.stub' => $this->getMigrationFileName($filesystem, 'create_audits_table.php', 20),
181183
], 'migrations');
182184

183185
// Publishing the seeders

src/Models/Model.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
namespace VentureDrake\LaravelCrm\Models;
44

55
use Illuminate\Database\Eloquent\Model as EloquentModel;
6+
use OwenIt\Auditing\Contracts\Auditable;
67

7-
class Model extends EloquentModel
8+
class Model extends EloquentModel implements Auditable
89
{
10+
use \OwenIt\Auditing\Auditable;
11+
912
public function saveQuietly(array $options = [])
1013
{
1114
return static::withoutEvents(function () use ($options) {

0 commit comments

Comments
 (0)