Skip to content

Commit

Permalink
Refactoring conditional statement.
Browse files Browse the repository at this point in the history
Update change log.
  • Loading branch information
ruchit288 committed Jun 19, 2023
1 parent dd49252 commit 3f80d7c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

All notable changes to `laravel-ip-gateway` will be documented in this file.

## 2.1.0 - 2023-06-19

- Code refactoring - remove unnecessary conditions.

## 2.0.0 - 2023-04-12

- Code refactoring and version upgrade.

## 1.0.0 - 2018-01-10

- First release
- First release
8 changes: 2 additions & 6 deletions src/IpGatewayProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@ class IpGatewayProvider extends ServiceProvider
public function boot()
{
$router = $this->app['router'];

if (config('ip-gateway')) {
foreach (config('ip-gateway.middleware') as $middlewareName) {
$router->pushMiddlewareToGroup($middlewareName, IpGatewayMiddleware::class);
}
foreach (config('ip-gateway.middleware') as $middlewareName) {
$router->pushMiddlewareToGroup($middlewareName, IpGatewayMiddleware::class);
}
}

Expand All @@ -50,6 +47,5 @@ public function publishFiles()
foreach ($publishableFiles as $storedPath => $publishPath) {
$this->publishes([$storedPath => $publishPath]);
}

}
}
18 changes: 8 additions & 10 deletions src/Middleware/IpGatewayMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ class IpGatewayMiddleware
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @param $request
* @param Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
Expand All @@ -33,7 +32,7 @@ public function handle($request, Closure $next)
$enableBlacklist = config('ip-gateway.enable_blacklist') === true;
$redirectRoute = config('ip-gateway.redirect_route_to');

if (config('ip-gateway') && config('ip-gateway.enable_package') === true) {
if (config('ip-gateway.enable_package') === true) { // Check if package status is enable.
foreach ($getClientIps as $ip) {
if ($enableBlacklist && $this->grantIpAddress($ip)) { // Its check blacklisted ip-addresses from ip-config file.
$prohibitRequest = true;
Expand All @@ -43,18 +42,18 @@ public function handle($request, Closure $next)
Log::warning($ip . ' IP address has tried to access.');
}
}
}

if ($prohibitRequest) {
return redirect($redirectRoute != '' ? $redirectRoute : '/404');
if ($prohibitRequest) {
return redirect($redirectRoute != '' ? $redirectRoute : '/404');
}
}

return $next($request);

} catch (\Exception $ex) {
Log::error('Problem occurred while handle an incoming request '.$ex->getMessage());
return redirect('/404');
}

}

/**
Expand All @@ -65,7 +64,6 @@ public function handle($request, Closure $next)
*/
protected function grantIpAddress(string $ip) : bool
{
$this->ipList = config('ip-gateway.ip-list');
return in_array($ip, $this->ipList);
return in_array($ip, config('ip-gateway.ip-list'));
}
}

0 comments on commit 3f80d7c

Please sign in to comment.