Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Laravel Role and Permission Middlewares
<?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);
}
}
<?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
You can’t perform that action at this time.