feat 数据权限
This commit is contained in:
@@ -47,7 +47,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dlhis.Message", "Src\Dlhis.
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dlhis.InfoCheck", "Src\Dlhis.InfoCheck\Dlhis.InfoCheck.csproj", "{4D656761-8CE7-4316-A3DF-8C1722D69CCE}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dlhis.Middle", "Src\Dlhis.Middle\Dlhis.Middle.csproj", "{762C944C-36A1-4F1A-803F-08F9BD8891D1}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dlhis.Middle", "Src\Dlhis.Middle\Dlhis.Middle.csproj", "{762C944C-36A1-4F1A-803F-08F9BD8891D1}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dlhis.DataPermission.Model", "Src\Dlhis.DataPermission.Model\Dlhis.DataPermission.Model.csproj", "{CA1427E2-CC70-4489-8653-1C05630C8392}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
@@ -123,6 +125,10 @@ Global
|
||||
{762C944C-36A1-4F1A-803F-08F9BD8891D1}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{762C944C-36A1-4F1A-803F-08F9BD8891D1}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{762C944C-36A1-4F1A-803F-08F9BD8891D1}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{CA1427E2-CC70-4489-8653-1C05630C8392}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{CA1427E2-CC70-4489-8653-1C05630C8392}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{CA1427E2-CC70-4489-8653-1C05630C8392}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{CA1427E2-CC70-4489-8653-1C05630C8392}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,7 +43,7 @@ public class DevicesController : ControllerBase
|
||||
{
|
||||
return await _deviceTemplateService.GetPageAsync(request);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 设备模板保存
|
||||
/// </summary>
|
||||
@@ -90,6 +90,7 @@ public class DevicesController : ControllerBase
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("page")]
|
||||
[DataPermission(true)]
|
||||
public async Task<Pagination<DevicePageResponse>> GetPageAsync([FromQuery] DevicePageRequest request)
|
||||
{
|
||||
return await _deviceService.GetPageAsync(request);
|
||||
|
||||
@@ -71,7 +71,7 @@ public class DeviceService : IDeviceService
|
||||
var registry = await _dbContext.Queryable<DeviceRegistryEntity>().Where(x => x.DeviceUniqueNo == request.UniqueNo).FirstAsync();
|
||||
if (registry == null)
|
||||
{
|
||||
registry = new DeviceRegistryEntity()
|
||||
registry = new DeviceRegistryEntity
|
||||
{
|
||||
DeviceUniqueNo = request.UniqueNo,
|
||||
LastHeartbeatTime = DateTime.Now,
|
||||
|
||||
8
Src/Dlhis.DataPermission.Model/DeviceCodeAttribute.cs
Normal file
8
Src/Dlhis.DataPermission.Model/DeviceCodeAttribute.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using Kurisu.AspNetCore.DataAccess.SqlSugar.Attributes;
|
||||
|
||||
namespace Dlhis.Basic.DataPermissions;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Property)]
|
||||
public class DeviceCodeAttribute : BaseDataPermissionAttribute
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using Kurisu.AspNetCore.DataAccess.SqlSugar.Attributes;
|
||||
|
||||
namespace Dlhis.Basic.DataPermissions;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Property)]
|
||||
public class DeviceGroupCodeAttribute : BaseDataPermissionAttribute
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Kurisu.AspNetCore" Version="0.9.20" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1,4 +1,5 @@
|
||||
using Dlhis.Entity.Device.Enums;
|
||||
using Dlhis.Basic.DataPermissions;
|
||||
using Dlhis.Entity.Device.Enums;
|
||||
using Kurisu.AspNetCore.DataAccess.Entity;
|
||||
using Kurisu.AspNetCore.DataAccess.SqlSugar;
|
||||
using Mapster;
|
||||
@@ -16,6 +17,7 @@ namespace Dlhis.Entity.Basic.Entity
|
||||
/// <summary>
|
||||
/// 设备code
|
||||
/// </summary>
|
||||
[DeviceCode]
|
||||
[SugarColumn(ColumnDescription = "设备code")]
|
||||
public Guid Code { get; set; }
|
||||
|
||||
|
||||
@@ -7,7 +7,11 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Kurisu.AspNetCore" Version="0.9.20" />
|
||||
<ProjectReference Include="..\Dlhis.DataPermission.Model\Dlhis.DataPermission.Model.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Kurisu.AspNetCore" Version="0.9.22" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
17
Src/Dlhis.Entity/Sys/Entities/DataPermissionEntity.cs
Normal file
17
Src/Dlhis.Entity/Sys/Entities/DataPermissionEntity.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using Dlhis.Entity.Sys.Enums;
|
||||
using Kurisu.AspNetCore.DataAccess.SqlSugar;
|
||||
using SqlSugar;
|
||||
|
||||
namespace Dlhis.Entity.Sys.Entities;
|
||||
|
||||
[SugarTable(TableName = "DataPermissions")]
|
||||
public class DataPermissionEntity : SugarBaseEntity
|
||||
{
|
||||
public AssignTypeEnumType AssignType { get; set; }
|
||||
|
||||
public string AssignValue { get; set; }
|
||||
|
||||
public DeviceAssignmentTypeEnumType UniqueType { get; set; }
|
||||
|
||||
public Guid UniqueValue { get; set; }
|
||||
}
|
||||
68
Src/Dlhis.Entity/Visitor/Entity/BusinessTripEntity.cs
Normal file
68
Src/Dlhis.Entity/Visitor/Entity/BusinessTripEntity.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using Kurisu.AspNetCore.DataAccess.SqlSugar;
|
||||
using SqlSugar;
|
||||
|
||||
namespace Dlhis.Entity.Visitor.Entity;
|
||||
|
||||
[SugarTable(TableName = "BusinessTrip")]
|
||||
public class BusinessTripEntity : SugarBaseEntity
|
||||
{
|
||||
/// <summary>
|
||||
/// 工号
|
||||
/// </summary>
|
||||
public string Code { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 姓名
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 开始日期
|
||||
/// </summary>
|
||||
public string BeginDate { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 结束日期
|
||||
/// </summary>
|
||||
public string EndDate { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 开始时间
|
||||
/// </summary>
|
||||
public DateTime BeginDateTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 结束时间
|
||||
/// </summary>
|
||||
public DateTime EndDateTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 出差类型
|
||||
/// </summary>
|
||||
public string Type { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 出差类型描述
|
||||
/// </summary>
|
||||
public string TypeDesc { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 出差时数
|
||||
/// </summary>
|
||||
public int Hour { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 出差天数
|
||||
/// </summary>
|
||||
public double Day { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 出差目的地
|
||||
/// </summary>
|
||||
public string Destination { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 出差目的地描述
|
||||
/// </summary>
|
||||
public string DestinationDesc { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Dlhis.Entity.Sys.Enums;
|
||||
using Dlhis.Sys.DataPermission.Request;
|
||||
using Dlhis.Sys.DataPermission.Services;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Dlhis.Sys.DataPermission.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// 数据权限
|
||||
/// </summary>
|
||||
[Authorize]
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class DataPermissionsController : ControllerBase
|
||||
{
|
||||
private readonly IDataPermissionService _dataPermissionService;
|
||||
|
||||
public DataPermissionsController(IDataPermissionService dataPermissionService)
|
||||
{
|
||||
_dataPermissionService = dataPermissionService;
|
||||
}
|
||||
|
||||
[HttpPost("save")]
|
||||
public async Task SaveAsync(DataPermissionSaveRequest request)
|
||||
{
|
||||
await _dataPermissionService.SaveAsync(request);
|
||||
}
|
||||
|
||||
|
||||
[HttpGet("assignments")]
|
||||
public async Task<List<Guid>> GetAssignmentsAsync(string assignValue, [EnumDataType(typeof(DeviceAssignmentTypeEnumType))] DeviceAssignmentTypeEnumType uniqueType)
|
||||
{
|
||||
return await _dataPermissionService.GetAssignmentsAsync(assignValue, uniqueType);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Dlhis.Entity.Sys.Enums;
|
||||
|
||||
namespace Dlhis.Sys.DataPermission.Request;
|
||||
|
||||
public class DataPermissionSaveRequest : IValidatableObject
|
||||
{
|
||||
[Required]
|
||||
public string AssignValue { get; set; }
|
||||
|
||||
[EnumDataType(typeof(DeviceAssignmentTypeEnumType))]
|
||||
public DeviceAssignmentTypeEnumType UniqueType { get; set; }
|
||||
|
||||
public List<Guid> Codes { get; set; } = new();
|
||||
|
||||
|
||||
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
|
||||
{
|
||||
if (UniqueType == DeviceAssignmentTypeEnumType.BusinessPlace)
|
||||
{
|
||||
yield return new ValidationResult("不支持营业点分配", new[] { nameof(UniqueType) });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using Dlhis.Entity.Sys.Enums;
|
||||
using Dlhis.Sys.DataPermission.Request;
|
||||
|
||||
namespace Dlhis.Sys.DataPermission.Services;
|
||||
|
||||
public interface IDataPermissionService : IScopeDependency
|
||||
{
|
||||
Task SaveAsync(DataPermissionSaveRequest request);
|
||||
|
||||
Task<List<Guid>> GetAssignmentsAsync(string assignValue, DeviceAssignmentTypeEnumType uniqueType);
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using Dlhis.Entity.Sys.Entities;
|
||||
using Dlhis.Entity.Sys.Enums;
|
||||
using Dlhis.Sys.DataPermission.Request;
|
||||
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
||||
|
||||
namespace Dlhis.Sys.DataPermission.Services.Implements;
|
||||
|
||||
public class DataPermissionService : IDataPermissionService
|
||||
{
|
||||
private readonly IDbContext _dbContext;
|
||||
|
||||
public DataPermissionService(IDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task SaveAsync(DataPermissionSaveRequest request)
|
||||
{
|
||||
var data = await _dbContext.Queryable<DataPermissionEntity>()
|
||||
.Where(x => x.AssignValue == request.AssignValue)
|
||||
.Where(x => x.AssignType == AssignTypeEnumType.User)
|
||||
.Where(x => x.UniqueType == request.UniqueType)
|
||||
.ToListAsync();
|
||||
|
||||
await _dbContext.DeleteReallyAsync(data);
|
||||
|
||||
await _dbContext.InsertAsync(request.Codes.Select(x => new DataPermissionEntity
|
||||
{
|
||||
AssignType = AssignTypeEnumType.User,
|
||||
AssignValue = request.AssignValue,
|
||||
UniqueType = request.UniqueType,
|
||||
UniqueValue = x
|
||||
}).ToList());
|
||||
}
|
||||
|
||||
public async Task<List<Guid>> GetAssignmentsAsync(string assignValue, DeviceAssignmentTypeEnumType uniqueType)
|
||||
{
|
||||
return await _dbContext.Queryable<DataPermissionEntity>()
|
||||
.Where(x => x.AssignValue == assignValue)
|
||||
.Where(x => x.AssignType == AssignTypeEnumType.User)
|
||||
.Where(x => x.UniqueType == uniqueType)
|
||||
.Select(x => x.UniqueValue)
|
||||
.ToListAsync();
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="DataPermission\Response\" />
|
||||
<Folder Include="Server\Request\" />
|
||||
<Folder Include="Server\Response\" />
|
||||
<Folder Include="Server\Services\Implements\" />
|
||||
|
||||
Reference in New Issue
Block a user