Skip to content

Commit

Permalink
Update PermissionHelper.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
neozhu committed Sep 28, 2024
1 parent f1087ec commit c898967
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/Server.UI/Services/PermissionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public PermissionHelper(IServiceScopeFactory scopeFactory, IFusionCache fusionCa
_userManager = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
_roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<ApplicationRole>>();
_fusionCache = fusionCache;
_refreshInterval = TimeSpan.FromDays(1);
_refreshInterval = TimeSpan.FromSeconds(30);
}

public async Task<IList<PermissionModel>> GetAllPermissionsByUserId(string userId)
Expand Down Expand Up @@ -72,7 +72,20 @@ private async Task<IList<Claim>> GetUserClaimsByUserId(string userId)
{
var user = await _userManager.FindByIdAsync(userId).ConfigureAwait(false)
?? throw new NotFoundException($"not found application user: {userId}");
return await _userManager.GetClaimsAsync(user).ConfigureAwait(false);
var userClaims= await _userManager.GetClaimsAsync(user).ConfigureAwait(false);
var roles = await _userManager.GetRolesAsync(user).ConfigureAwait(false);
var roleClaims = new List<Claim>();
foreach (var roleName in roles)
{
var role = await _roleManager.FindByNameAsync(roleName).ConfigureAwait(false)
?? throw new NotFoundException($"not found application role: {roleName}");
var claims = await _roleManager.GetClaimsAsync(role).ConfigureAwait(false);
roleClaims.AddRange(claims);
}
var allClaims = userClaims.Concat(roleClaims).Distinct(new ClaimComparer()).ToList();

return allClaims;

}, _refreshInterval).ConfigureAwait(false);
}

Expand Down Expand Up @@ -123,4 +136,18 @@ private async Task<IList<Claim>> GetUserClaimsByRoleId(string roleId)
return await _roleManager.GetClaimsAsync(role).ConfigureAwait(false);
}, _refreshInterval).ConfigureAwait(false);
}


public class ClaimComparer : IEqualityComparer<Claim>
{
public bool Equals(Claim x, Claim y)
{
return x.Type.Equals(y.Type) && x.Value.Equals(y.Value);
}

public int GetHashCode(Claim obj)
{
return HashCode.Combine(obj.Type, obj.Value);
}
}
}

0 comments on commit c898967

Please sign in to comment.