Last active
April 20, 2017 21:20
-
-
Save elton182/9aead7a8d0a04d596a0de304cf448942 to your computer and use it in GitHub Desktop.
Permission Middleware
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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