add 数据权限
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
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.ScheduledTask.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 deviceCodes = new List<Guid>();
|
||||
|
||||
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 IDataPermissionService : IScopeDependency
|
||||
{
|
||||
(bool, List<Guid>) GetMyDeviceCodes();
|
||||
(bool, List<Guid>) GetMyDeviceGroupCodes();
|
||||
}
|
||||
|
||||
public class MyDataPermissionService : IDataPermissionService
|
||||
{
|
||||
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 hasAllPermission = _currentUser.GetRole() is "admin" or "tenant-admin";
|
||||
|
||||
if (!hasAllPermission)
|
||||
{
|
||||
codes = _getDataPermissions.GetData<DeviceEntity>().FirstOrDefault().Value ?? new List<Guid>();
|
||||
}
|
||||
|
||||
return (!hasAllPermission, codes);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public (bool, List<Guid>) GetMyDeviceGroupCodes()
|
||||
{
|
||||
var codes = new List<Guid>();
|
||||
|
||||
var hasAllPermission = _currentUser.GetRole() is "admin" or "tenant-admin";
|
||||
if (!hasAllPermission)
|
||||
{
|
||||
codes = _getDataPermissions.GetData<DeviceGroupEntity>().FirstOrDefault().Value ?? new List<Guid>();
|
||||
}
|
||||
|
||||
return (!hasAllPermission, codes);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user