Skip to content

Commit ed18204

Browse files
committed
dispatch an event when user is blocked
1 parent 2122fbd commit ed18204

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

src/Events/UserBlockedEvent.php

Lines changed: 35 additions & 0 deletions
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

Lines changed: 6 additions & 5 deletions
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
@@ -95,23 +96,23 @@ public function blocked($request, $dailyLimit)
9596
return false;
9697
}
9798
else{
98-
if ($log_blocked_requests){
99+
if ($log_blocked_requests) {
99100
$key_notified = "block_bot:notified:{$ip}";
100-
if(!Redis::exists($key_notified))
101-
{
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);
105106
\Potelo\LaravelBlockBots\Jobs\ProcessLogWithIpInfo::dispatch($ip, $user_agent, 'BLOCKED', $full_url);
106-
107-
}
108107
}
108+
}
109109

110110
if($fake_mode)
111111
{
112112
return false;
113113
}
114114
else{
115+
event(new UserBlockedEvent(Auth::user(), $number_of_hits, Carbon::today()));
115116
return true;
116117
}
117118
}

0 commit comments

Comments
 (0)