feat 数据权限
This commit is contained in:
88
Src/Dlhis.Basic/DataPermissions/GetDataPermissionService.cs
Normal file
88
Src/Dlhis.Basic/DataPermissions/GetDataPermissionService.cs
Normal file
@@ -0,0 +1,88 @@
|
||||
using Dlhis.Entity.Device.Entities;
|
||||
using Dlhis.Entity.Sys.Entities;
|
||||
using Dlhis.Entity.Sys.Enums;
|
||||
using Kurisu.AspNetCore.Authentication.Abstractions;
|
||||
using Kurisu.AspNetCore.DataAccess.Entity;
|
||||
using Kurisu.AspNetCore.DataAccess.SqlSugar.Attributes;
|
||||
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
||||
|
||||
namespace Dlhis.Basic.DataPermissions;
|
||||
|
||||
public class GetDataPermissionService : IGetDataPermissions, ISingletonDependency
|
||||
{
|
||||
private readonly ICurrentUser _currentUser;
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
public GetDataPermissionService(ICurrentUser currentUser, IServiceProvider serviceProvider)
|
||||
{
|
||||
_currentUser = currentUser;
|
||||
_serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
|
||||
public Dictionary<string, List<Guid>> GetData<T>()
|
||||
{
|
||||
var dict = new Dictionary<string, List<Guid>>(2);
|
||||
using var scope = _serviceProvider.CreateScope();
|
||||
|
||||
var db = scope.ServiceProvider.GetService<IDbContext>();
|
||||
db.Ignore<ITenantId>(() =>
|
||||
{
|
||||
var userDataPermissions = db.Queryable<DataPermissionEntity>()
|
||||
.Where(x => x.AssignType == AssignTypeEnumType.User)
|
||||
.Where(x => x.AssignValue == _currentUser.GetStringSubjectId())
|
||||
.ToList();
|
||||
|
||||
var deviceCodeProperty = typeof(T)
|
||||
.GetProperties()
|
||||
.FirstOrDefault(x => x.IsDefined(typeof(DeviceCodeAttribute), false));
|
||||
|
||||
var deviceGroupCodeProperty = typeof(T)
|
||||
.GetProperties()
|
||||
.FirstOrDefault(x => x.IsDefined(typeof(DeviceGroupCodeAttribute), false));
|
||||
|
||||
|
||||
var deviceCodes = new List<Guid>();
|
||||
|
||||
//先计算分组权限下的设备
|
||||
var deviceGroupCodes = userDataPermissions.Where(x => x.UniqueType == DeviceAssignmentTypeEnumType.Group).Select(x => x.UniqueValue).ToList();
|
||||
if (deviceGroupCodes.Any() && deviceCodeProperty != null)
|
||||
{
|
||||
var data = db.Queryable<DeviceGroupEntity>().ToList();
|
||||
//开始递归处理
|
||||
var tops = data.Where(x => !x.PCode.HasValue).ToList();
|
||||
tops.ForEach(x => { DeviceGroupRecursionGetNext(x, data, deviceGroupCodes, deviceGroupCodes.Contains(x.Code)); });
|
||||
deviceGroupCodes = deviceGroupCodes.Distinct().ToList();
|
||||
deviceCodes = db.Queryable<DeviceGroupRelationshipEntity>().Where(x => deviceGroupCodes.Contains(x.GroupCode)).Select(x => x.DeviceCode).ToList();
|
||||
}
|
||||
|
||||
if (deviceGroupCodeProperty != null && deviceGroupCodes.Any())
|
||||
{
|
||||
dict.Add(deviceGroupCodeProperty.Name, deviceGroupCodes);
|
||||
}
|
||||
|
||||
if (deviceCodeProperty != null)
|
||||
{
|
||||
var currentDeviceCodes = userDataPermissions.Where(x => x.UniqueType == DeviceAssignmentTypeEnumType.Device).Select(x => x.UniqueValue).ToList();
|
||||
deviceCodes.AddRange(currentDeviceCodes);
|
||||
dict.Add(deviceCodeProperty.Name, deviceCodes.Distinct().ToList());
|
||||
}
|
||||
});
|
||||
|
||||
return dict;
|
||||
}
|
||||
|
||||
static void DeviceGroupRecursionGetNext(DeviceGroupEntity current, List<DeviceGroupEntity> data, List<Guid> result, bool add)
|
||||
{
|
||||
var next = data.Where(x => x.PCode == current.Code).ToList();
|
||||
foreach (var item in next)
|
||||
{
|
||||
if (add)
|
||||
{
|
||||
result.Add(item.Code);
|
||||
}
|
||||
|
||||
DeviceGroupRecursionGetNext(item, data, result, result.Contains(item.Code));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user