Skip to content

Commit af09dba

Browse files
authored
Merge pull request #51 from binafy/feat/support-multi-guard
[1.x] Feat - Support multi guards
2 parents b8b8a41 + 8766c00 commit af09dba

11 files changed

+142
-26
lines changed

config/user-monitoring.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,14 @@
3232
'table' => 'users',
3333

3434
/*
35-
* The correct guard.
35+
* You can customize which guards are used to authenticate or
36+
* store user data across different parts of the application. Each guard
37+
* will be checked independently, allowing users to be authenticated by
38+
* multiple guards and enabling more flexible user management.
39+
*
40+
* Make sure that each guard is properly configured under the 'guards' section in the auth.php config file.
3641
*/
37-
'guard' => 'web',
42+
'guards' => ['web'],
3843

3944
/*
4045
* If you are using uuid or ulid you can change it for the type of foreign_key.
@@ -100,8 +105,8 @@
100105
'on_read' => true,
101106
'on_restore' => false,
102107
'on_replicate' => false,
103-
104-
/**
108+
109+
/**
105110
* Determines if the application should use reverse proxy headers to fetch the real client IP
106111
* If set to true, it will try to get the IP from the specified header (X-Real-IP or X-Forwarded-For)
107112
* This is useful when using reverse proxies like Nginx or Cloudflare.

database/migrations/2023_07_22_230401_create_visits_monitoring_table.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ public function up(): void
2121
$table->string('platform');
2222
$table->string('device');
2323
$table->string('ip');
24+
$table->string('user_guard')->nullable();
2425
$table->text('page');
26+
2527
$table->timestamps();
2628
});
2729
}

database/migrations/2023_07_23_145723_create_actions_monitoring_table.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ public function up(): void
2424
$table->string('platform');
2525
$table->string('device');
2626
$table->string('ip');
27+
$table->string('user_guard')->nullable();
2728
$table->text('page');
29+
2830
$table->timestamps();
2931
});
3032
}

database/migrations/2023_07_25_132642_create_authentications_monitoring_table.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ public function up(): void
4141
$table->string('platform');
4242
$table->string('device');
4343
$table->string('ip');
44+
$table->string('user_guard')->nullable();
4445
$table->text('page');
46+
4547
$table->timestamps();
4648
});
4749
}

src/Middlewares/VisitMonitoringMiddleware.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Binafy\LaravelUserMonitoring\Middlewares;
44

55
use Binafy\LaravelUserMonitoring\Utills\Detector;
6+
use Binafy\LaravelUserMonitoring\Utills\UserUtils;
67
use Closure;
78
use Illuminate\Http\Request;
89
use Illuminate\Support\Facades\DB;
@@ -22,17 +23,17 @@ public function handle(Request $request, Closure $next): mixed
2223
}
2324

2425
$detector = new Detector();
25-
$guard = config('user-monitoring.user.guard', 'web');
2626
$exceptPages = config('user-monitoring.visit_monitoring.except_pages', []);
2727

2828
if (empty($exceptPages) || !$this->checkIsExceptPages($request->path(), $exceptPages)) {
2929
// Store visit
3030
DB::table(config('user-monitoring.visit_monitoring.table'))->insert([
31-
'user_id' => auth($guard)->id(),
31+
'user_id' => UserUtils::getUserId(),
3232
'browser_name' => $detector->getBrowser(),
3333
'platform' => $detector->getDevice(),
3434
'device' => $detector->getDevice(),
3535
'ip' => $request->ip(),
36+
'user_guard' => UserUtils::getCurrentGuardName(),
3637
'page' => $request->url(),
3738
'created_at' => now(),
3839
'updated_at' => now(),
@@ -45,7 +46,7 @@ public function handle(Request $request, Closure $next): mixed
4546
/**
4647
* Check request page are exists in expect pages.
4748
*/
48-
private function checkIsExceptPages(string $page, array $exceptPages): bool
49+
protected function checkIsExceptPages(string $page, array $exceptPages): bool
4950
{
5051
return collect($exceptPages)->contains($page);
5152
}

src/Models/ActionMonitoring.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Binafy\LaravelUserMonitoring\Models;
44

5+
use Binafy\LaravelUserMonitoring\Utills\ActionType;
56
use Illuminate\Database\Eloquent\Model;
67

78
class ActionMonitoring extends Model
@@ -22,21 +23,27 @@ class ActionMonitoring extends Model
2223

2324
# Methods
2425

26+
/**
27+
* Get the type color by action type.
28+
*/
2529
public function getTypeColor(): string
2630
{
2731
return match ($this->action_type) {
28-
'read' => 'blue',
29-
'store' => 'green',
30-
'update' => 'purple',
31-
'delete' => 'red',
32-
'restore' => 'yellow',
33-
'replicate' => 'pink',
32+
ActionType::ACTION_READ => 'blue',
33+
ActionType::ACTION_STORE => 'green',
34+
ActionType::ACTION_UPDATE => 'purple',
35+
ActionType::ACTION_DELETE => 'red',
36+
ActionType::ACTION_RESTORED => 'yellow',
37+
ActionType::ACTION_REPLICATE => 'pink',
3438
default => 'gray',
3539
};
3640
}
3741

3842
# Relations
3943

