-
Notifications
You must be signed in to change notification settings - Fork 906
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] Custom access closures #5335
Merged
Merged
Conversation
This file contains 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
[ci skip] [skip ci]
pxpm
reviewed
Oct 3, 2023
pxpm
reviewed
Oct 3, 2023
pxpm
reviewed
Oct 3, 2023
PRO companion for this PR: https://github.com/Laravel-Backpack/PRO/pull/208 |
pxpm
reviewed
Oct 10, 2023
promatik
approved these changes
Oct 11, 2023
Co-authored-by: António Almeida <[email protected]>
Awesome! Added docs for it here - Laravel-Backpack/docs#516 - please check and merge both. @promatik when this is merged and tagged, please let me know, so I'll write a quick article about it on our blog. |
promatik
approved these changes
Oct 15, 2023
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.
WHY
BEFORE - What was wrong? What was happening before this PR?
We could not prevent access to an entire operation. ✅
But could NOT prevent access to an operation for a certain entry. ❌
For example, in a Product CRUD, say a user
list
andshow
all items;update
anddelete
THEIR items;Before this PR, that was very difficult to do in Backpack. You had to override the Update operation and the Update button. 🤢Horrible.
AFTER - What is happening after this PR?
An access condition can still be
true
andfalse
like before. But in addition to that, it can also be acallable
.The developer can define their own closure that defines if an operation has access or not. And inside that closure, they have access to
$entry
, so they can define access depending on$entry
,backpack_user()
etc.That means you can do THIS in your
ProductCrudController
:Note that this is one of those VERY FEW things that is best done in the
setup()
method, so that it applies for all operations. For example, you want to set this access closure for theupdate
anddelete
operations even during thelist
operation, so that theupdate
anddelete
buttons get hidden... like magic 🪄HOW
How did you achieve that, in technical terms?
hasAccessCondition(string $operation) : bool
getAccessCondition(string $operation) : bool|callable|null
setAccessCondition(array|string $operation, bool|callable|null $condition) : void
hasAccess($operation, $entry)
hasAccessOrFail($operation, $entry)
hasAccessToAll($operation, $entry)
hasAccessToAny($operation, $entry)
Is it a breaking change?
NO. All extra parameters are optional. All return types and parameters types I've added are exactly as the ones in the comment blocks before. No change in scope for old functionality.
How can we test the before & after?
Use the example above in a CrudController. You should notice:
update
anddelete
buttons disappear for the entries you excludeshow
button still showsupdate
page, it will throw a 403 error