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)
-
0
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
-
0
Can you show relevant code?
-
0
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(); } }
-
0
That should work. Can you show the complete method instead of your workaround?
-
0
public async Task<GetRoleForEditOutput> GetRoleForEdit(NullableIdDto input) { var permissions = PermissionManager.GetAllPermissions();
var grantedPermissions = new Permission[0]; var grantedPermissionsList = new List<Permission>(); 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<RoleEditDto>(role); } else { roleEditDto = new RoleEditDto(); } var model= new GetRoleForEditOutput { Role = roleEditDto, Permissions = ObjectMapper.Map<List<FlatPermissionDto>>(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<FlatPermissionDto> moduleList = new List<FlatPermissionDto>(); 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; }
-
0
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?
-
0
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
-
0
So the roles do not actually have permissions, but only from your injected objects?
-
0
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).
-
0
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.
-
0
i think the problem because of the caching how can i disable cache of this page
-
0
Use AsNoTracking. Instead of GetRoleByIdAsync, do:
var role = await Repository.GetAll().AsNoTracking().FirstAsync(x => x.Id == input.Id.Value);
-
0
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
-
0
The problem is not with page caching. Why does it matter if you need to GetGrantedPermissionsAsync and inject anyway?
-
0
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);