客户数据权限修改

dev
cjy 1 month ago
parent 345479d530
commit aaa5eb7108

@ -132,5 +132,10 @@ namespace DS.WMS.Core.Info.Dtos
/// 服务项目
/// </summary>
public string? ServiceItem { get; set; }
/// <summary>
/// 干系人列表
/// </summary>
public List<long> UserIds { get; set; }
}
}

@ -459,4 +459,10 @@ public class InfoClient : SharedOrgModel<long>
/// </summary>
[Navigate(NavigateType.OneToMany, nameof(InfoClientContact.ClientId))]
public List<InfoClientContact>? Contacts { get; set; }
/// <summary>
/// 干系人列表
/// </summary>
[SqlSugar.SugarColumn(IsIgnore = true)]
public List<long> UserIds { get; set; }
}

@ -304,4 +304,12 @@ public interface ICommonService
/// <returns></returns>
public Task<(ISugarQueryable<T> queryable, string? ruleScope)> GetVisibleDataRuleFilter<T>(SqlSugarScopeProvider tenantDb);
/// <summary>
/// 获取当前用户的可视权限范围
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="tenantDb"></param>
/// <returns></returns>
public Task<(ISugarQueryable<T> queryable, string? ruleScope)> GetVisibleDataRuleFilter<T>(ISugarQueryable<T> queryable, SqlSugarScopeProvider tenantDb);
}

@ -13,6 +13,7 @@ using DS.WMS.Core.Info.Dtos;
using DS.WMS.Core.Info.Entity;
using DS.WMS.Core.Op.Dtos;
using DS.WMS.Core.Op.Entity;
using DS.WMS.Core.Op.Method;
using DS.WMS.Core.Sys.Dtos;
using DS.WMS.Core.Sys.Entity;
using DS.WMS.Core.Sys.Interface;
@ -30,7 +31,7 @@ public class ClientCommonService : IClientCommonService
private readonly ISqlSugarClient db;
private readonly IUser user;
private readonly ISaasDbService saasService;
private readonly ICommonService commonService;
/// <summary>
///
/// </summary>
@ -41,6 +42,7 @@ public class ClientCommonService : IClientCommonService
db = _serviceProvider.GetRequiredService<ISqlSugarClient>();
user = _serviceProvider.GetRequiredService<IUser>();
saasService = _serviceProvider.GetRequiredService<ISaasDbService>();
commonService = _serviceProvider.GetRequiredService<ICommonService>();
}
internal ISugarQueryable<SysDictData> DicQueryable(string typeCode, string queryKey)
@ -172,6 +174,27 @@ public class ClientCommonService : IClientCommonService
var orglist = db.Queryable<SysOrg>().ToList();
//var query = 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 + ")",
// //UserIds = SqlFunc.MappingColumn<string>(sql)
// //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();
// //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
// .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)
@ -189,6 +212,7 @@ public class ClientCommonService : IClientCommonService
.Select<ClientContactRes>().First();
})
.Take(20).WithCache($"{SqlSugarCacheConst.InfoClient}{user.TenantId}", SqlSugarCacheConst.CacheDurationInSeconds).ToListAsync();
return await Task.FromResult(DataResult<List<ControllerClientRes>>.Success("获取数据成功!", data, MultiLanguageConst.DataQuerySuccess));
}
/// <summary>

