Skip to content

Commit fb59463

Browse files
committed
User id as identifier on redis for logged users
1 parent ed18204 commit fb59463

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/Middleware/BlockBots.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ public function blocked($request, $dailyLimit)
7272

7373
$full_url = substr($request->fullUrl(), strlen($request->getScheme(). "://")); # Get the URL without scheme
7474

75-
$key_access_count = "block_bot:{$ip}";
75+
$key_identifier = Auth::check() ? Auth::id() : $ip;
76+
$key_access_count = "block_bot:{$key_identifier}";
7677

7778
$number_of_hits = 1;
7879

@@ -96,23 +97,22 @@ public function blocked($request, $dailyLimit)
9697
return false;
9798
}
9899
else{
99-
if ($log_blocked_requests) {
100+
if ($log_blocked_requests){
100101
$key_notified = "block_bot:notified:{$ip}";
101-
102102
if (!Redis::exists($key_notified)) {
103103
$end_of_day_unix_timestamp = Carbon::tomorrow()->startOfDay()->timestamp;
104104
Redis::set($key_notified, 1);
105105
Redis::expireat($key_notified, $end_of_day_unix_timestamp);
106-
\Potelo\LaravelBlockBots\Jobs\ProcessLogWithIpInfo::dispatch($ip, $user_agent, 'BLOCKED', $full_url);
106+
if (Auth::guest()) {
107+
\Potelo\LaravelBlockBots\Jobs\ProcessLogWithIpInfo::dispatch($ip, $user_agent, 'BLOCKED', $full_url);
108+
}
107109
}
108110
}
109111

110-
if($fake_mode)
111-
{
112+
if ($fake_mode) {
112113
return false;
113-
}
114-
else{
115-
event(new UserBlockedEvent(Auth::user(), $number_of_hits, Carbon::today()));
114+
} else {
115+
event(new UserBlockedEvent(Auth::user(), $number_of_hits, Carbon::now()));
116116
return true;
117117
}
118118
}

0 commit comments

Comments
 (0)