Skip to content

feat: Support hierarchical permissions in method#1309

Open
christianberkman wants to merge 1 commit intocodeigniter4:developfrom
christianberkman:hperm
Open

feat: Support hierarchical permissions in method#1309
christianberkman wants to merge 1 commit intocodeigniter4:developfrom
christianberkman:hperm

Conversation

@christianberkman
Copy link
Contributor

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, like admin.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, for admin.users.create, it will check for admin.users.create, admin.users.*, and admin.*.

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:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (No new PHPDoc blocks were necessary as the existing documentation sufficiently covers the functionality. The method signature remains unchanged.)
  • Unit testing, with >80% coverage (New test case testCanNestedPerms added to specifically cover the hierarchical permission logic.)
  • User guide updated (changed authorization.md file)
  • Conforms to style guide

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:

* `users.manage.create`

* `users.manage.edit`

* `users.manage.delete`

* `users.manage.anything.else`

However, it will not have access to:

* `users.view`

* `users.anything.else`

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.

@michalsn
Copy link
Member

michalsn commented Feb 7, 2026

Please rebase to resolve the errors, as they are now fixed in the develop branch.

Tests are still needed for Authorizable::can() with nested permissions (user-in-group via the matrix).

It would be nice to extract the duplicated wildcard logic into a shared method, but I'm not sure where to place it.

@neznaika0
Copy link

Are you worried about type errors? Write a test for her. Throw an exception if it's dangerous.

@christianberkman
Copy link
Contributor Author

@michalsn could you confirm I did the rebase correctly? Git (online) is still saying my branch is 1 commit behind on shield/develop but it looks to me the rebase did include that commit? /not-sure

@michalsn
Copy link
Member

@christianberkman It looks like your branch includes a commit that has already been merged into develop, so it's now appearing in the PR history.

You can clean this up by rebasing your branch onto the latest develop and dropping the already-merged commit.

Try:

git fetch origin
git rebase -i origin/develop

In the interactive editor, keep your feature commit and change the other one to drop.

Then force-push the updated branch:

git push --force-with-lease

That should leave the PR with only the initial commit.

Co-authored-by:  bgeneto <bgeneto@duck.com>
@christianberkman
Copy link
Contributor Author

@michalsn Thank you for your patience and guidance. I'll add to the Authorizable::can() test hopefully soon.

@bgeneto
Copy link
Contributor

bgeneto commented Feb 13, 2026

I sincerely hope we can successfully merge this PR one day 🤭.....
I’ve been relying on this feature (with success) across all my projects for quite some time.

@datamweb datamweb added the enhancement New feature or request label Feb 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants