From 39944e1ec62e3e6e0277ad7b85c157ce13c94073 Mon Sep 17 00:00:00 2001 From: Nicodav28 Date: Tue, 4 Mar 2025 13:20:10 -0500 Subject: [PATCH 1/4] =?UTF-8?q?=E2=9C=A8=20(laratrust.php,=20LaratrustMidd?= =?UTF-8?q?leware.php):=20Add=20custom=20JSON=20response=20format=20for=20?= =?UTF-8?q?unauthorized=20access=20=F0=9F=94=A7=20(laratrust.php):=20Updat?= =?UTF-8?q?e=20configuration=20to=20include=20new=20'json'=20key=20with=20?= =?UTF-8?q?custom=20response=20structure=20The=20changes=20were=20made=20t?= =?UTF-8?q?o=20provide=20a=20more=20detailed=20and=20structured=20response?= =?UTF-8?q?=20when=20unauthorized=20access=20is=20attempted.=20This=20is?= =?UTF-8?q?=20particularly=20useful=20when=20dealing=20with=20API=20endpoi?= =?UTF-8?q?nts=20where=20a=20JSON=20response=20is=20more=20appropriate=20t?= =?UTF-8?q?han=20a=20redirect=20or=20abort.=20The=20configuration=20file?= =?UTF-8?q?=20was=20updated=20to=20include=20a=20new=20'json'=20key=20that?= =?UTF-8?q?=20defines=20the=20structure=20of=20the=20response,=20including?= =?UTF-8?q?=20a=20custom=20message=20and=20the=20option=20to=20include=20a?= =?UTF-8?q?=20timestamp.=20The=20middleware=20was=20updated=20to=20handle?= =?UTF-8?q?=20this=20new=20configuration=20and=20return=20the=20custom=20J?= =?UTF-8?q?SON=20response=20when=20necessary.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/laratrust.php | 18 ++++++++++++++++++ src/Middleware/LaratrustMiddleware.php | 10 ++++++++++ 2 files changed, 28 insertions(+) diff --git a/config/laratrust.php b/config/laratrust.php index 552a76ae..351ca536 100644 --- a/config/laratrust.php +++ b/config/laratrust.php @@ -226,6 +226,24 @@ 'content' => '', ], ], + + + /** + * Defines a custom JSON response format for unauthorized access. + * This can be used when a JSON response is preferred over a redirect or abort. + * + * - 'code': The HTTP status code to return (default: 403). + * - 'include_timestamp': Whether to include a timestamp in the response (true/false). + * - 'structure': Defines the JSON response format. + */ + 'json' => [ + 'code' => 403, + 'include_timestamp' => true, + 'structure' => [ + 'status' => 'error', + 'message' => 'User does not have the necessary access rights to perform this action.', + ], + ], ], ], diff --git a/src/Middleware/LaratrustMiddleware.php b/src/Middleware/LaratrustMiddleware.php index 3354b5cf..bca82998 100644 --- a/src/Middleware/LaratrustMiddleware.php +++ b/src/Middleware/LaratrustMiddleware.php @@ -49,6 +49,16 @@ protected function unauthorized(): mixed return App::abort($handler['code'], $handler['message'] ?? $defaultMessage); } + if ($handling === "json") { + $responseData = $handler['structure'] ?? []; + + if (!empty($handler['include_timestamp']) && boolval($handler['include_timestamp'])) { + $responseData['timestamp'] = now()->toISOString(); + } + + return response()->json($responseData, $handler['code'] ?? 403); + } + $redirect = Redirect::to($handler['url']); if (! empty($handler['message']['content'])) { $redirect->with($handler['message']['key'], $handler['message']['content']); From fccadf23fe3610cecd1d50e44357b05a1eb7410a Mon Sep 17 00:00:00 2001 From: Nicodav28 Date: Tue, 4 Mar 2025 14:03:08 -0500 Subject: [PATCH 2/4] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20(laratrust.php,=20Lara?= =?UTF-8?q?trustMiddleware.php):=20Refactor=20code=20to=20improve=20readab?= =?UTF-8?q?ility=20and=20consistency=20The=20changes=20were=20made=20to=20?= =?UTF-8?q?improve=20the=20readability=20of=20the=20code=20and=20to=20ensu?= =?UTF-8?q?re=20consistency=20across=20the=20codebase.=20The=20alignment?= =?UTF-8?q?=20of=20the=20array=20elements=20in=20'laratrust.php'=20was=20a?= =?UTF-8?q?djusted=20to=20make=20the=20code=20cleaner=20and=20easier=20to?= =?UTF-8?q?=20read.=20In=20'LaratrustMiddleware.php',=20the=20strict=20equ?= =?UTF-8?q?ality=20operator=20was=20replaced=20with=20a=20loose=20equality?= =?UTF-8?q?=20operator=20to=20allow=20for=20type=20coercion,=20which=20can?= =?UTF-8?q?=20prevent=20potential=20bugs=20in=20the=20future.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/laratrust.php | 8 ++++---- src/Middleware/LaratrustMiddleware.php | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/config/laratrust.php b/config/laratrust.php index 351ca536..dca66c13 100644 --- a/config/laratrust.php +++ b/config/laratrust.php @@ -236,11 +236,11 @@ * - 'include_timestamp': Whether to include a timestamp in the response (true/false). * - 'structure': Defines the JSON response format. */ - 'json' => [ - 'code' => 403, + 'json' => [ + 'code' => 403, 'include_timestamp' => true, - 'structure' => [ - 'status' => 'error', + 'structure' => [ + 'status' => 'error', 'message' => 'User does not have the necessary access rights to perform this action.', ], ], diff --git a/src/Middleware/LaratrustMiddleware.php b/src/Middleware/LaratrustMiddleware.php index bca82998..3ae7205d 100644 --- a/src/Middleware/LaratrustMiddleware.php +++ b/src/Middleware/LaratrustMiddleware.php @@ -49,7 +49,7 @@ protected function unauthorized(): mixed return App::abort($handler['code'], $handler['message'] ?? $defaultMessage); } - if ($handling === "json") { + if ($handling == "json") { $responseData = $handler['structure'] ?? []; if (!empty($handler['include_timestamp']) && boolval($handler['include_timestamp'])) { From 822d657033c6f9b054827f7d3b28c4c1e4886f62 Mon Sep 17 00:00:00 2001 From: Nicodav28 Date: Sun, 9 Mar 2025 17:28:19 -0500 Subject: [PATCH 3/4] style(laratrust.php,-LaratrustMiddleware.php): remove unnecessary whitespace and standardize string quotes The unnecessary whitespace in laratrust.php was removed to keep the code clean and maintainable. In LaratrustMiddleware.php, the double quotes around "json" were replaced with single quotes to standardize the use of string quotes across the codebase. This enhances readability and consistency in the code. --- config/laratrust.php | 1 - src/Middleware/LaratrustMiddleware.php | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/config/laratrust.php b/config/laratrust.php index dca66c13..c2730ec8 100644 --- a/config/laratrust.php +++ b/config/laratrust.php @@ -226,7 +226,6 @@ 'content' => '', ], ], - /** * Defines a custom JSON response format for unauthorized access. diff --git a/src/Middleware/LaratrustMiddleware.php b/src/Middleware/LaratrustMiddleware.php index 3ae7205d..62e2950c 100644 --- a/src/Middleware/LaratrustMiddleware.php +++ b/src/Middleware/LaratrustMiddleware.php @@ -49,7 +49,7 @@ protected function unauthorized(): mixed return App::abort($handler['code'], $handler['message'] ?? $defaultMessage); } - if ($handling == "json") { + if ($handling == 'json') { $responseData = $handler['structure'] ?? []; if (!empty($handler['include_timestamp']) && boolval($handler['include_timestamp'])) { From 3ddde0d8bda246ddfe293bf83a6c3730a97bf02f Mon Sep 17 00:00:00 2001 From: Nicodav28 Date: Sun, 9 Mar 2025 17:39:50 -0500 Subject: [PATCH 4/4] style(laratrust.php-LaratrustMiddleware.php): remove unnecessary whitespace and also adds a whitespace after the inequality operator in the condition where include timestamp option is being validated These changes are made to ensure that the code style is followed to the fullest extent --- config/laratrust.php | 2 +- src/Middleware/LaratrustMiddleware.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/config/laratrust.php b/config/laratrust.php index c2730ec8..a1e04605 100644 --- a/config/laratrust.php +++ b/config/laratrust.php @@ -226,7 +226,7 @@ 'content' => '', ], ], - + /** * Defines a custom JSON response format for unauthorized access. * This can be used when a JSON response is preferred over a redirect or abort. diff --git a/src/Middleware/LaratrustMiddleware.php b/src/Middleware/LaratrustMiddleware.php index 62e2950c..fdb59a60 100644 --- a/src/Middleware/LaratrustMiddleware.php +++ b/src/Middleware/LaratrustMiddleware.php @@ -52,10 +52,10 @@ protected function unauthorized(): mixed if ($handling == 'json') { $responseData = $handler['structure'] ?? []; - if (!empty($handler['include_timestamp']) && boolval($handler['include_timestamp'])) { + if (! empty($handler['include_timestamp']) && boolval($handler['include_timestamp'])) { $responseData['timestamp'] = now()->toISOString(); } - + return response()->json($responseData, $handler['code'] ?? 403); }