Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Injecting permissions dynamically #3933


User avatar
0
sago created

i use module zero, and i injected some permissions dynamically and it stored as granted after saving . but GetGrantedPermissionsAsync function doesn't return them it returns only the permissions that only matches the hard coded ones and i tried to append them manually but apppermissions table is inaccessible. i need to append them to be shown as checked permissions


15 Answer(s)
  • User Avatar
    0
    sago created

    Update... i found my added permissions at role object but Role.Permmissions but this property filled only for the first time if i closed the window and opened it again it becomes null

  • User Avatar
    0
    aaron created
    Support Team

    Can you show relevant code?

  • User Avatar
    0
    sago created

    grantedPermissions = (await _roleManager.GetGrantedPermissionsAsync(role)).ToArray(); //because this function doesn't return my injected objects i iterated permissions on role.permessions as shown in below but the data only exist at the first time if i open them again it becomes null

                if (role.Permissions!=null)
                {
                   
                    foreach (var item in role.Permissions)
                    {
                        Permission p = new Permission(name: item.Name, displayName: L(item.Name));
                        grantedPermissionsList.Add(p);
                    }
                    if (grantedPermissionsList.Any())
                    {
                        grantedPermissions = grantedPermissionsList.ToArray();
                    }
                    
                }
    
  • User Avatar
    0
    aaron created
    Support Team

    That should work. Can you show the complete method instead of your workaround?

  • User Avatar
    0
    sago created

    public async Task<GetRoleForEditOutput> GetRoleForEdit(NullableIdDto input) { var permissions = PermissionManager.GetAllPermissions();

            var grantedPermissions = new Permission[0];
            var grantedPermissionsList = new List&lt;Permission&gt;();
            RoleEditDto roleEditDto;
    
            if (input.Id.HasValue) //Editing existing role?
            {
                var role = await _roleManager.GetRoleByIdAsync(input.Id.Value);
                grantedPermissions = (await _roleManager.GetGrantedPermissionsAsync(role)).ToArray();
                if (role.Permissions!=null)
                {
                   
                    foreach (var item in role.Permissions)
                    {
                        Permission p = new Permission(name: item.Name, displayName: L(item.Name));
                        grantedPermissionsList.Add(p);
                    }
                    if (grantedPermissionsList.Any())
                    {
                        grantedPermissions = grantedPermissionsList.ToArray();
                    }
                    
                }
               
    
    
                roleEditDto = ObjectMapper.Map&lt;RoleEditDto&gt;(role);
            }
            else
            {
                roleEditDto = new RoleEditDto();
            }
    
          var model= new GetRoleForEditOutput
            {
                Role = roleEditDto,
                Permissions = ObjectMapper.Map&lt;List&lt;FlatPermissionDto&gt;>(permissions).OrderBy(p => p.DisplayName).ToList(),
                GrantedPermissionNames = grantedPermissions.Select(p => p.Name).ToList()
            };
            var items = _SystemObjectRepository.GetAll().OrderBy(a => a.Id).ToList();
            foreach (var item in items.Where(a => a.SystemObjectTypeId == 1))
            {
                var parentname = GetEnglishName(item.JsonName);
                var parentnameForView = GetLocalizedJsonName(item.JsonName);
                var parentPermession = "Pages." + parentname.Replace(" ", "");
                model.Permissions.Add(new FlatPermissionDto() { DisplayName = parentnameForView, Name = parentPermession,ParentName= "Pages" });
                var childs =  DrawChilds(item.Id, parentPermession, items);
                model.Permissions.AddRange(childs);
            }
            return model;
        }
    

    private List<FlatPermissionDto> DrawChilds(int id, string parentPermessionName, List<SystemObject> list) {

            List&lt;FlatPermissionDto&gt; moduleList = new List&lt;FlatPermissionDto&gt;();
            var childs = list.Where(a => a.ParentId == id);
            if (childs.Any())
            {
                foreach (var item in childs)
                {
                    //var curchild = list.Where(a => a.Id == item.Id).FirstOrDefault();
                    FlatPermissionDto curchildPermession = new FlatPermissionDto();
                    var subParentName = GetEnglishName(item.JsonName);
                    var subParentNameForView = GetLocalizedJsonName(item.JsonName);
                    var subParentPermession = parentPermessionName + "." + subParentName.Replace(" ", "");
                    curchildPermession.DisplayName = subParentNameForView;
                    curchildPermession.Name = subParentPermession;
                    curchildPermession.ParentName = parentPermessionName;
                    moduleList.Add(curchildPermession);
                    moduleList.AddRange(DrawChilds(item.Id, subParentPermession, list));
                }
            }
       
            return moduleList;
        }
    
  • User Avatar
    0
    aaron created
    Support Team

    Do you mean that the permissions are already in the database and that every time you get (without editing), it doesn't give the same result?

  • User Avatar
    0
    sago created

    Yes , role.permission :some times comes with null and sometimes comes with my permissions that have been granted from my injected objects ,and it 's never comes from GetGrantedPermissionsAsync() function

  • User Avatar
    0
    aaron created
    Support Team

    So the roles do not actually have permissions, but only from your injected objects?

  • User Avatar
    0
    sago created

    no it has granted some permission of both (hard coded ones and injected ones), but GetGrantedPermissionsAsync() function returns only the granted permissions that matches hard coded ones

    but i noticed that GetRoleByIdAsync() has a property called permission this property some times comes with my granted injected permissions and sometimes comes with null (mostly comes with null).

  • User Avatar
    0
    aaron created
    Support Team

    The permissions in the role from GetRoleByIdAsync are populated only if the permissions exist in the current context. You should not rely on the permissions here.

    Always use GetGrantedPermissionsAsync, which returns permissions in the database, then inject other permissions.

  • User Avatar
    0
    sago created

    i think the problem because of the caching how can i disable cache of this page

  • User Avatar
    0
    aaron created
    Support Team

    Use AsNoTracking. Instead of GetRoleByIdAsync, do:

    var role = await Repository.GetAll().AsNoTracking().FirstAsync(x => x.Id == input.Id.Value);
    
  • User Avatar
    0
    sago created

    i think the problem is with caching because after i clear cache from maintenance page it works fine,how can i disable page to be cached

  • User Avatar
    0
    aaron created
    Support Team

    The problem is not with page caching. Why does it matter if you need to GetGrantedPermissionsAsync and inject anyway?

  • User Avatar
    0
    sago created

    thanks aaron very much the problem is solved by your effort

    var role = await _role.GetAllIncluding(a=>a.Permissions).AsNoTracking().FirstAsync(x => x.Id == input.Id.Value);