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.
60 lines
2.1 KiB
60 lines
2.1 KiB
3 months ago
|
using AutoMapper;
|
||
|
using Kean.Domain.Basic.Commands;
|
||
|
using Kean.Domain.Basic.Events;
|
||
|
using Kean.Domain.Basic.Repositories;
|
||
|
using Kean.Domain.Basic.SharedServices.Proxies;
|
||
|
using System.Threading;
|
||
|
using System.Threading.Tasks;
|
||
|
|
||
|
namespace Kean.Domain.Basic.CommandHandlers
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// 重置密码命令处理程序
|
||
|
/// </summary>
|
||
|
public sealed class ResetPasswordCommandHandler : CommandHandler<ResetPasswordCommand>
|
||
|
{
|
||
|
private readonly ICommandBus _commandBus; // 命令总线
|
||
|
private readonly IMapper _mapper; // 模型映射
|
||
|
private readonly IUserRepository _userRepository; // 用户仓库
|
||
|
private readonly IdentityProxy _identityProxy;// 身份域代理
|
||
|
|
||
|
/// <summary>
|
||
|
/// 依赖注入
|
||
|
/// </summary>
|
||
|
public ResetPasswordCommandHandler(
|
||
|
ICommandBus commandBus,
|
||
|
IMapper mapper,
|
||
|
IUserRepository userRepository,
|
||
|
IdentityProxy identityProxy)
|
||
|
{
|
||
|
_commandBus = commandBus;
|
||
|
_mapper = mapper;
|
||
|
_userRepository = userRepository;
|
||
|
_identityProxy = identityProxy;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 处理程序
|
||
|
/// </summary>
|
||
|
public override async Task Handle(ResetPasswordCommand command, CancellationToken cancellationToken)
|
||
|
{
|
||
|
if (command.ValidationResult.IsValid)
|
||
|
{
|
||
|
if (!await _userRepository.IsExist(command.Id))
|
||
|
{
|
||
|
await _commandBus.Notify(nameof(command.Id), "用户不存在", command.Id, nameof(ErrorCode.Gone),
|
||
|
cancellationToken: cancellationToken);
|
||
|
return;
|
||
|
}
|
||
|
await _userRepository.ResetPassword(command.Id, s => _identityProxy.EncodePassword(s));
|
||
|
await _commandBus.Trigger(_mapper.Map<ResetPasswordSuccessEvent>(command), cancellationToken);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
await _commandBus.Notify(command.ValidationResult,
|
||
|
cancellationToken: cancellationToken);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|