Base solution for your next web application
Open Closed

Change/Add/Remove Role Permission Tree Root Menu Item Dynamically #11619


User avatar
0
hongbing.wang created

Hi support team,

We need to find a way to access the AppPermissions.Pages permission, the root of the permission tree, and dynamically add or remove a root menu item. Please advise which method we should use.

I tried with the following code. The problem is role.Permissions is null, but the user’s role does have permissions, see the screenshot. What did I miss?

    private async Task RemoveAlarmPermissionsAsync()
    {
        var roleId = (int)AbpSession.UserId;
        var role = await _roleManager.GetRoleByIdAsync(roleId);
        var permissions = PermissionManager.GetAllPermissions();
        var rootAlarmPermission = permissions.Where(p => p.Name == "Pages.Alarms").FirstOrDefault();
        if (role.Permissions != null && rootAlarmPermission != null)
        {
            //remove Pages.Alarms from the root permission tree
            var alarmRolePermissionSetting = new RolePermissionSetting
            {
                TenantId = _tenantId,
                Name = "Pages.Alarms",
                IsGranted = true,
                RoleId = roleId
            };

            role.Permissions.Remove(alarmRolePermissionSetting);
        }
    }


6 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    We have replied to your email but here is the answer again;

    You can inject IPermissionDefinitionContext and get any permission using its GetPermissionOrNull method. Then, you can add a permission to any permission element's children. If you want to keep these permissions when the app is restarted, you also need to add this permission to the database.

  • User Avatar
    0
    hongbing.wang created

    Hi ismcagdas, Thank you. I tried injecting IPermissionDefinitionContext through constructor and getting any permission using its GetPermissionOrNull method. But I got the runtime error "Can't create component as it has dependencies to be satisfied". What else do I need to implement? Can I inject IPermissionDefinitionContext in any app service?

    I think we already did it in AppAuthProviderExtension, but this method is only called once on application startup.

    Can we override the "auth" response for this call: https://localhost:44301/AbpUserConfiguration/GetAll?d=1687166682491

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    You can override AbpUserConfiguration/GetAll but I think it will not work as you want. Could oyu tell me where did you injected IPermissionDefinitionContext and full error message ?

    Yes, since AuthorizaitoProvider is called once, you also need to update related cache and database tables as well.

  • User Avatar
    0
    hongbing.wang created

    Hi ismcagdas,

    We tried it in a couple of AppService classes. We also tried it in TokenAuthController.cs. The errors are similar, for example, "Can't create component 'umsplus.Web.Controllers.TokenAuthController' as it has dependencies to be satisfied".

    We have found other ways to dynamic show/hide menu items and permission tree items in app-navigation.service.ts, PermissionAppService and RoleAppService.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @hongbing.wang

    So, is this resolved for you ?

  • User Avatar
    0
    hongbing.wang created

    This issue has been resolved.