获取所有具有特定权限的用户?
Get all users with specific permission?
如何获取所有具有特定权限的用户?
例如,我们需要枚举所有允许特定权限的用户。像这样的代码
var v=_userManager.GetAllUserWithSpecificPermission("ISO.Report.ReportV1");
foreach(var t in v)
{
Logger.Info(t.UserName);
}
您需要编写查询以按权限过滤用户。这可能会指导您:
var query = from user in UserManager.Users
join ur in _userRoleRepository.GetAll() on user.Id equals ur.UserId into urJoined
from ur in urJoined.DefaultIfEmpty()
join up in _userPermissionRepository.GetAll() on new {UserId = user.Id, Name = input.Permission} equals
new {up.UserId, up.Name} into upJoined
from up in upJoined.DefaultIfEmpty()
join rp in _rolePermissionRepository.GetAll() on new {RoleId = ur.RoleId, Name = input.Permission}
equals new {rp.RoleId, rp.Name} into rpJoined
from rp in rpJoined.DefaultIfEmpty()
where (up != null && up.IsGranted) || (up == null && rp != null)
group user by user
into userGrouped
select userGrouped.Key;
var users = await query.ToListAsync();
如何获取所有具有特定权限的用户? 例如,我们需要枚举所有允许特定权限的用户。像这样的代码
var v=_userManager.GetAllUserWithSpecificPermission("ISO.Report.ReportV1");
foreach(var t in v)
{
Logger.Info(t.UserName);
}
您需要编写查询以按权限过滤用户。这可能会指导您:
var query = from user in UserManager.Users
join ur in _userRoleRepository.GetAll() on user.Id equals ur.UserId into urJoined
from ur in urJoined.DefaultIfEmpty()
join up in _userPermissionRepository.GetAll() on new {UserId = user.Id, Name = input.Permission} equals
new {up.UserId, up.Name} into upJoined
from up in upJoined.DefaultIfEmpty()
join rp in _rolePermissionRepository.GetAll() on new {RoleId = ur.RoleId, Name = input.Permission}
equals new {rp.RoleId, rp.Name} into rpJoined
from rp in rpJoined.DefaultIfEmpty()
where (up != null && up.IsGranted) || (up == null && rp != null)
group user by user
into userGrouped
select userGrouped.Key;
var users = await query.ToListAsync();