Skip to content

Commit 1fbcaa1

Browse files
committed
feat(Audit): multiple user type support
1 parent 85246ff commit 1fbcaa1

File tree

6 files changed

+44
-28
lines changed

6 files changed

+44
-28
lines changed

config/audit.php

+8-4
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,18 @@
3030
| User Keys, Model
3131
|--------------------------------------------------------------------------
3232
|
33-
| Define the User primary key, foreign key and Eloquent model.
33+
| Define the User Eloquent model, morph prefix and authentication guards
34+
| to use in the User resolver.
3435
|
3536
*/
3637

3738
'user' => [
38-
'primary_key' => 'id',
39-
'foreign_key' => 'user_id',
40-
'model' => App\User::class,
39+
'model' => App\User::class,
40+
'morph_prefix' => 'user',
41+
'guards' => [
42+
'web',
43+
'api',
44+
],
4145
],
4246

4347
/*

database/migrations/audits.stub

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class CreateAuditsTable extends Migration
2727
{
2828
Schema::create('audits', function (Blueprint $table) {
2929
$table->increments('id');
30-
$table->unsignedInteger('user_id')->nullable();
30+
$table->nullableMorphs('user');
3131
$table->string('event');
3232
$table->morphs('auditable');
3333
$table->text('old_values')->nullable();

src/Audit.php

+6-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
use DateTimeInterface;
1818
use Illuminate\Database\Eloquent\Model;
19-
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2019
use Illuminate\Database\Eloquent\Relations\MorphTo;
2120
use Illuminate\Support\Facades\Config;
2221

@@ -70,20 +69,18 @@ public function auditable(): MorphTo
7069
/**
7170
* {@inheritdoc}
7271
*/
73-
public function user(): BelongsTo
72+
public function user(): MorphTo
7473
{
75-
return $this->belongsTo(
76-
Config::get('audit.user.model'),
77-
Config::get('audit.user.foreign_key', 'user_id'),
78-
Config::get('audit.user.primary_key', 'id')
79-
);
74+
return $this->morphTo();
8075
}
8176

8277
/**
8378
* {@inheritdoc}
8479
*/
8580
public function resolveData(): array
8681
{
82+
$morphPrefix = Config::get('audit.user.morph_prefix', 'user');
83+
8784
// Metadata
8885
$this->data = [
8986
'audit_id' => $this->id,
@@ -94,7 +91,8 @@ public function resolveData(): array
9491
'audit_tags' => $this->tags,
9592
'audit_created_at' => $this->serializeDate($this->created_at),
9693
'audit_updated_at' => $this->serializeDate($this->updated_at),
97-
'user_id' => $this->getAttribute(Config::get('audit.user.foreign_key', 'user_id')),
94+
'user_id' => $this->getAttribute($morphPrefix.'_id'),
95+
'user_type' => $this->getAttribute($morphPrefix.'_type'),
9896
];
9997

10098
if ($this->user) {

src/Auditable.php

+14-11
Original file line numberDiff line numberDiff line change
@@ -278,21 +278,24 @@ public function toAudit(): array
278278
}
279279
}
280280

281-
$userForeignKey = Config::get('audit.user.foreign_key', 'user_id');
281+
$morphPrefix = Config::get('audit.user.morph_prefix', 'user');
282282

283283
$tags = implode(',', $this->generateTags());
284284

285+
$user = $this->resolveUser();
286+
285287
return $this->transformAudit([
286-
'old_values' => $old,
287-
'new_values' => $new,
288-
'event' => $this->auditEvent,
289-
'auditable_id' => $this->getKey(),
290-
'auditable_type' => $this->getMorphClass(),
291-
$userForeignKey => $this->resolveUser(),
292-
'url' => $this->resolveUrl(),
293-
'ip_address' => $this->resolveIpAddress(),
294-
'user_agent' => $this->resolveUserAgent(),
295-
'tags' => empty($tags) ? null : $tags,
288+
'old_values' => $old,
289+
'new_values' => $new,
290+
'event' => $this->auditEvent,
291+
'auditable_id' => $this->getKey(),
292+
'auditable_type' => $this->getMorphClass(),
293+
$morphPrefix.'_id' => $user ? $user->getAuthIdentifier() : null,
294+
$morphPrefix.'_type' => $user ? $user->getMorphClass() : null,
295+
'url' => $this->resolveUrl(),
296+
'ip_address' => $this->resolveIpAddress(),
297+
'user_agent' => $this->resolveUserAgent(),
298+
'tags' => empty($tags) ? null : $tags,
296299
]);
297300
}
298301

src/Contracts/Audit.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
namespace OwenIt\Auditing\Contracts;
1616

17-
use Illuminate\Database\Eloquent\Relations\BelongsTo;
1817
use Illuminate\Database\Eloquent\Relations\MorphTo;
1918

2019
interface Audit
@@ -43,9 +42,9 @@ public function auditable(): MorphTo;
4342
/**
4443
* User responsible for the changes.
4544
*
46-
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
45+
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
4746
*/
48-
public function user(): BelongsTo;
47+
public function user(): MorphTo;
4948

5049
/**
5150
* Audit data resolver.

src/Resolvers/UserResolver.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
namespace OwenIt\Auditing\Resolvers;
1616

1717
use Illuminate\Support\Facades\Auth;
18+
use Illuminate\Support\Facades\Config;
1819

1920
class UserResolver implements \OwenIt\Auditing\Contracts\UserResolver
2021
{
@@ -23,6 +24,17 @@ class UserResolver implements \OwenIt\Auditing\Contracts\UserResolver
2324
*/
2425
public static function resolve()
2526
{
26-
return Auth::check() ? Auth::user()->getAuthIdentifier() : null;
27+
$guards = Config::get('audit.user.guards', [
28+
'web',
29+
'api',
30+
]);
31+
32+
foreach ($guards as $guard) {
33+
if (Auth::guard($guard)->check()) {
34+
return Auth::guard($guard)->user();
35+
}
36+
}
37+
38+
return null;
2739
}
2840
}

0 commit comments

Comments
 (0)