Permission.php 725 B

12345678910111213141516171819202122232425262728293031
  1. <?php
  2. namespace App\Http\Middleware;
  3. use Closure;
  4. use Illuminate\Http\Request;
  5. use Spatie\Permission\Exceptions\UnauthorizedException;
  6. class Permission
  7. {
  8. /**
  9. * Handle an incoming request.
  10. *
  11. * @param Request $request
  12. * @param Closure $next
  13. * @return mixed
  14. */
  15. public function handle($request, Closure $next, $guard = null)
  16. {
  17. if (app('auth')->guard($guard)->guest()) {
  18. throw UnauthorizedException::notLoggedIn();
  19. }
  20. $route = request()->route()->getName();
  21. if (app('auth')->guard($guard)->user()->can($route)) {
  22. return $next($request);
  23. }
  24. throw UnauthorizedException::forPermissions((array) $route);
  25. }
  26. }