Search code examples
permissionshierarchyaspnetboilerplate

Parent/child permissions grouping


According to the documentation on Authorization, I can use child permissions to group all related permissions to their parents:

"Permissions can have parent and child permissions. While this does not affect permission checking, it helps to group the permissions in the UI."

As an example, I would like to create a parent Permission "User Management" with "Create" , "Read", "Update" and "Delete" sub-permissions.

However, after setting my permission in SetPermissions, the hierarchy is not persisted anywhere so there's no way for me to know how permissions are related to each other from UI's perspective because the JSON returned is all flattened and the AbpPermissions table doesn't look like it maintains this either.

Here's my code snippet (Fig. 1)

Fig1

And here's my AbpPermissions table (Fig. 2)

Fig2

I would appreciate any suggestion or any insight on this.

Thanks and best regards.


Solution

  • A Permission is already hierachical, with Parent and Children properties.

    You can retrieve the structure that you defined using:

    var rootPermissions = _permissionManager.GetAllPermissions()
        .Where(p => p.Parent == null)
        .ToList();
    

    You can then recursively iterate the rootPermissions and check if it is granted:

    var isAssigned = _permissionChecker.IsGranted(permission.Name);