Skip to content

Commit

Permalink
Handle not cached IPs
Browse files Browse the repository at this point in the history
  • Loading branch information
mchev committed Feb 21, 2023
1 parent b99044d commit b9e48ac
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/IP.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class IP
{
public static function ban(string|array $ips): void
{
$bannedIps = Cache::get('banned-ips');
$bannedIps = self::getBannedIPsFromCache();

foreach ((array) $ips as $ip) {
if (! in_array($ip, $bannedIps)) {
Expand Down Expand Up @@ -43,4 +43,11 @@ public static function banned(): Builder
->notExpired()
->groupBy('ip');
}

public function getBannedIPsFromCache(): Array
{
return Cache::has('banned-ips')
? Cache::get('banned-ips')
: self::self::banned()->pluck('ip')->toArray();
}
}
2 changes: 1 addition & 1 deletion src/Middleware/IPBanned.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class IPBanned
{
public function handle($request, Closure $next): Response
{
if ($request->ip() && in_array($request->ip(), Cache::get('banned-ips'))) {
if ($request->ip() && in_array($request->ip(), IP::getBannedIPsFromCache())) {
return (config('ban.fallback_url'))
? redirect(config('ban.fallback_url'))
: abort(403, config('ban.message'));
Expand Down
2 changes: 1 addition & 1 deletion src/Middleware/LogoutBanned.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class LogoutBanned
public function handle($request, Closure $next): Response
{
if ($request->user() && $request->user()->isBanned()
|| $request->ip() && in_array($request->ip(), Cache::get('banned-ips'))) {
|| $request->ip() && in_array($request->ip(), IP::getBannedIPsFromCache())) {
if ($request->user()) {
auth()->logout();
$request->session()->invalidate();
Expand Down

0 comments on commit b9e48ac

Please sign in to comment.