diff --git a/src/Events/UserBlockedEvent.php b/src/Events/UserBlockedEvent.php new file mode 100644 index 0000000..8395c61 --- /dev/null +++ b/src/Events/UserBlockedEvent.php @@ -0,0 +1,35 @@ +user = $user; + $this->number_of_hits = $number_of_hits; + $this->block_date = $block_date; + } +} diff --git a/src/Middleware/BlockBots.php b/src/Middleware/BlockBots.php index 8e30da7..e5325d2 100644 --- a/src/Middleware/BlockBots.php +++ b/src/Middleware/BlockBots.php @@ -10,6 +10,7 @@ use Carbon\Carbon; use Potelo\LaravelBlockBots\CheckIfBotIsReal; +use Potelo\LaravelBlockBots\Events\UserBlockedEvent; class BlockBots @@ -71,7 +72,8 @@ public function blocked($request, $dailyLimit) $full_url = substr($request->fullUrl(), strlen($request->getScheme(). "://")); # Get the URL without scheme - $key_access_count = "block_bot:{$ip}"; + $key_identifier = Auth::check() ? Auth::id() : $ip; + $key_access_count = "block_bot:{$key_identifier}"; $number_of_hits = 1; @@ -95,23 +97,22 @@ public function blocked($request, $dailyLimit) return false; } else{ - if ($log_blocked_requests){ + if ($log_blocked_requests){ $key_notified = "block_bot:notified:{$ip}"; - if(!Redis::exists($key_notified)) - { + if (!Redis::exists($key_notified)) { $end_of_day_unix_timestamp = Carbon::tomorrow()->startOfDay()->timestamp; Redis::set($key_notified, 1); Redis::expireat($key_notified, $end_of_day_unix_timestamp); - \Potelo\LaravelBlockBots\Jobs\ProcessLogWithIpInfo::dispatch($ip, $user_agent, 'BLOCKED', $full_url); - - } + if (Auth::guest()) { + \Potelo\LaravelBlockBots\Jobs\ProcessLogWithIpInfo::dispatch($ip, $user_agent, 'BLOCKED', $full_url); + } } + } - if($fake_mode) - { + if ($fake_mode) { return false; - } - else{ + } else { + event(new UserBlockedEvent(Auth::user(), $number_of_hits, Carbon::now())); return true; } }