获取委托单位下拉列表 添加干系人数据权限过滤

dev
cjy 1 month ago
parent 724666ec46
commit ebc8bcb162

@ -25,4 +25,9 @@ public static class DataRuleConst
/// 数据权限配置中当前登录所属部门的key
/// </summary>
public const string LoginDept = "{loginDept}";
/// <summary>
/// 数据权限配置中客户干系人的key
/// </summary>
public const string Stakeholders = "{loginDept}";
}

@ -53,6 +53,11 @@ namespace DS.WMS.Core.Code.Dtos
/// </summary>
public string DataRules { get; set; }
/// <summary>
/// 附加权限规则
/// </summary>
public string AdditionDataRules { get; set; }
/// <summary>
/// 数据权限描述
/// </summary>

@ -51,6 +51,10 @@ namespace DS.WMS.Core.Code.Dtos
/// 权限规则
/// </summary>
public string DataRules { get; set; }
/// <summary>
/// 附加权限规则
/// </summary>
public string AdditionDataRules { get; set; }
/// <summary>
/// 数据权限描述

@ -57,7 +57,11 @@ namespace DS.WMS.Core.Code.Entity
/// </summary>
[SugarColumn(ColumnDescription = "权限规则", IsNullable = true, ColumnDataType = StaticConfig.CodeFirst_BigString)]
public string DataRules { get; set; }
/// <summary>
/// 附加权限规则
/// </summary>
[SugarColumn(ColumnDescription = "附加权限规则", IsNullable = true, ColumnDataType = StaticConfig.CodeFirst_BigString)]
public string AdditionDataRules { get; set; }
/// <summary>
/// 权限描述
/// </summary>

@ -310,6 +310,6 @@ public interface ICommonService
/// <typeparam name="T"></typeparam>
/// <param name="tenantDb"></param>
/// <returns></returns>
public Task<(ISugarQueryable<T> queryable, string? ruleScope)> GetVisibleDataRuleFilter<T>(ISugarQueryable<T> queryable, SqlSugarScopeProvider tenantDb);
public Task<(ISugarQueryable<T> queryable, string? ruleScope, List<IConditionalModel>? contidions)> GetSpecialVisibleDataRuleFilter<T>(ISugarQueryable<T> queryable, SqlSugarScopeProvider tenantDb);
}

