提问人:Safi Mustafa 提问时间:1/15/2020 最后编辑:victcdvaSafi Mustafa 更新时间:12/30/2020 访问量:688
在IQueryable<T>中设置属性值,根据某些条件与另一个表连接后
Set value of property in IQueryable<T> after join with another table based on some conditions
问:
我正在实现 WPF 的授权。我与屏幕绑定了权限。例如,有一个名为 floor 的屏幕。它有四个与之关联的权限。
- 景观楼层
- 添加楼层
- 编辑楼层
- 删除楼层
除了每个权限外,我们还有四个级别的权限。
- 都
- 自我
- 角色
- 用户
因此,如果用户具有权限级别为 {ALL} 的 {View Floor} 权限。他们可以查看任何用户创建的所有楼层。
如果用户具有 {Self} 权限级别。他们只能查看自己创建的楼层。
如果用户的权限级别为 {Role}。他们可以查看根据指定角色分配的所有楼层。
权限级别的角色分配方式如下:
Roles: Supervisor, Technician
如果 BranchManager 具有上述权限级别。然后,他可以查看由他自己创建的楼层,以及由具有主管或技术员角色的任何用户创建的楼层。
权限级别 {User} 与 {Role} 相同。
这是我的 IQueryable 来获取地板:
IQueryable<FloorModel> FloorsQueryable = (from f in Floors
join b in Branches on f.BranchId equals b.Id
where (string.IsNullOrEmpty(filters.Name) || f.Name.ToLower().Contains(filters.Name.ToLower()))
&& (string.IsNullOrEmpty(filters.BranchName) || b.Name.ToLower().Contains(filters.BranchName.ToLower()))
&& (f.IsDeleted == null || f.IsDeleted == false)
&& f.BranchId == CurrentBranchId
&& f.ApplicationId == CurrentApplicationId
select new FloorModel
{
Id = f.Id,
Name = f.Name,
Code = f.Code,
Branch = new BranchBriefModel()
{
Id = f.BranchId,
Name = b.Name,
},
IsActive = f.IsActive ?? false,
Description = f.Description,
CreatedBy = f.CreatedBy ?? 0,
ApplicationId = CurrentApplicationId,
}).AsQueryable();
在此之后,我需要根据权限级别对此查询应用一些自定义过滤器
FloorsQueryable = CustomFilter(FloorsQueryable,CurrentUserId,filters);
CustomFilter 代码
public IQueryable<T> CustomFilter<T>(IQueryable<T> query, int CurrentUserId, BaseSearchModel search) where T : BaseModel, new()
{
var distinctClaims = search.ScreenPermission.Select(x => x.Claim.PermissionLevel).Distinct().ToList();
var viewPermission = search.ScreenPermission.Where(x => x.Name.Contains("View")).FirstOrDefault();
if (viewPermission != null && viewPermission.Claim.PermissionLevel != PermissionLevelCatalog.All)
{
if (distinctClaims.Any(x => x == PermissionLevelCatalog.Role))
{
query = (from q in query
join ur in UsersInRoles on q.CreatedBy equals ur.UserId
join r in Roles on ur.RoleId equals r.Id
select q);
}
string propertyName = "CreatedBy";
//if (string.IsNullOrEmpty(propertyName))
// return query;
// we have parameter "generic"
var selectorParameter = Expression.Parameter(typeof(T), "generic");
// constant "CurrentUserId"
var searchTermExpression = Expression.Constant(CurrentUserId);
// "generic.CreatedBy"
var selector = Expression.PropertyOrField(selectorParameter, propertyName);
// "generic.CreatedBy == "CurrentUserId"
var equal = Expression.Equal(selector, searchTermExpression);
// generic => generic.Name == "CurrentUserId"
var genericWhere = Expression.Lambda<Func<T, bool>>(equal, selectorParameter);
query = query.Where(genericWhere);
}
return query;
}
现在,对于权限级别 {Role},我需要在 IQueryable 的 select 子句中添加 CreatorRoleId,以在以下代码中应用所需的检查。
if (distinctClaims.Any(x => x == PermissionLevelCatalog.Role))
{
query = (from q in query
join ur in UsersInRoles on q.CreatedBy equals ur.UserId
join r in Roles on ur.RoleId equals r.Id
select q
);
}
是否可以设置CreatorRoleId的值?像下面这样的东西
select {q=>q.CreatorRoleId=r.Id return q;}
这应该是通用的,并且针对任何 IQueryable 运行。我不想为项目中的所有查询联接 Role 和 UserInRole 表,因为只有当某些用户具有此 {Role} 权限级别时才需要这些联接。
答: 暂无答案
评论
Select
Where
x.CreatedBy == CurrentUserId
Where
Select