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

Permession management issue #4171


User avatar
0
sago created

hey all, i have an issue with permessions, i made a workaround solution for generating permessions dynamically from database and it can be set properly the problem is with this function SetGrantedPermissionsAsync(role, permissions) if i unselect some permessions it removes only predefined permessions,and i tried to make it manauly so i made custom repository and make a stroed to remove all permessions to be set again but it didn't work any help ?


9 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    What do you mean by it didn't work?

  • User Avatar
    0
    sago created

    the strored procedure doesnt executed ,it started and never ended

    create PROCEDURE [dbo].[Core_Remove_permessions_By_RoleId] @roleId int AS BEGIN SET NOCOUNT ON; -- Insert statements for procedure here delete AbpPermissions where RoleId=@roleId END

  • User Avatar
    0
    sago created

    here is my code

    private async Task UpdateGrantedPermissionsAsync(Role role, List<string> grantedPermissionNames) {

            var grantedPermissions = PermissionManager.GetPermissionsFromNamesByValidating(grantedPermissionNames);
            List&lt;Permission&gt; customList = new List&lt;Permission&gt;();
            foreach (var item in grantedPermissions)
            {
                customList.Add(item);
            }
            var additionalPermessions = await GetGrantedPermessionsFromSystemObjectsAsync(grantedPermissionNames);
            customList.AddRange(additionalPermessions);
            await RemoveOldgarntedPermissionAsync(role.Id);
            await _roleManager.SetGrantedPermissionsAsync(role, customList);
        }
        private async Task RemoveOldgarntedPermissionAsync(int id)
        {
            var oldpermessionsToBeDeleted = await _sysobjectCustomRepo.RemoveGrantedPermessionsForRole(id);
        }
    

    RemoveOldgarntedPermissionAsync:this function call stored procedure to delete but it doesn't execute till the end of function UpdateGrantedPermissionsAsync so it deletes after setting new permessions i don't know how to make it executes before setting the new permessions

  • User Avatar
    0
    aaron created
    Support Team

    What are the current and expected behaviors? Give an example with before and after permissions.

  • User Avatar
    0
    sago created

    i expected when updating permessions for arole or user

    first removes old roles with my custom stored procedure then set the new permessions with setgrantedpermessions function

    but what happening now it removes the permessions at the end of the function although i put await befor my custom function the result is the whole permessions removed for this role

  • User Avatar
    0
    aaron created
    Support Team

    Try calling SaveChanges in RemoveOldgarntedPermissionAsync.

  • User Avatar
    0
    sago created

    RemoveOldgarntedPermissionAsync is in application layer how can i call savechanges, i tried to call it from my custom repositry but it didn't removes the records till the end also.

    this is the function in my custom repo. public async Task<string> RemoveGrantedPermessionsForRole(int id) { EnsureConnectionOpen();

            using (var command = CreateCommand("Core_Remove_permessions_By_RoleId", CommandType.StoredProcedure, new SqlParameter("@roleId", id)))
            {
                using (var dataReader = await command.ExecuteReaderAsync())
                {
                    var result = "";
    
                    while (dataReader.Read())
                    {
                        result = (dataReader["roleId"].ToString());
                    }
                     
                    Context.SaveChanges();
                    return result;
                }
            }
    
  • User Avatar
    0
    aaron created
    Support Team

    Okay, what's probably happening is:

    • your stored procedure removes the permissions, but
    • the role permission cache says the permissions are still there, so
    • the role permissions don't get re-added. You can inject ICacheManager and clear the cache:
    private async Task RemoveOldgarntedPermissionAsync(int id)
    {
        var oldpermessionsToBeDeleted = await _sysobjectCustomRepo.RemoveGrantedPermessionsForRole(id);
        _cacheManager.GetRolePermissionCache().Clear();
    }
    
  • User Avatar
    0
    sago created

    Thanks alot,this was my problem