fix 权限分配优化按部门
This commit is contained in:
@@ -110,9 +110,9 @@ public class DevicePermissionService : IDevicePermissionService
|
||||
if (request.Values.IsPresent())
|
||||
{
|
||||
var gainfoNos = await _dbContext.Queryable<GainfoEntity>()
|
||||
.Where(x => request.Values.Contains(x.Code) || request.Values.Contains(x.No))
|
||||
.Select(x => x.No)
|
||||
.ToListAsync();
|
||||
.Where(x => request.Values.Contains(x.Code) || request.Values.Contains(x.No))
|
||||
.Select(x => x.No)
|
||||
.ToListAsync();
|
||||
|
||||
(gainfoNos.Count != request.Values.Count).ThrowIfTrue("存在非当前权限访问的人员,操作禁止!");
|
||||
|
||||
@@ -177,13 +177,15 @@ public class DevicePermissionService : IDevicePermissionService
|
||||
|
||||
public async Task<List<Guid>> GetAssignmentsAsync(DeviceGetAssignmentsRequest request)
|
||||
{
|
||||
var devicePermissions = await _dbContext.Queryable<DevicePermissionEntity>()
|
||||
return await _dbContext.Queryable<DevicePermissionEntity>()
|
||||
.WhereIF(request.TenantId.IsPresent(), x => x.TenantId == request.TenantId)
|
||||
.Select(x => x.UniqueValue)
|
||||
.ToListAsync();
|
||||
var deviceGroups = await _dbContext.Queryable<DeviceGroupEntity>().ToListAsync();
|
||||
|
||||
var result = GetAssignCodesInternal(devicePermissions, deviceGroups, request.UniqueType, request.AssignType, request.AssignValue);
|
||||
return result.FirstOrDefault(x => x.Tenant == _currentUser.GetStringTenantId())?.Codes;
|
||||
// var deviceGroups = await _dbContext.Queryable<DeviceGroupEntity>().ToListAsync();
|
||||
//
|
||||
// var result = GetAssignCodesInternal(devicePermissions, deviceGroups, request.UniqueType, request.AssignType, request.AssignValue);
|
||||
// return result.FirstOrDefault(x => x.Tenant == _currentUser.GetStringTenantId())?.Codes;
|
||||
}
|
||||
|
||||
|
||||
@@ -301,6 +303,7 @@ public class DevicePermissionService : IDevicePermissionService
|
||||
{
|
||||
await _dbContext.InsertAsync(waitAddTasks);
|
||||
}
|
||||
|
||||
if (waitDeleteTasks.Any())
|
||||
{
|
||||
await _dbContext.InsertAsync(waitDeleteTasks);
|
||||
@@ -420,23 +423,23 @@ public class DevicePermissionService : IDevicePermissionService
|
||||
ids.Add(await _dbContext.Queryable<GainfoEntity>().Where(x => x.No == permission.AssignValue).Select(x => x.Id).SingleAsync());
|
||||
break;
|
||||
case AssignTypeEnumType.Group:
|
||||
{
|
||||
var code = Guid.Parse(permission.AssignValue);
|
||||
ids.AddRange(await _dbContext.Queryable<GainfoGroupEntity>().Where(x => x.Code == code)
|
||||
.LeftJoin<GainfoGroupRelationshipEntity>((a, b) => a.Code == b.GroupCode)
|
||||
.LeftJoin<GainfoEntity>((a, b, c) => b.UserNo == c.No)
|
||||
.Select((a, b, c) => c.Id).ToListAsync());
|
||||
}
|
||||
{
|
||||
var code = Guid.Parse(permission.AssignValue);
|
||||
ids.AddRange(await _dbContext.Queryable<GainfoGroupEntity>().Where(x => x.Code == code)
|
||||
.LeftJoin<GainfoGroupRelationshipEntity>((a, b) => a.Code == b.GroupCode)
|
||||
.LeftJoin<GainfoEntity>((a, b, c) => b.UserNo == c.No)
|
||||
.Select((a, b, c) => c.Id).ToListAsync());
|
||||
}
|
||||
break;
|
||||
case AssignTypeEnumType.Department:
|
||||
{
|
||||
ids.AddRange(await _dbContext.Queryable<GainfoEntity>().Where(x => x.DepartmentNo == permission.AssignValue).Select(x => x.Id).ToListAsync());
|
||||
}
|
||||
{
|
||||
ids.AddRange(await _dbContext.Queryable<GainfoEntity>().Where(x => x.DepartmentNo == permission.AssignValue).Select(x => x.Id).ToListAsync());
|
||||
}
|
||||
break;
|
||||
case AssignTypeEnumType.Tenant:
|
||||
{
|
||||
ids.AddRange(await _dbContext.Queryable<GainfoEntity>().Where(x => x.Factory == permission.AssignValue).Select(x => x.Id).ToListAsync());
|
||||
}
|
||||
{
|
||||
ids.AddRange(await _dbContext.Queryable<GainfoEntity>().Where(x => x.Factory == permission.AssignValue).Select(x => x.Id).ToListAsync());
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
@@ -506,8 +509,9 @@ public class DevicePermissionService : IDevicePermissionService
|
||||
var deviceGroups = await _dbContext.Queryable<DeviceGroupEntity>().ToListAsync();
|
||||
var deviceGroupCodes = deviceGroups.Select(x => x.Code).ToList();
|
||||
var deviceRelationships = await _dbContext.Queryable<DeviceGroupRelationshipEntity>().Where(x => deviceGroupCodes.Contains(x.GroupCode)).ToListAsync();
|
||||
var departments = await _dbContext.Queryable<DepartmentEntity>().ToListAsync();
|
||||
|
||||
var result = GetAssignDeviceCodesInternal(gainfo, gainfoGroups, devicePermissions, deviceGroups, deviceRelationships, deviceRegistries, devices);
|
||||
var result = GetAssignDeviceCodesInternal(gainfo, gainfoGroups, devicePermissions, deviceGroups, deviceRelationships, deviceRegistries, devices, departments);
|
||||
return result.SelectMany(x => x.Codes).ToList();
|
||||
}
|
||||
|
||||
@@ -531,18 +535,46 @@ public class DevicePermissionService : IDevicePermissionService
|
||||
.ToListAsync();
|
||||
|
||||
//Department
|
||||
var departmentNos = permissions.Where(x => x.AssignType == AssignTypeEnumType.Department).Select(x => x.AssignValue).ToList();
|
||||
var userNosByDepartment = await _dbContext.Queryable<GainfoEntity>().Where(x => departmentNos.Contains(x.DepartmentNo)).Select(x => x.No).ToListAsync();
|
||||
var tenantDepartmentNos = permissions.Where(x => x.AssignType == AssignTypeEnumType.Department).ToDictionary(x => x.TenantId, x => new List<string> { x.AssignValue });
|
||||
var departments = await _dbContext.Queryable<DepartmentEntity>().ToListAsync();
|
||||
|
||||
var gainfoByDeptWhere = LinqExpressionExtensions.False<GainfoEntity>();
|
||||
foreach ((string factory, List<string> departmentNos) in tenantDepartmentNos)
|
||||
{
|
||||
var matchDepartments = departmentNos.ToHashSet();
|
||||
departmentNos.ForEach(x => RecursionGetNextAllDepartment(x, departments, matchDepartments));
|
||||
|
||||
var departmentNosToSearch = matchDepartments.ToList();
|
||||
|
||||
gainfoByDeptWhere = gainfoByDeptWhere.Or(x => x.Factory == factory && departmentNosToSearch.Contains(x.DepartmentNo));
|
||||
}
|
||||
|
||||
var userNosByDepartment = await _dbContext.Queryable<GainfoEntity>()
|
||||
.Where(gainfoByDeptWhere)
|
||||
.Select(x => x.No)
|
||||
.ToListAsync();
|
||||
|
||||
|
||||
//Tenant
|
||||
var tenants = permissions.Where(x => x.AssignType == AssignTypeEnumType.Tenant).Select(x => x.AssignValue).Distinct().ToList();
|
||||
var userNosByWerk = await _dbContext.Queryable<GainfoEntity>().Where(x => tenants.Contains(x.Factory)).Select(x => x.No).ToListAsync();
|
||||
|
||||
return userNos.Concat(userNosByGroup).Concat(userNosByDepartment).Concat(userNosByWerk).Distinct().ToList();
|
||||
|
||||
|
||||
void RecursionGetNextAllDepartment(string current, List<DepartmentEntity> data, HashSet<string> result)
|
||||
{
|
||||
var next = data.Where(x => x.PNo == current);
|
||||
foreach (var item in next.Select(x => x.No))
|
||||
{
|
||||
result.Add(item);
|
||||
RecursionGetNextAllDepartment(item, data, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static List<UserTenantDeviceCodeDto> GetAssignCodesInternal(
|
||||
List<DevicePermissionEntity> devicePermissions, List<DeviceGroupEntity> deviceGroups,
|
||||
List<DevicePermissionEntity> devicePermissions, List<DeviceGroupEntity> deviceGroups, List<DepartmentEntity> departments,
|
||||
DeviceAssignmentTypeEnumType uniqueType, AssignTypeEnumType assignType, string assignValue)
|
||||
{
|
||||
var tenantDevicePermissions = devicePermissions
|
||||
@@ -558,7 +590,22 @@ public class DevicePermissionService : IDevicePermissionService
|
||||
{
|
||||
//开始递归处理
|
||||
var tops = deviceGroups.Where(x => !x.PCode.HasValue).ToList();
|
||||
tops.ForEach(x => { RecursionGetNext(x, deviceGroups, tenantDevicePermissions, tenantDevicePermissions.SelectMany(item => item.Codes).Contains(x.Code)); });
|
||||
tops.ForEach(x => { DeviceGroupRecursionGetNext(x, deviceGroups, tenantDevicePermissions, tenantDevicePermissions.SelectMany(item => item.Codes).Contains(x.Code)); });
|
||||
}
|
||||
|
||||
if (assignType == AssignTypeEnumType.Department)
|
||||
{
|
||||
//递归向上获取 assignValue = department.No
|
||||
var departmentNos = new List<string>();
|
||||
DepartmentRecursionGetPNo(assignValue, departments, departmentNos);
|
||||
|
||||
tenantDevicePermissions = devicePermissions
|
||||
.Where(x => x.UniqueType == uniqueType)
|
||||
.Where(x => x.AssignType == assignType)
|
||||
.Where(x => departmentNos.Contains(x.AssignValue))
|
||||
.GroupBy(x => x.TenantId)
|
||||
.Select(x => new UserTenantDeviceCodeDto { Tenant = x.Key, Codes = x.Select(p => p.UniqueValue).ToList() })
|
||||
.ToList();
|
||||
}
|
||||
|
||||
return tenantDevicePermissions.GroupBy(x => x.Tenant).Select(x =>
|
||||
@@ -572,9 +619,9 @@ public class DevicePermissionService : IDevicePermissionService
|
||||
};
|
||||
}).ToList();
|
||||
|
||||
void RecursionGetNext(DeviceGroupEntity current, List<DeviceGroupEntity> deviceGroups, List<UserTenantDeviceCodeDto> result, bool add)
|
||||
void DeviceGroupRecursionGetNext(DeviceGroupEntity current, List<DeviceGroupEntity> currentDeviceGroups, List<UserTenantDeviceCodeDto> result, bool add)
|
||||
{
|
||||
var next = deviceGroups.Where(x => x.PCode == current.Code).ToList();
|
||||
var next = currentDeviceGroups.Where(x => x.PCode == current.Code).ToList();
|
||||
foreach (var item in next)
|
||||
{
|
||||
if (add)
|
||||
@@ -582,9 +629,21 @@ public class DevicePermissionService : IDevicePermissionService
|
||||
result.Add(new UserTenantDeviceCodeDto { Tenant = item.TenantId, Codes = new List<Guid> { item.Code } });
|
||||
}
|
||||
|
||||
RecursionGetNext(item, deviceGroups, result, result.SelectMany(x => x.Codes).Contains(item.Code));
|
||||
DeviceGroupRecursionGetNext(item, currentDeviceGroups, result, result.SelectMany(x => x.Codes).Contains(item.Code));
|
||||
}
|
||||
}
|
||||
|
||||
void DepartmentRecursionGetPNo(string current, List<DepartmentEntity> data, List<string> result)
|
||||
{
|
||||
var p = data.FirstOrDefault(x => x.No == current)?.PNo;
|
||||
if (!p.IsPresent())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
result.Add(p);
|
||||
DepartmentRecursionGetPNo(p, data, result);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<Dictionary<string, List<UserTenantDeviceCodeDto>>> GetAssignDeviceCodesInternalAsync(List<string> userNos)
|
||||
@@ -606,11 +665,12 @@ public class DevicePermissionService : IDevicePermissionService
|
||||
var devicePermissions = await _dbContext.Queryable<DevicePermissionEntity>().ToListAsync();
|
||||
var deviceGroups = await _dbContext.Queryable<DeviceGroupEntity>().ToListAsync();
|
||||
var deviceRelationships = await _dbContext.Queryable<DeviceGroupRelationshipEntity>().ToListAsync();
|
||||
var departments = await _dbContext.Queryable<DepartmentEntity>().ToListAsync();
|
||||
|
||||
var result = new Dictionary<string, List<UserTenantDeviceCodeDto>>(gainfos.Count, StringComparer.OrdinalIgnoreCase);
|
||||
foreach (var gainfo in gainfos)
|
||||
{
|
||||
var codes = GetAssignDeviceCodesInternal(gainfo, gainfoGroupRelationships, devicePermissions, deviceGroups, deviceRelationships, registries, devices);
|
||||
var codes = GetAssignDeviceCodesInternal(gainfo, gainfoGroupRelationships, devicePermissions, deviceGroups, deviceRelationships, registries, devices, departments);
|
||||
result.Add(gainfo.No, codes);
|
||||
}
|
||||
|
||||
@@ -624,8 +684,7 @@ public class DevicePermissionService : IDevicePermissionService
|
||||
List<DeviceGroupEntity> deviceGroups,
|
||||
List<DeviceGroupRelationshipEntity> deviceGroupRelationships,
|
||||
List<DeviceRegistryEntity> deviceRegistries,
|
||||
List<DeviceEntity> devices
|
||||
)
|
||||
List<DeviceEntity> devices, List<DepartmentEntity> departments)
|
||||
{
|
||||
var userNo = gainfo.No;
|
||||
var deviceDtos = new List<UserTenantDeviceCodeDto>();
|
||||
@@ -636,9 +695,9 @@ public class DevicePermissionService : IDevicePermissionService
|
||||
var gainfoGroups = gainfoGroupRelationships.Where(x => x.UserNo == userNo).Select(x => x.GroupCode).ToList();
|
||||
|
||||
//按用户
|
||||
var du = GetAssignCodesInternal(devicePermissions, deviceGroups, DeviceAssignmentTypeEnumType.Device, AssignTypeEnumType.User, userNo);
|
||||
var gu = GetAssignCodesInternal(devicePermissions, deviceGroups, DeviceAssignmentTypeEnumType.Group, AssignTypeEnumType.User, userNo);
|
||||
var pu = GetAssignCodesInternal(devicePermissions, deviceGroups, DeviceAssignmentTypeEnumType.BusinessPlace, AssignTypeEnumType.User, userNo);
|
||||
var du = GetAssignCodesInternal(devicePermissions, deviceGroups, departments, DeviceAssignmentTypeEnumType.Device, AssignTypeEnumType.User, userNo);
|
||||
var gu = GetAssignCodesInternal(devicePermissions, deviceGroups, departments, DeviceAssignmentTypeEnumType.Group, AssignTypeEnumType.User, userNo);
|
||||
var pu = GetAssignCodesInternal(devicePermissions, deviceGroups, departments, DeviceAssignmentTypeEnumType.BusinessPlace, AssignTypeEnumType.User, userNo);
|
||||
|
||||
deviceDtos.AddRange(du);
|
||||
deviceGroupDtos.AddRange(gu);
|
||||
@@ -647,9 +706,9 @@ public class DevicePermissionService : IDevicePermissionService
|
||||
//按用户组
|
||||
foreach (var groupCode in gainfoGroups)
|
||||
{
|
||||
var dg = GetAssignCodesInternal(devicePermissions, deviceGroups, DeviceAssignmentTypeEnumType.Device, AssignTypeEnumType.Group, groupCode.ToString());
|
||||
var gg = GetAssignCodesInternal(devicePermissions, deviceGroups, DeviceAssignmentTypeEnumType.Group, AssignTypeEnumType.Group, groupCode.ToString());
|
||||
var pg = GetAssignCodesInternal(devicePermissions, deviceGroups, DeviceAssignmentTypeEnumType.BusinessPlace, AssignTypeEnumType.Group, groupCode.ToString());
|
||||
var dg = GetAssignCodesInternal(devicePermissions, deviceGroups, departments, DeviceAssignmentTypeEnumType.Device, AssignTypeEnumType.Group, groupCode.ToString());
|
||||
var gg = GetAssignCodesInternal(devicePermissions, deviceGroups, departments, DeviceAssignmentTypeEnumType.Group, AssignTypeEnumType.Group, groupCode.ToString());
|
||||
var pg = GetAssignCodesInternal(devicePermissions, deviceGroups, departments, DeviceAssignmentTypeEnumType.BusinessPlace, AssignTypeEnumType.Group, groupCode.ToString());
|
||||
deviceDtos.AddRange(dg);
|
||||
deviceGroupDtos.AddRange(gg);
|
||||
businessPlaceDtos.AddRange(pg);
|
||||
@@ -657,17 +716,17 @@ public class DevicePermissionService : IDevicePermissionService
|
||||
|
||||
//按部门
|
||||
var gainfoFactoryDevicePermissions = devicePermissions.Where(x => x.TenantId == gainfo.Factory).ToList();
|
||||
var dd = GetAssignCodesInternal(gainfoFactoryDevicePermissions, deviceGroups, DeviceAssignmentTypeEnumType.Device, AssignTypeEnumType.Department, gainfo.DepartmentNo);
|
||||
var gd = GetAssignCodesInternal(gainfoFactoryDevicePermissions, deviceGroups, DeviceAssignmentTypeEnumType.Group, AssignTypeEnumType.Department, gainfo.DepartmentNo);
|
||||
var pd = GetAssignCodesInternal(gainfoFactoryDevicePermissions, deviceGroups, DeviceAssignmentTypeEnumType.BusinessPlace, AssignTypeEnumType.Department, gainfo.DepartmentNo);
|
||||
var dd = GetAssignCodesInternal(gainfoFactoryDevicePermissions, deviceGroups, departments, DeviceAssignmentTypeEnumType.Device, AssignTypeEnumType.Department, gainfo.DepartmentNo);
|
||||
var gd = GetAssignCodesInternal(gainfoFactoryDevicePermissions, deviceGroups, departments, DeviceAssignmentTypeEnumType.Group, AssignTypeEnumType.Department, gainfo.DepartmentNo);
|
||||
var pd = GetAssignCodesInternal(gainfoFactoryDevicePermissions, deviceGroups, departments, DeviceAssignmentTypeEnumType.BusinessPlace, AssignTypeEnumType.Department, gainfo.DepartmentNo);
|
||||
deviceDtos.AddRange(dd);
|
||||
deviceGroupDtos.AddRange(gd);
|
||||
businessPlaceDtos.AddRange(pd);
|
||||
|
||||
//按厂区
|
||||
var dt = GetAssignCodesInternal(devicePermissions, deviceGroups, DeviceAssignmentTypeEnumType.Device, AssignTypeEnumType.Tenant, gainfo.Factory);
|
||||
var gt = GetAssignCodesInternal(devicePermissions, deviceGroups, DeviceAssignmentTypeEnumType.Group, AssignTypeEnumType.Tenant, gainfo.Factory);
|
||||
var pt = GetAssignCodesInternal(devicePermissions, deviceGroups, DeviceAssignmentTypeEnumType.BusinessPlace, AssignTypeEnumType.Tenant, gainfo.Factory);
|
||||
var dt = GetAssignCodesInternal(devicePermissions, deviceGroups, departments, DeviceAssignmentTypeEnumType.Device, AssignTypeEnumType.Tenant, gainfo.Factory);
|
||||
var gt = GetAssignCodesInternal(devicePermissions, deviceGroups, departments, DeviceAssignmentTypeEnumType.Group, AssignTypeEnumType.Tenant, gainfo.Factory);
|
||||
var pt = GetAssignCodesInternal(devicePermissions, deviceGroups, departments, DeviceAssignmentTypeEnumType.BusinessPlace, AssignTypeEnumType.Tenant, gainfo.Factory);
|
||||
deviceDtos.AddRange(dt);
|
||||
deviceGroupDtos.AddRange(gt);
|
||||
businessPlaceDtos.AddRange(pt);
|
||||
@@ -675,7 +734,8 @@ public class DevicePermissionService : IDevicePermissionService
|
||||
var deviceGroupCodes = deviceGroupDtos.SelectMany(x => x.Codes).Distinct().ToList();
|
||||
var businessPlaceCodes = businessPlaceDtos.SelectMany(x => x.Codes).Distinct().ToList();
|
||||
|
||||
var deviceCodesByGroup = deviceGroupRelationships.Where(x => deviceGroupCodes.Contains(x.GroupCode)).GroupBy(x => x.TenantId)
|
||||
var deviceCodesByGroup = deviceGroupRelationships.Where(x => deviceGroupCodes.Contains(x.GroupCode))
|
||||
.GroupBy(x => x.TenantId)
|
||||
.Select(x => new UserTenantDeviceCodeDto
|
||||
{
|
||||
Tenant = x.Key,
|
||||
|
||||
Reference in New Issue
Block a user