@ -1,3 +1,4 @@
using Amazon.Runtime.Internal;
using DS.Module.Core;
using DS.Module.Core.Constants;
using DS.Module.Core.Data;
@ -17,11 +18,13 @@ using DS.WMS.Core.Op.Method;
using DS.WMS.Core.Sys.Dtos;
using DS.WMS.Core.Sys.Entity;
using DS.WMS.Core.Sys.Interface;
using LogicExtensions;
using Mapster;
using Masuit.Tools;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using System.Collections.Generic;
using System.Linq;
namespace DS.WMS.Core.Sys.Method;
@ -189,9 +192,61 @@ public class ClientCommonService : IClientCommonService
var contacts = await tenantDb.Queryable<InfoClientContact>().Where(x => x.Status == StatusEnum.Enable && x.IsDefault == true)
.Select<ClientContactRes>().ToListAsync();
var conditions = new List<IConditionalModel>();
var query = tenantDb.Queryable<InfoClient>();
(query, _, conditions) = await commonService.GetSpecialVisibleDataRuleFilter<InfoClient>(query, tenantDb);
//new List<ConditionalCollections>()
//var query = tenantDb.Queryable<InfoClient>()//.Includes(a=>a.Stakeholders)
var userids = new List<string>();
if (conditions.Count>0)
{
foreach (ConditionalTree item in conditions)
{
Console.WriteLine(item.ConditionalList);
if (item.ConditionalList.Count>0 )
{
foreach (var item1 in item.ConditionalList)
{
//Console.WriteLine(item1.Value as);
var temp = item1.Value as SqlSugar.ConditionalModel;
if (temp.FieldName == "Stakeholders")
{
userids = temp.FieldValue.Split(',').ToList();
}
}
}
}
}
if (userids.Count==0)
{
userids.Add(user.UserId);
}
var data = await tenantDb.Queryable<InfoClient>().Includes(a=>a.Stakeholders)
.InnerJoin<InfoClientTag>((a, b) => a.Id == b.ClientId)
.Where((a, b) => a.Status == StatusEnum.Enable.ToEnumInt() && b.IsController == true || SqlFunc.Subqueryable<InfoClientStakeholder>().Where(c => c.ClientId == a.Id && c.StartDate <= DateTime.Now && c.EndDate >= DateTime.Now && userids.Contains(c.CreateBy.ToString())).Any()) //&& c.CreateBy == long.Parse(user.UserId)
.WhereIF(!string.IsNullOrEmpty(queryKey), (a, b) => a.CodeName.Contains(queryKey) || a.ShortName.Contains(queryKey) || a.Description.Contains(queryKey))
.Select((a, b) => new ControllerClientRes
{
Id = a.Id,
PinYinCode = a.ShortName + "(" + a.CodeName + ")",
//UserIds = a.Stakeholders.Where(c => c.StartDate <= DateTime.Now && c.EndDate >= DateTime.Now).Select(n=>n.CreateBy).ToList()
}, true
)
.Mapper(it =>
{
it.ClientContact = contacts.Where(x => x.ClientId == it.Id).FirstOrDefault();
})
//.MergeTable()
.Take(20).WithCache($"{SqlSugarCacheConst.InfoClient}{user.TenantId}", SqlSugarCacheConst.CacheDurationInSeconds).ToListAsync(); ;
//(query, _) = await commonService.GetVisibleDataRuleFilter<ControllerClientRes>(query, tenantDb);
//var data = await query.Where(a=>a.UserIds.Contains(long.Parse(user.UserId)))
// .Take(20).WithCache($"{SqlSugarCacheConst.InfoClient}{user.TenantId}", SqlSugarCacheConst.CacheDurationInSeconds).ToListAsync();
//var data = await tenantDb.Queryable<InfoClient>()
// .InnerJoin<InfoClientTag>((a, b) => a.Id == b.ClientId)
// .Where((a, b) => a.Status == StatusEnum.Enable.ToEnumInt() && b.IsController == true)
// .WhereIF(!string.IsNullOrEmpty(queryKey), (a, b) => a.CodeName.Contains(queryKey) || a.ShortName.Contains(queryKey) || a.Description.Contains(queryKey))
@ -199,38 +254,15 @@ public class ClientCommonService : IClientCommonService
// {
// Id = a.Id,
// PinYinCode = a.ShortName + "(" + a.CodeName + ")",
// //UserIds = a.Stakeholders.Where(c => c.StartDate <= DateTime.Now && c.EndDate >= DateTime.Now).Select(n=>n.CreateBy).ToList()
// //UserIds = SqlFunc.Subqueryable<InfoClientStakeholder>().Where(c => c.ClientId == a.Id && c.StartDate <= DateTime.Now && c.EndDate >= DateTime.Now).ToList(it=>it.CreateBy)
// //SaleOrgName = string.IsNullOrEmpty(a.SaleOrgId) ? "": orglist.Where(x=>x.Id == long.Parse(a.SaleOrgId)).FirstOrDefault().OrgName
// }, true
// )
// .Mapper(it =>
// {
// it.UserIds = SqlFunc.Subqueryable<InfoClientStakeholder>().Where(c => c.ClientId == it.Id && c.StartDate <= DateTime.Now && c.EndDate >= DateTime.Now).ToList(n => n.CreateBy);
// //it.ClientContact = tenantDb.Queryable<InfoClientContact>().Where(x => x.ClientId == it.Id && x.Status == StatusEnum.Enable && x.IsDefault == true).Select<ClientContactRes>().First();
// //it.ClientContact = contacts.Where(x => x.ClientId == it.Id).FirstOrDefault();
// //it.UserIds = tenantDb.Queryable<InfoClientStakeholder>().Where(c => c.StartDate <= DateTime.Now && c.EndDate >= DateTime.Now && c.ClientId == it.Id).Select(n => n.CreateBy).ToList();
// });
//(query, _) = await commonService.GetVisibleDataRuleFilter<ControllerClientRes>(query, tenantDb);
//var data = await query
// {
// it.ClientContact = tenantDb.Queryable<InfoClientContact>().Where(x => x.ClientId == it.Id && x.Status == StatusEnum.Enable && x.IsDefault == true)
// .Select<ClientContactRes>().First();
// })
// .Take(20).WithCache($"{SqlSugarCacheConst.InfoClient}{user.TenantId}", SqlSugarCacheConst.CacheDurationInSeconds).ToListAsync();
var data = await tenantDb.Queryable<InfoClient>()
.InnerJoin<InfoClientTag>((a, b) => a.Id == b.ClientId)
.Where((a, b) => a.Status == StatusEnum.Enable.ToEnumInt() && b.IsController == true)
.WhereIF(!string.IsNullOrEmpty(queryKey), (a, b) => a.CodeName.Contains(queryKey) || a.ShortName.Contains(queryKey) || a.Description.Contains(queryKey))
.Select((a, b) => new ControllerClientRes
{
Id = a.Id,
PinYinCode = a.ShortName + "(" + a.CodeName + ")",
//SaleOrgName = string.IsNullOrEmpty(a.SaleOrgId) ? "": orglist.Where(x=>x.Id == long.Parse(a.SaleOrgId)).FirstOrDefault().OrgName
}, true
)
.Mapper(it =>
{
it.ClientContact = tenantDb.Queryable<InfoClientContact>().Where(x => x.ClientId == it.Id && x.Status == StatusEnum.Enable && x.IsDefault == true)
.Select<ClientContactRes>().First();
})
.Take(20).WithCache($"{SqlSugarCacheConst.InfoClient}{user.TenantId}", SqlSugarCacheConst.CacheDurationInSeconds).ToListAsync();
return await Task.FromResult(DataResult<List<ControllerClientRes>>.Success("获取数据成功!", data, MultiLanguageConst.DataQuerySuccess));
}

