feat: Support hierarchical permissions in method#1309
Open
christianberkman wants to merge 1 commit intocodeigniter4:developfrom
Open
feat: Support hierarchical permissions in method#1309christianberkman wants to merge 1 commit intocodeigniter4:developfrom
christianberkman wants to merge 1 commit intocodeigniter4:developfrom
Conversation
Co-authored-by: bgeneto <bgeneto@duck.com>
5 tasks
Member
|
Please rebase to resolve the errors, as they are now fixed in the Tests are still needed for It would be nice to extract the duplicated wildcard logic into a shared method, but I'm not sure where to place it. |
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.
This is a reboot of #1253 by @bgeneto. I have copied his changes and applied them to the most recent develop branch. I did run PHPStan and Rector which did not return errors on the changed files.
Description
This pull request enhances the
can()method in CodeIgniter Shield to support hierarchical permissions, allowing for more granular control over user access. Previously, Shield only supported single-level permissions (e.g.,users.create), see #1229 This change enables checking permissions with multiple levels, likeadmin.users.crud.create, using wildcard matching at each level.The core change involves modifying the
can()method to generate a series of wildcard checks based on the input permission string. It now iteratively checks for all possible parent levels. For example, foradmin.users.create, it will check foradmin.users.create,admin.users.*, andadmin.*.A new test case,
testCanNestedPerms, has been added to verify the correct behavior of the hierarchical permission checks, including various positive and negative scenarios.This improvement provides greater flexibility in defining and managing permissions within applications built with Shield. It allows for a more natural and organized permission structure, reflecting the hierarchical nature of many application features.
Checklist:
testCanNestedPermsadded to specifically cover the hierarchical permission logic.)User Guide Updates (Required):
The following section needs to be added/updated in the User Guide (likely within the "Authorization" or "Permissions" section):
Hierarchical Permissions
Shield now supports hierarchical permissions, allowing you to define permissions with multiple levels separated by dots (
.). This enables a more granular and organized approach to authorization.You can use the wildcard character (
*) at any level to represent all permissions within that level.Example:
If a group has the permission
users.manage.*, it will have access to:However, it will not have access to:
The
can()method will automatically check all parent levels when evaluating a permission.Note: This update significantly improves the flexibility of the authorization system. Be sure to review your existing permissions and consider how you can leverage hierarchical permissions to simplify and improve your authorization logic.