@ -27,6 +27,8 @@ using DS.Module.Core.Constants;
using DS.WMS.Core.Op.Entity;
using DS.Module.Core.Data;
using DS.WMS.Core.TaskPlat.Entity;
using DS.WMS.Core.Invoice.Dtos;
using DS.WMS.Core.Op.Method;
namespace DS.WMS.Core.Sys.Method;
@ -42,7 +44,6 @@ public class CommonService : ICommonService
private readonly IHttpContextAccessor IhttpContext;
private readonly IWebHostEnvironment _environment;
private readonly ILogger<CommonService> _logger;
/// <summary>
/// 任务台基础表自定义列对应的Code
/// </summary>
@ -1582,6 +1583,151 @@ public class CommonService : ICommonService
}
}
/// <summary>
/// 获取当前用户的可视权限范围
/// </summary>
/// <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)
{
// 因为TaskBaseAllocation不能继承IOrgId所以在这里根据条件在需要的时候手动添加IOrgId查询筛选器
bool isTask = typeof(T) == typeof(TaskBaseAllocation);
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"); //超级管理员特权
}
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);
if (rule.IsNull())
{
return (queryable, null); //默认查询
}
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"); //查询自身
}
var ruleInfo = tenantDb.Queryable<CodeDataRuleTemplate>().First(x => x.Id == (long)rule.VisibleTemplateId);
if (ruleInfo.RuleScope == "all")
{
return (queryable.ClearFilter<IOrgId>(), ruleInfo.RuleScope); //查询全部
}
else if (ruleInfo.RuleScope == "none")
{
if (isTask)
{ return (queryable, ruleInfo.RuleScope); }
else
{
SqlSugar.Check.ExceptionEasy("NO Operation", "没有数据查看权限!");//返回提示
}
}
else if (ruleInfo.RuleScope == "self_org")
{
if (isTask) queryable.Filter(typeof(IOrgId));
var conditionalModels = ConvertConditionalModel(ruleInfo.DataRules);
if (conditionalModels.Count > 0)
{
return (queryable.Where(conditionalModels), ruleInfo.RuleScope);//自定义查询
}
else
{
return (queryable, ruleInfo.RuleScope);//默认查询
}
}
else if (ruleInfo.RuleScope == "self_org_with_child") // 当前机构及下属机构
{
List<SysOrg> orgList = await db.Queryable<SysOrg>().ToChildListAsync(s => s.ParentId, user.OrgId);
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);
}
else if (ruleInfo.RuleScope == "self")
{
var conditionalModels = ConvertConditionalModel(ruleInfo.DataRules);
if (conditionalModels.Count > 0)
{
return (queryable.Where(conditionalModels), ruleInfo.RuleScope);//自定义查询
}
else
{
contidions.Add(new ConditionalModel { FieldName = "CreateBy", ConditionalType = ConditionalType.Equal, FieldValue = user.UserId });
return (queryable.Where(contidions), ruleInfo.RuleScope); //查询创建人自身信息
}
}
else if (ruleInfo.RuleScope == "self_dept")
{
if (String.IsNullOrEmpty(userInfo.DeptName))
{
if (!isTask)
{
SqlSugar.Check.ExceptionEasy("NO Operation", "当前员工没有默认归属部门!");
}
}
var conditionalModels = ConvertConditionalModel(ruleInfo.DataRules);
if (conditionalModels.Count > 0)
{
return (queryable.Where(conditionalModels), ruleInfo.RuleScope);//自定义查询
}
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); //查询本部门
}
}
else if (ruleInfo.RuleScope == "select_org")
{
var conditionalModels = ConvertConditionalModel(ruleInfo.DataRules);
//var conditions = ruleInfo.DataRules.ConvertSqlSugarExpression();
//var conditionalModels =
// db.ConfigQuery.Context.Utilities.JsonToConditionalModels(
// JsonConvert.SerializeObject(conditions));
return (queryable.ClearFilter<IOrgId>().Where(conditionalModels), ruleInfo.RuleScope); //查询指定机构
}
else if (ruleInfo.RuleScope == "select_user")
{
//var conditionalModels = ConvertConditionalModel(ruleInfo.DataRules);
var conditionalModels = ConvertConditionalModel(ruleInfo.DataRules);
return (queryable.ClearFilter<IOrgId>().Where(conditionalModels), ruleInfo.RuleScope);//查询指定人员
}
else if (ruleInfo.RuleScope == "customize")
{
//var conditionalModels = ConvertConditionalModel(ruleInfo.DataRules);
var conditionalModels = ConvertConditionalModel(ruleInfo.DataRules);
return (queryable.Where(conditionalModels), ruleInfo.RuleScope);//自定义查询
}
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, ruleInfo.RuleScope);
#endregion
}
}
private List<IConditionalModel> ConvertConditionalModel(string rules)
{

Loading…
Cancel
Save