Skip to content

Commit 671533d

Browse files
committed
Update Actionable.php
1 parent c86ff71 commit 671533d

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/Traits/Actionable.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace Binafy\LaravelUserMonitoring\Traits;
44

5+
use Binafy\LaravelUserMonitoring\Contracts\MonitoringCondition;
56
use Binafy\LaravelUserMonitoring\Utills\ActionType;
67
use Binafy\LaravelUserMonitoring\Utills\Detector;
78
use Binafy\LaravelUserMonitoring\Utills\UserUtils;
9+
use Illuminate\Http\Request;
810
use Illuminate\Support\Facades\DB;
911

1012
trait Actionable
@@ -20,6 +22,11 @@ protected static function boot(): void
2022
return;
2123
}
2224

25+
// Custom conditions from config
26+
if (! static::shouldMonitor(request())) {
27+
return;
28+
}
29+
2330
if (config('user-monitoring.action_monitoring.on_store', false)) {
2431
static::created(function (mixed $model) {
2532
static::insertActionMonitoring($model, ActionType::ACTION_STORE);
@@ -94,4 +101,30 @@ private static function getRealIP(): string
94101
? request()->header(config('user-monitoring.real_ip_header'))
95102
: request()->ip();
96103
}
104+
105+
/**
106+
* Determine if monitoring should be performed for the given request and user.
107+
*/
108+
protected static function shouldMonitor(Request $request): bool
109+
{
110+
$config = config('user-monitoring.action_monitoring.conditions', []);
111+
112+
foreach ($config as $condition) {
113+
if (is_callable($condition)) {
114+
if (! $condition($request)) {
115+
return false;
116+
}
117+
} elseif (is_string($condition)) {
118+
$instance = new $condition;
119+
if (! $instance instanceof MonitoringCondition) {
120+
continue;
121+
}
122+
if (! $instance->shouldMonitor($request)) {
123+
return false;
124+
}
125+
}
126+
}
127+
128+
return true;
129+
}
97130
}

0 commit comments

Comments
 (0)