|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using MediatR;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using X1.Domain.Entities;
|
|
|
|
|
using X1.Domain.Entities.Enums;
|
|
|
|
|
using X1.Domain.Repositories.Identity;
|
|
|
|
|
using X1.Domain.Common;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace X1.Application.Features.Permissions.Queries;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取所有权限查询处理器
|
|
|
|
|
/// </summary>
|
|
|
|
|
public sealed class GetAllPermissionsQueryHandler : IRequestHandler<GetAllPermissionsQuery, OperationResult<GetAllPermissionsResponse>>
|
|
|
|
|
{
|
|
|
|
|
private readonly IPermissionRepository _permissionRepository;
|
|
|
|
|
private readonly ILogger<GetAllPermissionsQueryHandler> _logger;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 初始化处理器
|
|
|
|
|
/// </summary>
|
|
|
|
|
public GetAllPermissionsQueryHandler(
|
|
|
|
|
IPermissionRepository permissionRepository,
|
|
|
|
|
ILogger<GetAllPermissionsQueryHandler> logger)
|
|
|
|
|
{
|
|
|
|
|
_permissionRepository = permissionRepository;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 处理获取所有权限查询
|
|
|
|
|
/// </summary>
|
|
|
|
|
public async Task<OperationResult<GetAllPermissionsResponse>> Handle(
|
|
|
|
|
GetAllPermissionsQuery request,
|
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("开始获取权限列表,页码: {PageNumber}, 页大小: {PageSize}", request.PageNumber, request.PageSize);
|
|
|
|
|
|
|
|
|
|
var permissions = await _permissionRepository.GetAllAsync();
|
|
|
|
|
|
|
|
|
|
if (!permissions.Any())
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("未找到任何权限");
|
|
|
|
|
return OperationResult<GetAllPermissionsResponse>.CreateSuccess(
|
|
|
|
|
new GetAllPermissionsResponse(
|
|
|
|
|
new List<PermissionListItemDto>(),
|
|
|
|
|
0,
|
|
|
|
|
request.PageNumber,
|
|
|
|
|
request.PageSize
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 应用筛选条件
|
|
|
|
|
var filteredPermissions = permissions.AsQueryable();
|
|
|
|
|
|
|
|
|
|
// 关键词搜索
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(request.Keyword))
|
|
|
|
|
{
|
|
|
|
|
filteredPermissions = filteredPermissions.Where(p =>
|
|
|
|
|
p.Name.Contains(request.Keyword) ||
|
|
|
|
|
p.Code.Contains(request.Keyword) ||
|
|
|
|
|
(p.Description != null && p.Description.Contains(request.Keyword))
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 类型筛选
|
|
|
|
|
if (request.Type.HasValue)
|
|
|
|
|
{
|
|
|
|
|
// 这里需要根据实际的Permission实体字段来筛选
|
|
|
|
|
// 暂时跳过类型筛选
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 资源类型筛选
|
|
|
|
|
if (request.ResourceType.HasValue)
|
|
|
|
|
{
|
|
|
|
|
// 这里需要根据实际的Permission实体字段来筛选
|
|
|
|
|
// 暂时跳过资源类型筛选
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 操作类型筛选
|
|
|
|
|
if (request.ActionType.HasValue)
|
|
|
|
|
{
|
|
|
|
|
// 这里需要根据实际的Permission实体字段来筛选
|
|
|
|
|
// 暂时跳过操作类型筛选
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 级别筛选
|
|
|
|
|
if (request.Level.HasValue)
|
|
|
|
|
{
|
|
|
|
|
// 这里需要根据实际的Permission实体字段来筛选
|
|
|
|
|
// 暂时跳过级别筛选
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 启用状态筛选
|
|
|
|
|
if (request.IsEnabled.HasValue)
|
|
|
|
|
{
|
|
|
|
|
filteredPermissions = filteredPermissions.Where(p => p.IsEnabled == request.IsEnabled.Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 系统权限筛选
|
|
|
|
|
if (request.IsSystem.HasValue)
|
|
|
|
|
{
|
|
|
|
|
filteredPermissions = filteredPermissions.Where(p => p.IsSystem == request.IsSystem.Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取总数
|
|
|
|
|
var totalCount = filteredPermissions.Count();
|
|
|
|
|
|
|
|
|
|
// 分页
|
|
|
|
|
var pagedPermissions = filteredPermissions
|
|
|
|
|
.Skip((request.PageNumber - 1) * request.PageSize)
|
|
|
|
|
.Take(request.PageSize)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
// 转换为DTO
|
|
|
|
|
var permissionDtos = pagedPermissions.Select(p => new PermissionListItemDto(
|
|
|
|
|
p.Id,
|
|
|
|
|
p.Name,
|
|
|
|
|
p.Code,
|
|
|
|
|
p.Description,
|
|
|
|
|
1, // 默认Type为Resource
|
|
|
|
|
1, // 默认Level为View
|
|
|
|
|
GetResourceTypeValue(p.ExtractResourceType() ?? "unknown"),
|
|
|
|
|
1, // 默认ActionType为View
|
|
|
|
|
p.IsEnabled,
|
|
|
|
|
p.IsSystem,
|
|
|
|
|
0 // 默认SortOrder
|
|
|
|
|
)).ToList();
|
|
|
|
|
|
|
|
|
|
var response = new GetAllPermissionsResponse(
|
|
|
|
|
permissionDtos,
|
|
|
|
|
totalCount,
|
|
|
|
|
request.PageNumber,
|
|
|
|
|
request.PageSize
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("权限列表获取成功,共 {TotalCount} 条记录,当前页 {PageNumber}/{TotalPages}",
|
|
|
|
|
totalCount, request.PageNumber, response.TotalPages);
|
|
|
|
|
|
|
|
|
|
return OperationResult<GetAllPermissionsResponse>.CreateSuccess(response);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "获取权限列表时发生错误");
|
|
|
|
|
return OperationResult<GetAllPermissionsResponse>.CreateFailure(
|
|
|
|
|
new List<string> { "获取权限列表失败" });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取资源类型数值
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static int GetResourceTypeValue(string resourceType)
|
|
|
|
|
{
|
|
|
|
|
return resourceType.ToLower() switch
|
|
|
|
|
{
|
|
|
|
|
"dashboard" => 1,
|
|
|
|
|
"users" => 2,
|
|
|
|
|
"roles" => 3,
|
|
|
|
|
"permissions" => 4,
|
|
|
|
|
"settings" => 5,
|
|
|
|
|
"scenarios" => 6,
|
|
|
|
|
"testcases" => 7,
|
|
|
|
|
"teststeps" => 8,
|
|
|
|
|
"tasks" => 9,
|
|
|
|
|
"taskreviews" => 10,
|
|
|
|
|
"taskexecutions" => 11,
|
|
|
|
|
"functionalanalysis" => 12,
|
|
|
|
|
"performanceanalysis" => 13,
|
|
|
|
|
"issueanalysis" => 14,
|
|
|
|
|
"ueanalysis" => 15,
|
|
|
|
|
"devices" => 16,
|
|
|
|
|
"protocols" => 17,
|
|
|
|
|
"ranconfigurations" => 18,
|
|
|
|
|
"imsconfigurations" => 19,
|
|
|
|
|
"corenetworkconfigs" => 20,
|
|
|
|
|
"networkstackconfigs" => 21,
|
|
|
|
|
"deviceruntimes" => 22,
|
|
|
|
|
"protocollogs" => 23,
|
|
|
|
|
"terminalservices" => 24,
|
|
|
|
|
"terminaldevices" => 25,
|
|
|
|
|
"adboperations" => 26,
|
|
|
|
|
"atoperations" => 27,
|
|
|
|
|
_ => 0
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|