@ -1594,49 +1594,47 @@ public class CommonService : ICommonService
/// <typeparam name="T"></typeparam>
/// <param name="tenantDb"></param>
/// <returns></returns>
public async Task<(ISugarQueryable<T> queryable, string? ruleScope)> GetVisibleDataRuleFilter<T>(ISugarQueryable<T> queryable,SqlSugarScopeProvider tenantDb)
public async Task<(ISugarQueryable<T> queryable, string? ruleScope, List<IConditionalModel>? contidions)> GetSpecialVisibleDataRuleFilter<T>(ISugarQueryable<T> queryable,SqlSugarScopeProvider tenantDb)
{
// 因为TaskBaseAllocation不能继承IOrgId所以在这里根据条件在需要的时候手动添加IOrgId查询筛选器
bool isTask = typeof(T) == typeof(TaskBaseAllocation);
var contidions = new List<IConditionalModel>();
var otherContidions = new List<IConditionalModel>();
var userInfo = db.Queryable<SysUser>().First(x => x.Id == long.Parse(user.UserId));
if (userInfo.UserType == 0)
{
if (isTask) queryable.Filter(typeof(IOrgId));
return (queryable, "all"); //超级管理员特权
return (queryable, "all", otherContidions); //超级管理员特权
}
var moduleName = typeof(T).Name.ToLower();
//var rule = tenantDb.Queryable<CodeDataRule>()
// .First(u => u.UserId == long.Parse(user.UserId) && u.PermissionEntity.ToLower() == moduleName && u.Status == StatusEnum.Enable);
var rule = tenantDb.Queryable<CodeDataRule>()
.First(u => u.UserId == long.Parse(user.UserId) && u.PermissionEntity.ToLower() == "infoclient" && u.Status == StatusEnum.Enable);
.First(u => u.UserId == long.Parse(user.UserId) && u.PermissionEntity.ToLower() == moduleName && u.Status == StatusEnum.Enable);
if (rule.IsNull())
{
return (queryable, null); //默认查询
return (queryable, null, otherContidions); //默认查询
}
else
{
var contidions = new List<IConditionalModel>();
{
#region 可视权限
if ((long)rule.VisibleTemplateId == 0)
{
contidions.Add(new ConditionalModel { FieldName = "CreateBy", ConditionalType = ConditionalType.Equal, FieldValue = user.UserId });
return (queryable.Where(contidions), "self"); //查询自身
return (queryable.Where(contidions), "self", otherContidions); //查询自身
}
var ruleInfo = tenantDb.Queryable<CodeDataRuleTemplate>().First(x => x.Id == (long)rule.VisibleTemplateId);
if (ruleInfo.RuleScope == "all")
{
return (queryable.ClearFilter<IOrgId>(), ruleInfo.RuleScope); //查询全部
return (queryable.ClearFilter<IOrgId>(), ruleInfo.RuleScope, otherContidions); //查询全部
}
else if (ruleInfo.RuleScope == "none")
{
if (isTask)
{ return (queryable, ruleInfo.RuleScope); }
{ return (queryable, ruleInfo.RuleScope, otherContidions); }
else
{
SqlSugar.Check.ExceptionEasy("NO Operation", "没有数据查看权限!");//返回提示
@ -1646,13 +1644,14 @@ public class CommonService : ICommonService
{
if (isTask) queryable.Filter(typeof(IOrgId));
var conditionalModels = ConvertConditionalModel(ruleInfo.DataRules);
var additions = ConvertConditionalModel(ruleInfo.AdditionDataRules);
if (conditionalModels.Count > 0)
{
return (queryable.Where(conditionalModels), ruleInfo.RuleScope);//自定义查询
return (queryable.Where(conditionalModels), ruleInfo.RuleScope, additions);//自定义查询
}
else
{
return (queryable, ruleInfo.RuleScope);//默认查询
return (queryable.Where(contidions), ruleInfo.RuleScope, otherContidions);//默认查询
}
}
else if (ruleInfo.RuleScope == "self_org_with_child") // 当前机构及下属机构
@ -1661,19 +1660,20 @@ public class CommonService : ICommonService
IEnumerable<long> orgIdList = orgList.Select(x => x.Id);
contidions.Add(new ConditionalModel { FieldName = "OrgId", ConditionalType = ConditionalType.In, FieldValue = string.Join(",", orgIdList) });
return (queryable.Where(contidions), ruleInfo.RuleScope);
return (queryable.Where(contidions), ruleInfo.RuleScope, otherContidions);
}
else if (ruleInfo.RuleScope == "self")
{
var conditionalModels = ConvertConditionalModel(ruleInfo.DataRules);
var additions = ConvertConditionalModel(ruleInfo.AdditionDataRules);
if (conditionalModels.Count > 0)
{
return (queryable.Where(conditionalModels), ruleInfo.RuleScope);//自定义查询
return (queryable.Where(conditionalModels), ruleInfo.RuleScope, additions);//自定义查询
}
else
{
contidions.Add(new ConditionalModel { FieldName = "CreateBy", ConditionalType = ConditionalType.Equal, FieldValue = user.UserId });
return (queryable.Where(contidions), ruleInfo.RuleScope); //查询创建人自身信息
return (queryable.Where(contidions), ruleInfo.RuleScope, otherContidions); //查询创建人自身信息
}
}
@ -1687,15 +1687,16 @@ public class CommonService : ICommonService
}
}
var conditionalModels = ConvertConditionalModel(ruleInfo.DataRules);
var additions = ConvertConditionalModel(ruleInfo.AdditionDataRules);
if (conditionalModels.Count > 0)
{
return (queryable.Where(conditionalModels), ruleInfo.RuleScope);//自定义查询
{
return (queryable.Where(conditionalModels), ruleInfo.RuleScope, additions);//自定义查询
}
else
{
var deptUsers = db.Queryable<SysUser>().Where(x => x.DeptId == userInfo.DeptId).Select(x => x.Id).ToList();
contidions.Add(new ConditionalModel { FieldName = "CreateBy", ConditionalType = ConditionalType.In, FieldValue = string.Join(",", deptUsers) });
return (queryable.Where(contidions), ruleInfo.RuleScope); //查询本部门
return (queryable.Where(contidions), ruleInfo.RuleScope, contidions); //查询本部门
}
}
else if (ruleInfo.RuleScope == "select_org")
@ -1705,28 +1706,31 @@ public class CommonService : ICommonService
//var conditionalModels =
// db.ConfigQuery.Context.Utilities.JsonToConditionalModels(
// JsonConvert.SerializeObject(conditions));
return (queryable.ClearFilter<IOrgId>().Where(conditionalModels), ruleInfo.RuleScope); //查询指定机构
var additions = ConvertConditionalModel(ruleInfo.AdditionDataRules);
return (queryable.ClearFilter<IOrgId>().Where(conditionalModels), ruleInfo.RuleScope, additions); //查询指定机构
}
else if (ruleInfo.RuleScope == "select_user")
{
//var conditionalModels = ConvertConditionalModel(ruleInfo.DataRules);
var conditionalModels = ConvertConditionalModel(ruleInfo.DataRules);
return (queryable.ClearFilter<IOrgId>().Where(conditionalModels), ruleInfo.RuleScope);//查询指定人员
var additions = ConvertConditionalModel(ruleInfo.AdditionDataRules);
return (queryable.ClearFilter<IOrgId>().Where(conditionalModels), ruleInfo.RuleScope, additions);//查询指定人员
}
else if (ruleInfo.RuleScope == "customize")
{
//var conditionalModels = ConvertConditionalModel(ruleInfo.DataRules);
var conditionalModels = ConvertConditionalModel(ruleInfo.DataRules);
return (queryable.Where(conditionalModels), ruleInfo.RuleScope);//自定义查询
var additions = ConvertConditionalModel(ruleInfo.AdditionDataRules);
return (queryable.Where(conditionalModels), ruleInfo.RuleScope, additions);//自定义查询
}
else if (ruleInfo.DataRules.IsNull())
{
contidions.Add(new ConditionalModel { FieldName = "CreateBy", ConditionalType = ConditionalType.Equal, FieldValue = user.UserId });
return (queryable.Where(contidions), ruleInfo.RuleScope); //没有设置数据规则,那么视为该资源允许被创建人查看
return (queryable.Where(contidions), ruleInfo.RuleScope, otherContidions); //没有设置数据规则,那么视为该资源允许被创建人查看
}
return (queryable, ruleInfo.RuleScope);
return (queryable, ruleInfo.RuleScope, otherContidions);
#endregion
}

Loading…
Cancel
Save