From 9833b1486695b13337eab3254327ed2dedc0e2e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Thu, 12 Aug 2021 08:56:35 +0200 Subject: [PATCH] Add AuthorizationException handling --- app/Enums/ApiError.php | 9 +++++++++ app/Exceptions/Handler.php | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/app/Enums/ApiError.php b/app/Enums/ApiError.php index 85e2e91..2b20851 100644 --- a/app/Enums/ApiError.php +++ b/app/Enums/ApiError.php @@ -51,6 +51,13 @@ use Kwaadpepper\Enum\BaseEnum; * Too many requests have been attempted. The is a throttle security. * Normally you should not trigger this. * If this happens fill a ticket on https://supportweb.ldc.fr + * with some technical details + * + * @method static self authorizationException() + * You are maybe trying to access data that is out of your range. + * This should not happen normally, it is here to allow you to handle this case. + * If this happens fill a ticket on https://supportweb.ldc.fr + * with some technical details * * @package LDCCrmClient * @subpackage enums @@ -71,6 +78,7 @@ class ApiError extends BaseEnum 'singleOptinCustomerExists' => 120, 'methodNotAllowed' => 150, 'wrongTimeStamp' => 151, + 'authorizationException' => 403, 'throttleRequestsException' => 429 ]; } @@ -88,6 +96,7 @@ class ApiError extends BaseEnum 'needAuth' => trans('You need to be authenticated'), 'methodNotAllowed' => trans('This method is not allowed for this request'), 'wrongTimeStamp' => trans('Your request is malformed, please check timestamp header and parameter'), + 'authorizationException' => trans('You are not autorized to access this'), 'throttleRequestsException' => trans('Throttle: too many request have been attempted, please retry later') ]; } diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 3b93ee2..203bbed 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -9,6 +9,7 @@ use App\Exceptions\Api\V2\CrmException; use App\Http\Resources\Api\V2\ApiResponse as V2ApiResponse; use App\Lib\Helper; use App\Mail\ExceptionAlert; +use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Auth\AuthenticationException; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; @@ -143,6 +144,14 @@ class Handler extends ExceptionHandler ApiError::throttleRequestsException()->value, ApiError::throttleRequestsException()->label ); + case AuthorizationException::class: + /** @var AuthorizationException $e */ + return $this->getReponse( + $request, + 403, + ApiError::authorizationException()->value, + ApiError::authorizationException()->label + ); case CrmException::class: default: if (config('app.env') === 'testing') {