feat(LaratrustMiddleware@unauthorized): Add JSON response handling for unauthorized access#691
Open
Nicodav28 wants to merge 4 commits intosantigarcor:masterfrom
Open
feat(LaratrustMiddleware@unauthorized): Add JSON response handling for unauthorized access#691Nicodav28 wants to merge 4 commits intosantigarcor:masterfrom
Nicodav28 wants to merge 4 commits intosantigarcor:masterfrom
Conversation
…format for unauthorized access 🔧 (laratrust.php): Update configuration to include new 'json' key with custom response structure The changes were made to provide a more detailed and structured response when unauthorized access is attempted. This is particularly useful when dealing with API endpoints where a JSON response is more appropriate than a redirect or abort. The configuration file was updated to include a new 'json' key that defines the structure of the response, including a custom message and the option to include a timestamp. The middleware was updated to handle this new configuration and return the custom JSON response when necessary.
… readability and consistency The changes were made to improve the readability of the code and to ensure consistency across the codebase. The alignment of the array elements in 'laratrust.php' was adjusted to make the code cleaner and easier to read. In 'LaratrustMiddleware.php', the strict equality operator was replaced with a loose equality operator to allow for type coercion, which can prevent potential bugs in the future.
…tespace 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.
…espace 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
Author
|
@santigarcor Would you like to give a brief review to this PR, I can make any adjustment you consider pertinent to approve this PR, please and thank you! |
santigarcor
requested changes
Jun 2, 2025
Owner
santigarcor
left a comment
There was a problem hiding this comment.
What if instead of returning a json the configuration allows for a custom response handler? Like an invokable class that returns a response? In that way you can do whatever you want.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What’s New?
unauthorized()method inLaratrustMiddleware.laratrust.middleware.handlingis set tojson, the middleware will return a structured JSON response instead of redirecting or aborting.Why This Change?
config/laratrust.php.Changes Made
unauthorized()to support JSON response.jsonhandling option inconfig/laratrust.php, with:code).include_timestamp).structure).abortandredirecthandlers.How to Test?
config/laratrust.php:php artisan config:clearto apply the changes.{ "status": "error", "message": "User does not have the necessary access rights to perform this action.", "timestamp": "2024-07-01T12:34:56.789Z" }Let me know if you need any adjustments