44+
/**
45+
* Relation one-to-many, User model.
46+
*/
4047
public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
4148
{
4249
return $this->belongsTo(

src/Providers/LaravelUserMonitoringEventServiceProvider.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Binafy\LaravelUserMonitoring\Providers;
44

55
use Binafy\LaravelUserMonitoring\Utills\Detector;
6+
use Binafy\LaravelUserMonitoring\Utills\UserUtils;
67
use Illuminate\Auth\Events\Login;
78
use Illuminate\Auth\Events\Logout;
89
use Illuminate\Foundation\Support\Providers\EventServiceProvider;
@@ -14,25 +15,24 @@ class LaravelUserMonitoringEventServiceProvider extends EventServiceProvider
1415
public function boot(): void
1516
{
1617
$detector = new Detector();
17-
$guard = config('user-monitoring.user.guard');
1818
$table = config('user-monitoring.authentication_monitoring.table');
1919

2020
// Login Event
2121
if (config('user-monitoring.authentication_monitoring.on_login', false)) {
22-
Event::listen(function (Login $event) use ($detector, $guard, $table) {
22+
Event::listen(function (Login $event) use ($detector, $table) {
2323
DB::table($table)
2424
->insert(
25-
$this->insertData($guard, $detector, 'login'),
25+
$this->insertData($detector, 'login'),
2626
);
2727
});
2828
}
2929

3030
// Logout Event
3131
if (config('user-monitoring.authentication_monitoring.on_logout', false)) {
32-
Event::listen(function (Logout $event) use ($detector, $guard, $table) {
32+
Event::listen(function (Logout $event) use ($detector, $table) {
3333
DB::table($table)
3434
->insert(
35-
$this->insertData($guard, $detector, 'logout'),
35+
$this->insertData($detector, 'logout'),
3636
);
3737
});
3838
}
@@ -41,15 +41,16 @@ public function boot(): void
4141
/**
4242
* Get insert data.
4343
*/
44-
private function insertData(string $guard, Detector $detector, string $actionType): array
44+
private function insertData(Detector $detector, string $actionType): array
4545
{
4646
return [
47-
'user_id' => auth($guard)->id(),
47+
'user_id' => UserUtils::getUserId(),
4848
'action_type' => $actionType,
4949
'browser_name' => $detector->getBrowser(),
5050
'platform' => $detector->getDevice(),
5151
'device' => $detector->getDevice(),
5252
'ip' => request()->ip(),
53+
'user_guard' => UserUtils::getCurrentGuardName(),
5354
'page' => request()->url(),
5455
'created_at' => now(),
5556
'updated_at' => now(),

src/Traits/Actionable.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Binafy\LaravelUserMonitoring\Utills\ActionType;
66
use Binafy\LaravelUserMonitoring\Utills\Detector;
7+
use Binafy\LaravelUserMonitoring\Utills\UserUtils;
78
use Illuminate\Support\Facades\DB;
89

910
trait Actionable
@@ -64,16 +65,16 @@ protected static function boot(): void
6465
private static function insertActionMonitoring(mixed $model, string $actionType): void
6566
{
6667
$detector = new Detector;
67-
$guard = config('user-monitoring.user.guard');
6868

6969
DB::table(config('user-monitoring.action_monitoring.table'))->insert([
70-
'user_id' => auth($guard)->id(),
70+
'user_id' => UserUtils::getUserId(),
7171
'action_type' => $actionType,
7272
'table_name' => $model->getTable(),
7373
'browser_name' => $detector->getBrowser(),
7474
'platform' => $detector->getDevice(),
7575
'device' => $detector->getDevice(),
7676
'ip' => self::getRealIP(),
77+
'user_guard' => UserUtils::getCurrentGuardName(),
7778
'page' => request()->url(),
7879
'created_at' => now(),
7980
'updated_at' => now(),

src/Utills/ActionType.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,13 @@ class ActionType
1010
public const ACTION_READ = 'read';
1111
public const ACTION_RESTORED = 'restore';
1212
public const ACTION_REPLICATE = 'replicate';
13+
14+
public static array $types = [
15+
self::ACTION_STORE,
16+
self::ACTION_UPDATE,
17+
self::ACTION_DELETE,
18+
self::ACTION_READ,
19+
self::ACTION_RESTORED,
20+
self::ACTION_REPLICATE,
21+
];
1322
}

src/Utills/UserUtils.php

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@
33
namespace Binafy\LaravelUserMonitoring\Utills;
44

55
use Illuminate\Database\Schema\Blueprint;
6+
use Illuminate\Support\Facades\Auth;
67

78
class UserUtils
89
{
910
/**
1011
* Return user foreign key by ulid, uuid, id.
11-
*
12-
* @param Blueprint $table
13-
* @return void
1412
*/
15-
public static function userForeignKey(Blueprint $table)
13+
public static function userForeignKey(Blueprint $table): void
1614
{
1715
$type = config('user-monitoring.user.foreign_key_type', 'id');
1816

@@ -33,4 +31,38 @@ public static function userForeignKey(Blueprint $table)
3331
->nullOnDelete();
3432
}
3533
}
34+
35+
/**
36+
* Get the current guard name.
37+
*/
38+
public static function getCurrentGuardName(): ?string
39+
{
40+
$guards = array_keys(config('auth.guards')); // Get all guard names from config
41+
42+
foreach ($guards as $guard) {
43+
if (Auth::guard($guard)->check()) {
44+
return $guard;
45+
}
46+
}
47+
48+
return null;
49+
}
50+
51+
/**
52+
* Get the user id by guards.
53+
*/
54+
public static function getUserId(): ?int
55+
{
56+
$guards = config('user-monitoring.user.guards', ['web']);
57+
58+
foreach ($guards as $guard) {
59+
try {
60+
if (Auth::guard($guard)->check()) {
61+
return Auth::guard($guard)->id();
62+
}
63+
} catch (\Exception $e) {}
64+
}
65+
66+
return null;
67+
}
3668
}

0 commit comments

Comments
 (0)