Permission.php 695 B

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