Created
January 29, 2017 15:40
-
-
Save tabacitu/acd7929cc9f60d104da1df4c39f0dad2 to your computer and use it in GitHub Desktop.
Laravel Role and Permission Middlewares
This file contains hidden or 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 App\Http\Middleware; | |
use Closure; | |
use Auth; | |
class PermissionMiddleware | |
{ | |
/** | |
* Handle an incoming request. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \Closure $next | |
* @return mixed | |
*/ | |
public function handle($request, Closure $next, $permission) | |
{ | |
if (Auth::guest()) { | |
return redirect('login'); | |
} | |
if (! $request->user()->can($permission)) { | |
abort(403); | |
} | |
return $next($request); | |
} | |
} |
This file contains hidden or 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 App\Http\Middleware; | |
use Closure; | |
use Auth; | |
class RoleMiddleware | |
{ | |
/** | |
* Handle an incoming request. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \Closure $next | |
* @return mixed | |
*/ | |
public function handle($request, Closure $next, $role) | |
{ | |
if (Auth::guest()) { | |
return redirect('login'); | |
} | |
if (! $request->user()->hasRole($role)) { | |
abort(403); | |
} | |
return $next($request); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment