135 lines
4.7 KiB
C#
135 lines
4.7 KiB
C#
using Dlhis.DataPermission.Model;
|
|
using Dlhis.Entity.Basic.Entity;
|
|
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);
|
|
var currentRole = _currentUser.GetRole();
|
|
if (currentRole is "admin" or "tenant-admin")
|
|
{
|
|
return dict;
|
|
}
|
|
|
|
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())
|
|
{
|
|
var data = db.Queryable<DeviceGroupEntity>().ToList();
|
|
|
|
var myDeviceGroupCodes = new List<Guid>();
|
|
foreach (Guid deviceGroupCode in deviceGroupCodes)
|
|
{
|
|
myDeviceGroupCodes.AddRange(RecursionHelper.HandlerDeviceGroupNextCodes(deviceGroupCode, data));
|
|
}
|
|
|
|
myDeviceGroupCodes = myDeviceGroupCodes.Distinct().ToList();
|
|
|
|
if (deviceCodeProperty != null)
|
|
{
|
|
deviceCodes = db.Queryable<DeviceGroupRelationshipEntity>().Where(x => myDeviceGroupCodes.Contains(x.GroupCode)).Select(x => x.DeviceCode).ToList();
|
|
}
|
|
|
|
if (deviceGroupCodeProperty != null)
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
|
|
public interface IMyDataPermissionService : IScopeDependency
|
|
{
|
|
(bool, List<Guid>) GetMyDeviceCodes();
|
|
(bool, List<Guid>) GetMyDeviceGroupCodes();
|
|
}
|
|
|
|
public class MyDataPermissionService : IMyDataPermissionService
|
|
{
|
|
private readonly IGetDataPermissions _getDataPermissions;
|
|
private readonly ICurrentUser _currentUser;
|
|
|
|
public MyDataPermissionService(IGetDataPermissions getDataPermissions, ICurrentUser currentUser)
|
|
{
|
|
_getDataPermissions = getDataPermissions;
|
|
_currentUser = currentUser;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public (bool, List<Guid>) GetMyDeviceCodes()
|
|
{
|
|
var codes = new List<Guid>();
|
|
|
|
var disableFilter = _currentUser.GetRole() is "admin" or "tenant-admin";
|
|
if (!disableFilter)
|
|
{
|
|
codes = _getDataPermissions.GetData<DeviceEntity>().FirstOrDefault().Value ?? new List<Guid>();
|
|
}
|
|
|
|
return (!disableFilter, codes);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public (bool, List<Guid>) GetMyDeviceGroupCodes()
|
|
{
|
|
var codes = new List<Guid>();
|
|
|
|
var disableFilter = _currentUser.GetRole() is "admin" or "tenant-admin";
|
|
if (!disableFilter)
|
|
{
|
|
codes = _getDataPermissions.GetData<DeviceGroupEntity>().FirstOrDefault().Value ?? new List<Guid>();
|
|
}
|
|
|
|
return (!disableFilter, codes);
|
|
}
|
|
} |