finalizare 1.0
This commit is contained in:
+7
@@ -0,0 +1,7 @@
|
||||
namespace Application.Endpoints.Authorization.UserResetPassword;
|
||||
|
||||
public class UserResetPasswordCommand
|
||||
{
|
||||
public string Email { get; set; } = string.Empty;
|
||||
public string Password { get; set; } = string.Empty;
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
using Application.Services.Database.PostgreSQL;
|
||||
using Application.Services.Email;
|
||||
using Application.Services.HashingAlgorithms;
|
||||
|
||||
namespace Application.Endpoints.Authorization.UserResetPassword;
|
||||
|
||||
public class UserResetPasswordHandler(
|
||||
IEmailService emailService,
|
||||
IHashingAlgorithms hashingAlgorithms,
|
||||
IDoctorRepository doctorRepository,
|
||||
IPatientRepository patientRepository,
|
||||
IAdminRepository adminRepository)
|
||||
{
|
||||
public async Task<BaseResponse> Handle(UserResetPasswordCommand request, CancellationToken token)
|
||||
{
|
||||
var validator = new UserResetPasswordValidator(
|
||||
patientRepository, doctorRepository, adminRepository);
|
||||
var result = await validator.ValidateAsync(request, token);
|
||||
if (!result.IsValid)
|
||||
{
|
||||
var firstError = result.Errors.FirstOrDefault();
|
||||
var errorCode = int.TryParse(firstError.ErrorCode, out var code) ? code : -1;
|
||||
var errorMessage = firstError.ErrorMessage;
|
||||
|
||||
return new BaseResponse
|
||||
{
|
||||
StatusCode = errorCode,
|
||||
Message = errorMessage,
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
|
||||
string? name = null;
|
||||
var patient = await patientRepository.FindByEmailAsync(request.Email, token);
|
||||
if (patient != null)
|
||||
{
|
||||
name = patient.Name;
|
||||
patient.SetPassword(hashingAlgorithms.Sha256Algorithm(request.Password));
|
||||
await patientRepository.UpdateAsync(patient, token);
|
||||
}
|
||||
|
||||
var doctor = await doctorRepository.FindByEmailAsync(request.Email, token);
|
||||
if (doctor != null)
|
||||
{
|
||||
name = doctor.Name;
|
||||
doctor.SetPassword(hashingAlgorithms.Sha256Algorithm(request.Password));
|
||||
await doctorRepository.UpdateAsync(doctor, token);
|
||||
}
|
||||
|
||||
var admin = await adminRepository.FindByEmailAsync(request.Email, token);
|
||||
if (admin != null)
|
||||
{
|
||||
name = admin.Name;
|
||||
admin.SetPassword(hashingAlgorithms.Sha256Algorithm(request.Password));
|
||||
await adminRepository.UpdateAsync(admin, token);
|
||||
}
|
||||
|
||||
var emailBody = emailService.GenerateResetCredentialsEmailBody(name, request.Email, request.Password);
|
||||
await emailService.SendEmailAsync(request.Email, emailService.GetSuccessfulPasswordResetSubject(), emailBody);
|
||||
|
||||
return new BaseResponse
|
||||
{
|
||||
StatusCode = HttpStatusCodes.OK,
|
||||
Message = "Password updated successfully.",
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
using Application.Services.Database.PostgreSQL;
|
||||
using Domain;
|
||||
using FluentValidation;
|
||||
|
||||
namespace Application.Endpoints.Authorization.UserResetPassword;
|
||||
|
||||
public class UserResetPasswordValidator : AbstractValidator<UserResetPasswordCommand>
|
||||
{
|
||||
private readonly IAdminRepository _adminRepository;
|
||||
private readonly IDoctorRepository _doctorRepository;
|
||||
private readonly IPatientRepository _patientRepository;
|
||||
|
||||
public UserResetPasswordValidator(IPatientRepository patientRepository,
|
||||
IDoctorRepository doctorRepository, IAdminRepository adminRepository)
|
||||
{
|
||||
_patientRepository = patientRepository;
|
||||
_doctorRepository = doctorRepository;
|
||||
_adminRepository = adminRepository;
|
||||
|
||||
RuleFor(x => x.Email)
|
||||
.NotEmpty().WithMessage("Email is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
|
||||
.EmailAddress().WithMessage("Invalid email format.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
|
||||
.MustAsync(async (email, token) => await AccountRegistered(email, token))
|
||||
.WithMessage("Account already registered in system.").WithErrorCode(HttpStatusCodes.BadRequest.ToString());
|
||||
|
||||
|
||||
RuleFor(x => x.Password)
|
||||
.NotEmpty().WithMessage("Password is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
|
||||
.MinimumLength(8).WithMessage("Password must be at least 8 characters long.")
|
||||
.WithErrorCode(HttpStatusCodes.BadRequest.ToString());
|
||||
|
||||
RuleFor(x => x)
|
||||
.MustAsync(async (request, token) =>
|
||||
await AccountRegistered(request.Email, token))
|
||||
.WithMessage("Account already registered in system.").WithErrorCode(HttpStatusCodes.BadRequest.ToString());
|
||||
}
|
||||
|
||||
private bool BeAValidRole(string role)
|
||||
{
|
||||
var validRoles = new[] { UserRoles.Admin, UserRoles.Doctor, UserRoles.Patient };
|
||||
return validRoles.Contains(role);
|
||||
}
|
||||
|
||||
private async Task<bool> AccountRegistered(string email, CancellationToken token)
|
||||
{
|
||||
var patient = await _patientRepository.FindByEmailAsync(email, token);
|
||||
var doctor = await _doctorRepository.FindByEmailAsync(email, token);
|
||||
var admin = await _adminRepository.FindByEmailAsync(email, token);
|
||||
|
||||
return patient != null || doctor != null || admin != null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user