Skip to content

Instantly share code, notes, and snippets.

@elton182
Last active April 20, 2017 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elton182/9aead7a8d0a04d596a0de304cf448942 to your computer and use it in GitHub Desktop.
Save elton182/9aead7a8d0a04d596a0de304cf448942 to your computer and use it in GitHub Desktop.
Permission Middleware
<?php
namespace safed\Http\Middleware;
use Artesaos\Defender\Facades\Defender;
use Closure;
class Permission
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$url = $request->path();
$url = explode('/', $url);
$permission = '';
if (isset($url[0])) {
$permission = $url[0];
}
if (isset($url[1])) {
$permission = $permission . '.' . $url[1];
}
else{
$permission = $permission . '.' . 'index';
}
if (Defender::permissionExists($permission) && !Defender::hasPermission($permission)) {
$request->session()->flash('error', 'Você não tem permissão!');
return redirect()->action('HomeController@index');
}
return $next($request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment