Skip to content

Commit 5480bfa

Browse files
Merge pull request #4 from marivaldojr/master
Dispatch an event when user is blocked
2 parents 53cdeac + fb59463 commit 5480bfa

File tree

2 files changed

+47
-11
lines changed

2 files changed

+47
-11
lines changed

src/Events/UserBlockedEvent.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Potelo\LaravelBlockBots\Events;
4+
5+
use Carbon\Carbon;
6+
use Illuminate\Queue\SerializesModels;
7+
8+
class UserBlockedEvent
9+
{
10+
use SerializesModels;
11+
12+
public $user;
13+
14+
/** @var integer */
15+
public $number_of_hits;
16+
17+
/** @var Carbon */
18+
public $block_date;
19+
20+
/**
21+
* Create a new event instance.
22+
*
23+
* @param $user
24+
* @param integer $number_of_hits
25+
* @param Carbon $block_date
26+
*
27+
* @return void
28+
*/
29+
public function __construct($user, $number_of_hits, $block_date)
30+
{
31+
$this->user = $user;
32+
$this->number_of_hits = $number_of_hits;
33+
$this->block_date = $block_date;
34+
}
35+
}

src/Middleware/BlockBots.php

+12-11
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use Carbon\Carbon;
1212
use Potelo\LaravelBlockBots\CheckIfBotIsReal;
13+
use Potelo\LaravelBlockBots\Events\UserBlockedEvent;
1314

1415

1516
class BlockBots
@@ -71,7 +72,8 @@ public function blocked($request, $dailyLimit)
7172

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

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

7678
$number_of_hits = 1;
7779

@@ -95,23 +97,22 @@ public function blocked($request, $dailyLimit)
9597
return false;
9698
}
9799
else{
98-
if ($log_blocked_requests){
100+
if ($log_blocked_requests){
99101
$key_notified = "block_bot:notified:{$ip}";
100-
if(!Redis::exists($key_notified))
101-
{
102+
if (!Redis::exists($key_notified)) {
102103
$end_of_day_unix_timestamp = Carbon::tomorrow()->startOfDay()->timestamp;
103104
Redis::set($key_notified, 1);
104105
Redis::expireat($key_notified, $end_of_day_unix_timestamp);
105-
\Potelo\LaravelBlockBots\Jobs\ProcessLogWithIpInfo::dispatch($ip, $user_agent, 'BLOCKED', $full_url);
106-
107-
}
106+
if (Auth::guest()) {
107+
\Potelo\LaravelBlockBots\Jobs\ProcessLogWithIpInfo::dispatch($ip, $user_agent, 'BLOCKED', $full_url);
108+
}
108109
}
110+
}
109111

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

0 commit comments

Comments
 (0)