ref: project structure
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
using MediatR;
|
||||
using mws.backend.dotnet.application.Auth;
|
||||
using mws.backend.dotnet.application.Common;
|
||||
using mws.backend.dotnet.application.Permissions;
|
||||
|
||||
namespace mws.backend.dotnet.application.Users;
|
||||
|
||||
public record ResetPasswordCommand(Guid ActorUserId, Guid Id, ResetPasswordRequest Request) : IRequest;
|
||||
|
||||
public class ResetPasswordHandler(IUnitOfWork uow, IPasswordHasher passwordHasher, IPermissionService permissions)
|
||||
: IRequestHandler<ResetPasswordCommand>
|
||||
{
|
||||
private const string Screen = "users";
|
||||
|
||||
public async Task Handle(ResetPasswordCommand command, CancellationToken ct)
|
||||
{
|
||||
await permissions.EnsureAsync(command.ActorUserId, Screen, PermissionAction.Edit, ct);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(command.Request.NewPassword))
|
||||
{
|
||||
throw new BadRequestException("New password is required");
|
||||
}
|
||||
|
||||
var user = await uow.Users.GetByIdAsync(command.Id, ct)
|
||||
?? throw new NotFoundException("User not found");
|
||||
|
||||
user.PasswordHash = passwordHasher.Hash(command.Request.NewPassword);
|
||||
user.UpdatedAt = DateTime.UtcNow;
|
||||
await uow.SaveChangesAsync(ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user