Permission.php 753 B

1234567891011121314151617181920212223242526272829303132
  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. * @param null $guard
  14. * @return mixed
  15. */
  16. public function handle($request, Closure $next, $guard = null)
  17. {
  18. if (app('auth')->guard($guard)->guest()) {
  19. throw UnauthorizedException::notLoggedIn();
  20. }
  21. $route = request()->route()->getName();
  22. if (app('auth')->guard($guard)->user()->can($route)) {
  23. return $next($request);
  24. }
  25. throw UnauthorizedException::forPermissions((array) $route);
  26. }
  27. }