You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
2.6 KiB
76 lines
2.6 KiB
|
4 months ago
|
using System;
|
||
|
|
using System.Threading;
|
||
|
|
using System.Threading.Tasks;
|
||
|
|
using MediatR;
|
||
|
|
using Microsoft.Extensions.Logging;
|
||
|
|
using X1.Domain.Entities;
|
||
|
|
using X1.Domain.Repositories.Identity;
|
||
|
|
using X1.Domain.Common;
|
||
|
|
|
||
|
|
namespace X1.Application.Features.Permissions.Commands.UpdatePermission;
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 更新权限命令处理器
|
||
|
|
/// </summary>
|
||
|
|
public sealed class UpdatePermissionCommandHandler : IRequestHandler<UpdatePermissionCommand, OperationResult<UpdatePermissionResponse>>
|
||
|
|
{
|
||
|
|
private readonly IPermissionRepository _permissionRepository;
|
||
|
|
private readonly ILogger<UpdatePermissionCommandHandler> _logger;
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 初始化处理器
|
||
|
|
/// </summary>
|
||
|
|
public UpdatePermissionCommandHandler(
|
||
|
|
IPermissionRepository permissionRepository,
|
||
|
|
ILogger<UpdatePermissionCommandHandler> logger)
|
||
|
|
{
|
||
|
|
_permissionRepository = permissionRepository;
|
||
|
|
_logger = logger;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 处理更新权限请求
|
||
|
|
/// </summary>
|
||
|
|
public async Task<OperationResult<UpdatePermissionResponse>> Handle(
|
||
|
|
UpdatePermissionCommand request,
|
||
|
|
CancellationToken cancellationToken)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
// 查找权限
|
||
|
|
var permission = await _permissionRepository.GetByIdAsync(request.Id, cancellationToken);
|
||
|
|
if (permission == null)
|
||
|
|
{
|
||
|
|
_logger.LogWarning("权限 {PermissionId} 不存在", request.Id);
|
||
|
|
return OperationResult<UpdatePermissionResponse>.CreateFailure("权限不存在");
|
||
|
|
}
|
||
|
|
|
||
|
|
// 检查是否为系统权限
|
||
|
|
if (permission.IsSystem)
|
||
|
|
{
|
||
|
|
_logger.LogWarning("系统权限 {PermissionId} 不允许修改", request.Id);
|
||
|
|
return OperationResult<UpdatePermissionResponse>.CreateFailure("系统权限不允许修改");
|
||
|
|
}
|
||
|
|
|
||
|
|
// 更新权限信息
|
||
|
|
permission.Update(
|
||
|
|
request.Name,
|
||
|
|
request.Description,
|
||
|
|
request.Level,
|
||
|
|
request.IsEnabled);
|
||
|
|
|
||
|
|
_permissionRepository.UpdateAsync(permission, cancellationToken);
|
||
|
|
|
||
|
|
_logger.LogInformation("权限 {PermissionId} 更新成功", request.Id);
|
||
|
|
|
||
|
|
return OperationResult<UpdatePermissionResponse>.CreateSuccess(
|
||
|
|
new UpdatePermissionResponse(request.Id, true));
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
_logger.LogError(ex, "更新权限 {PermissionId} 失败", request.Id);
|
||
|
|
return OperationResult<UpdatePermissionResponse>.CreateFailure("更新权限失败,请稍后重试");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|