diff --git a/src/Entrust/Middleware/EntrustAbility.php b/src/Entrust/Middleware/EntrustAbility.php index 13f0cf93..2e8556df 100644 --- a/src/Entrust/Middleware/EntrustAbility.php +++ b/src/Entrust/Middleware/EntrustAbility.php @@ -52,7 +52,13 @@ public function handle($request, Closure $next, $roles, $permissions, $validateA } if ($this->auth->guest() || !$request->user()->ability($roles, $permissions, [ 'validate_all' => $validateAll ])) { - abort(403); + + if ($request->ajax() || $request->wantsJson()) { + return response('Unauthorized.', 401); + } else { + abort(403); + } + } return $next($request); diff --git a/src/Entrust/Middleware/EntrustPermission.php b/src/Entrust/Middleware/EntrustPermission.php index 6058f337..69877348 100644 --- a/src/Entrust/Middleware/EntrustPermission.php +++ b/src/Entrust/Middleware/EntrustPermission.php @@ -42,7 +42,13 @@ public function handle($request, Closure $next, $permissions) } if ($this->auth->guest() || !$request->user()->can($permissions)) { - abort(403); + + if ($request->ajax() || $request->wantsJson()) { + return response('Unauthorized.', 401); + } else { + abort(403); + } + } return $next($request); diff --git a/src/Entrust/Middleware/EntrustRole.php b/src/Entrust/Middleware/EntrustRole.php index f872f50d..8f83256b 100644 --- a/src/Entrust/Middleware/EntrustRole.php +++ b/src/Entrust/Middleware/EntrustRole.php @@ -42,7 +42,13 @@ public function handle($request, Closure $next, $roles) } if ($this->auth->guest() || !$request->user()->hasRole($roles)) { - abort(403); + + if ($request->ajax() || $request->wantsJson()) { + return response('Unauthorized.', 401); + } else { + abort(403); + } + } return $next($request);