Skip to content

Instantly share code, notes, and snippets.

@Cannonb4ll
Created July 31, 2020 11:40
Show Gist options
  • Save Cannonb4ll/2478860954b44c98d49855e9b87ffd2e to your computer and use it in GitHub Desktop.
Save Cannonb4ll/2478860954b44c98d49855e9b87ffd2e to your computer and use it in GitHub Desktop.
Demo a Laravel application
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class Demo
{
protected $safeRoutes = [
'login',
'logout',
];
protected $allowedIps = [
//
];
public function handle(Request $request, Closure $next)
{
if ($this->isNotAllowedToDoThis($request)) {
return redirect('/')->with('info', __('This action is unavailable in the demo mode.'));
}
return $next($request);
}
protected function isNotAllowedToDoThis($request): bool
{
return
$this->isInDemo() && // App should be in demo mode
$this->isMethodMatched($request) && // Should match any of these REST request methods
!$this->isRouteWhitelisted($request) && // Should not be included inside the safe routes
!$this->isIpWhitelisted($request); // Current IP should not be whitelisted
}
protected function isInDemo(): bool
{
return config('app.demo');
}
protected function isMethodMatched(Request $request): bool
{
return in_array(strtoupper($request->method()), ['POST', 'PATCH', 'DELETE', 'PUT']);
}
protected function isIpWhitelisted(Request $request): bool
{
return in_array($request->ip(), $this->allowedIps);
}
protected function isRouteWhitelisted(Request $request): bool
{
return in_array($request->path(), $this->safeRoutes);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment