From c8266c31de4acf33268e74ec362b4676675000a0 Mon Sep 17 00:00:00 2001 From: support Date: Mon, 23 Nov 2026 18:51:04 +0100 Subject: [PATCH] Storefront sign-in and password recovery: answer the same whether or not an address has an account --- ...PasswordRecoverySendCommandHandlerTests.cs | 114 ++++++++++++----- .../CustomerValidatorsStoreScopingTests.cs | 16 --- .../LoginValidatorAccountStateTests.cs | 116 ++++++++++++++++++ ...veryConfirmValidatorExternalSignInTests.cs | 23 ++++ .../PasswordRecoverySendCommandHandler.cs | 16 ++- .../Controllers/AccountController.cs | 3 - .../GetPasswordRecoveryConfirmHandler.cs | 5 +- .../Validators/Customer/LoginValidator.cs | 19 ++- .../PasswordRecoveryConfirmValidator.cs | 35 ++---- .../Customer/PasswordRecoveryValidator.cs | 31 +---- 10 files changed, 263 insertions(+), 115 deletions(-) create mode 100644 src/Tests/Grand.Web.Tests/Validators/Customer/LoginValidatorAccountStateTests.cs diff --git a/src/Tests/Grand.Web.Tests/Commands/Customers/PasswordRecoverySendCommandHandlerTests.cs b/src/Tests/Grand.Web.Tests/Commands/Customers/PasswordRecoverySendCommandHandlerTests.cs index 5e6cb1209..f65321126 100644 --- a/src/Tests/Grand.Web.Tests/Commands/Customers/PasswordRecoverySendCommandHandlerTests.cs +++ b/src/Tests/Grand.Web.Tests/Commands/Customers/PasswordRecoverySendCommandHandlerTests.cs @@ -1,5 +1,6 @@ using Grand.Business.Core.Utilities.Authentication; using Grand.Business.Core.Interfaces.Authentication; +using Grand.Business.Core.Interfaces.Common.Directory; using Grand.Business.Core.Interfaces.Customers; using Grand.Business.Core.Interfaces.Messages; using Grand.Domain.Customers; @@ -14,50 +15,105 @@ namespace Grand.Web.Tests.Commands.Customers; [TestClass] public class PasswordRecoverySendCommandHandlerTests { + private readonly Customer _customer = new() { Id = "c1", Active = true }; + private Mock _customerService; + private Mock _groupService; + private PasswordRecoverySendCommandHandler _handler; + private Mock _messages; + private Mock _policy; + + [TestInitialize] + public void Init() + { + _customerService = new Mock(); + _messages = new Mock(); + _policy = new Mock(); + _policy.Setup(x => x.PasswordSignIn(It.IsAny(), SignInSurface.Storefront)) + .ReturnsAsync(PasswordSignInDecision.Password); + _groupService = new Mock(); + _groupService.Setup(x => x.IsRegistered(_customer)).ReturnsAsync(true); + _handler = new PasswordRecoverySendCommandHandler(_customerService.Object, _messages.Object, _policy.Object, + _groupService.Object); + } + + private Task Send(Customer customer) => + _handler.Handle(new PasswordRecoverySendCommand { + Customer = customer, Store = new Grand.Domain.Stores.Store(), Language = new Language { Id = "l1" } + }, CancellationToken.None); + + private void VerifyNoMessage() + { + _messages.Verify(x => x.SendCustomerPasswordRecoveryMessage(It.IsAny(), + It.IsAny(), It.IsAny()), Times.Never); + _customerService.Verify(x => x.UpdateUserField(It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny()), Times.Never); + } + [TestMethod] public async Task ExternalSignInRequired_NoMessage_NoToken() { - var customerService = new Mock(); - var messages = new Mock(); - var policy = new Mock(); - var customer = new Customer { Id = "c1" }; - policy.Setup(x => x.PasswordSignIn(customer, It.IsAny())).ReturnsAsync(new PasswordSignInDecision(false, new Mock().Object)); - var handler = new PasswordRecoverySendCommandHandler(customerService.Object, messages.Object, policy.Object); + _policy.Setup(x => x.PasswordSignIn(_customer, It.IsAny())) + .ReturnsAsync(new PasswordSignInDecision(false, new Mock().Object)); - Assert.IsTrue(await handler.Handle(new PasswordRecoverySendCommand { - Customer = customer, Store = new Grand.Domain.Stores.Store(), Language = new Language { Id = "l1" } - }, CancellationToken.None)); + Assert.IsTrue(await Send(_customer)); - messages.Verify(x => x.SendCustomerPasswordRecoveryMessage(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); - customerService.Verify(x => x.UpdateUserField(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + VerifyNoMessage(); } [TestMethod] public async Task NullCustomer_NoMessage() { - var messages = new Mock(); - var handler = new PasswordRecoverySendCommandHandler(new Mock().Object, messages.Object, - new Mock().Object); + Assert.IsTrue(await Send(null)); + + VerifyNoMessage(); + } + + [TestMethod] + public async Task InactiveCustomer_NoMessage() + { + _customer.Active = false; + + Assert.IsTrue(await Send(_customer)); + + VerifyNoMessage(); + } + + [TestMethod] + public async Task DeletedCustomer_NoMessage() + { + _customer.Deleted = true; + + Assert.IsTrue(await Send(_customer)); + + VerifyNoMessage(); + } + + [TestMethod] + public async Task LockedOutCustomer_NoMessage() + { + _customer.CannotLoginUntilDateUtc = DateTime.UtcNow.AddMinutes(10); - Assert.IsTrue(await handler.Handle(new PasswordRecoverySendCommand { - Store = new Grand.Domain.Stores.Store(), Language = new Language { Id = "l1" } - }, CancellationToken.None)); + Assert.IsTrue(await Send(_customer)); - messages.Verify(x => x.SendCustomerPasswordRecoveryMessage(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + VerifyNoMessage(); + } + + [TestMethod] + public async Task NotRegistered_NoMessage() + { + _groupService.Setup(x => x.IsRegistered(_customer)).ReturnsAsync(false); + + Assert.IsTrue(await Send(_customer)); + + VerifyNoMessage(); } [TestMethod] public async Task PasswordAllowed_MessageSent() { - var messages = new Mock(); - var policy = new Mock(); - policy.Setup(x => x.PasswordSignIn(It.IsAny(), SignInSurface.Storefront)) - .ReturnsAsync(PasswordSignInDecision.Password); - var handler = new PasswordRecoverySendCommandHandler(new Mock().Object, messages.Object, - policy.Object); - await handler.Handle(new PasswordRecoverySendCommand { - Customer = new Customer { Id = "c1" }, Store = new Grand.Domain.Stores.Store(), Language = new Language { Id = "l1" } - }, CancellationToken.None); - messages.Verify(x => x.SendCustomerPasswordRecoveryMessage(It.IsAny(), It.IsAny(), "l1"), Times.Once); + await Send(_customer); + + _messages.Verify(x => x.SendCustomerPasswordRecoveryMessage(_customer, It.IsAny(), + "l1"), Times.Once); } -} +} \ No newline at end of file diff --git a/src/Tests/Grand.Web.Tests/Validators/Customer/CustomerValidatorsStoreScopingTests.cs b/src/Tests/Grand.Web.Tests/Validators/Customer/CustomerValidatorsStoreScopingTests.cs index 2d88ee61b..106517f35 100644 --- a/src/Tests/Grand.Web.Tests/Validators/Customer/CustomerValidatorsStoreScopingTests.cs +++ b/src/Tests/Grand.Web.Tests/Validators/Customer/CustomerValidatorsStoreScopingTests.cs @@ -94,22 +94,6 @@ public async Task RegisterValidator_ScopesEmailLookupToCurrentStore() customerService.Verify(c => c.GetCustomerByEmail(It.IsAny(), StoreId), Times.AtLeastOnce); } - [TestMethod] - public async Task PasswordRecoveryValidator_ScopesEmailLookupToCurrentStore() - { - var customerService = new Mock(); - var validator = new PasswordRecoveryValidator( - new List>(), - new List>(), - customerService.Object, new Grand.Domain.Common.CaptchaSettings(), - new Mock().Object, new Mock().Object, - Translation().Object, Context()); - - await validator.ValidateAsync(new PasswordRecoveryModel { Email = "u@x.com" }); - - customerService.Verify(c => c.GetCustomerByEmail(It.IsAny(), StoreId), Times.AtLeastOnce); - } - [TestMethod] public async Task PasswordRecoveryConfirmValidator_ScopesEmailLookupToCurrentStore() { diff --git a/src/Tests/Grand.Web.Tests/Validators/Customer/LoginValidatorAccountStateTests.cs b/src/Tests/Grand.Web.Tests/Validators/Customer/LoginValidatorAccountStateTests.cs new file mode 100644 index 000000000..41b6530cc --- /dev/null +++ b/src/Tests/Grand.Web.Tests/Validators/Customer/LoginValidatorAccountStateTests.cs @@ -0,0 +1,116 @@ +using Grand.Business.Core.Events.Customers; +using Grand.Business.Core.Utilities.Authentication; +using Grand.Business.Core.Interfaces.Authentication; +using Grand.Business.Core.Interfaces.Common.Directory; +using Grand.Business.Core.Interfaces.Common.Security; +using Grand.Business.Core.Interfaces.Customers; +using Grand.Domain.Common; +using Grand.Domain.Customers; +using Grand.Infrastructure; +using Grand.Infrastructure.Models; +using Grand.Infrastructure.Validators; +using Grand.Mediator; +using Grand.SharedKernel.Captcha; +using Grand.Web.Common.Localization; +using Grand.Web.Models.Customer; +using Grand.Web.Validators.Customer; +using Microsoft.AspNetCore.Http; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; + +namespace Grand.Web.Tests.Validators.Customers; + +/// +/// The sign-in form does not tell which addresses have an account; the state of an account is told only to +/// someone who knows its password +/// +[TestClass] +public class LoginValidatorAccountStateTests +{ + private const string WrongCredentials = "Account.Login.WrongCredentials"; + + private readonly Customer _customer = new() + { Email = "a@b.c", Active = true, PasswordFormatId = PasswordFormat.Clear, Password = "pw" }; + + private Mock _customerService; + private Mock _groups; + private Mock _mediator; + + [TestInitialize] + public void Init() + { + _customerService = new Mock(); + _customerService.Setup(c => c.GetCustomerByEmail("a@b.c", It.IsAny())).ReturnsAsync(_customer); + _groups = new Mock(); + _groups.Setup(g => g.IsRegistered(_customer)).ReturnsAsync(true); + _mediator = new Mock(); + } + + private async Task Errors(string email, string password) + { + var encryption = new Mock(); + encryption.Setup(e => e.VerifyPassword(It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny())) + .Returns((string entered, PasswordFormat _, string stored, string _, HashedPasswordFormat _) => + entered == stored); + var translation = new Mock(); + translation.Setup(t => t.GetResource(It.IsAny())).Returns(k => k); + var policy = new Mock(); + policy.Setup(x => x.PasswordSignIn(It.IsAny(), SignInSurface.Storefront)) + .ReturnsAsync(PasswordSignInDecision.Password); + var store = new Mock(); + store.Setup(s => s.CurrentStore).Returns(new Grand.Domain.Stores.Store { Id = "store-1" }); + var ctx = new Mock(); + ctx.Setup(c => c.StoreContext).Returns(store.Object); + + var validator = new LoginValidator( + new List>(), new List>(), + _customerService.Object, _groups.Object, encryption.Object, translation.Object, new CustomerSettings(), + new CaptchaSettings(), new Mock().Object, new Mock().Object, + ctx.Object, policy.Object, _mediator.Object); + + var result = await validator.ValidateAsync(new LoginModel { Email = email, Password = password }); + return result.Errors.Select(e => e.ErrorMessage).ToArray(); + } + + [TestMethod] + public async Task UnknownEmail_WrongCredentials() + { + CollectionAssert.AreEqual(new[] { WrongCredentials }, await Errors("nobody@b.c", "pw")); + } + + [TestMethod] + public async Task DeletedAccount_WrongCredentials() + { + _customer.Deleted = true; + + CollectionAssert.AreEqual(new[] { WrongCredentials }, await Errors("a@b.c", "pw")); + } + + [TestMethod] + public async Task NotRegistered_WrongCredentials() + { + _groups.Setup(g => g.IsRegistered(_customer)).ReturnsAsync(false); + + CollectionAssert.AreEqual(new[] { WrongCredentials }, await Errors("a@b.c", "pw")); + } + + [TestMethod] + public async Task InactiveAccount_WrongPassword_WrongCredentialsAndCounted() + { + _customer.Active = false; + + CollectionAssert.AreEqual(new[] { WrongCredentials }, await Errors("a@b.c", "wrong")); + _mediator.Verify(m => m.Publish(It.IsAny(), It.IsAny()), + Times.Once); + } + + [TestMethod] + public async Task InactiveAccount_RightPassword_NotActive() + { + _customer.Active = false; + + CollectionAssert.AreEqual(new[] { "Account.Login.WrongCredentials.NotActive" }, + await Errors("a@b.c", "pw")); + } +} \ No newline at end of file diff --git a/src/Tests/Grand.Web.Tests/Validators/Customer/PasswordRecoveryConfirmValidatorExternalSignInTests.cs b/src/Tests/Grand.Web.Tests/Validators/Customer/PasswordRecoveryConfirmValidatorExternalSignInTests.cs index 75135835e..bff2d7d81 100644 --- a/src/Tests/Grand.Web.Tests/Validators/Customer/PasswordRecoveryConfirmValidatorExternalSignInTests.cs +++ b/src/Tests/Grand.Web.Tests/Validators/Customer/PasswordRecoveryConfirmValidatorExternalSignInTests.cs @@ -67,6 +67,29 @@ public async Task ValidLink_Valid() Assert.IsTrue((await Validate()).IsValid); } + //the page does not tell which addresses have an account, nor what state it is in + [TestMethod] + public async Task UnknownEmail_WrongToken() + { + var result = await _validator.ValidateAsync(new PasswordRecoveryConfirmModel { + Email = "nobody@x.com", Token = Token, NewPassword = "N3w-password", ConfirmNewPassword = "N3w-password" + }); + + CollectionAssert.AreEqual(new[] { "Account.PasswordRecovery.WrongToken" }, + result.Errors.Select(e => e.ErrorMessage).ToArray()); + } + + [TestMethod] + public async Task InactiveAccount_WrongToken() + { + _customer.Active = false; + + var result = await Validate(); + + CollectionAssert.AreEqual(new[] { "Account.PasswordRecovery.WrongToken" }, + result.Errors.Select(e => e.ErrorMessage).ToArray()); + } + //a link sent before the account had to sign in through the provider sets no password any more [TestMethod] public async Task ExternalSignInRequired_WrongToken() diff --git a/src/Web/Grand.Web/Commands/Handler/Customers/PasswordRecoverySendCommandHandler.cs b/src/Web/Grand.Web/Commands/Handler/Customers/PasswordRecoverySendCommandHandler.cs index ef95bd26a..5fe51ce6d 100644 --- a/src/Web/Grand.Web/Commands/Handler/Customers/PasswordRecoverySendCommandHandler.cs +++ b/src/Web/Grand.Web/Commands/Handler/Customers/PasswordRecoverySendCommandHandler.cs @@ -1,4 +1,5 @@ using Grand.Business.Core.Interfaces.Authentication; +using Grand.Business.Core.Interfaces.Common.Directory; using Grand.Business.Core.Interfaces.Customers; using Grand.Business.Core.Interfaces.Messages; using Grand.Domain.Customers; @@ -12,21 +13,30 @@ public class PasswordRecoverySendCommandHandler : IRequestHandler Handle(PasswordRecoverySendCommand request, CancellationToken cancellationToken) { - //an account that signs in only with the identity provider has no password to recover; the page answers as usual - if (request.Customer == null || !(await _externalSignInPolicy.PasswordSignIn(request.Customer, SignInSurface.Storefront)).Allowed) + //no message for an address without an account that may sign in, nor for an account that signs in only with the + //identity provider (no password to recover); the page answers as usual, so it does not tell which addresses have one + if (request.Customer is not { Deleted: false, Active: true }) + return true; + if (request.Customer.CannotLoginUntilDateUtc.HasValue && request.Customer.CannotLoginUntilDateUtc.Value > DateTime.UtcNow) + return true; + if (!await _groupService.IsRegistered(request.Customer) || + !(await _externalSignInPolicy.PasswordSignIn(request.Customer, SignInSurface.Storefront)).Allowed) return true; //save token and current date diff --git a/src/Web/Grand.Web/Controllers/AccountController.cs b/src/Web/Grand.Web/Controllers/AccountController.cs index 13d0b146f..26c765b73 100644 --- a/src/Web/Grand.Web/Controllers/AccountController.cs +++ b/src/Web/Grand.Web/Controllers/AccountController.cs @@ -460,9 +460,6 @@ public virtual async Task> PasswordRe return RedirectToRoute("Login"); var customer = await _customerService.GetCustomerByEmail(email, _contextAccessor.StoreContext.CurrentStore.Id); - if (customer == null) - return RedirectToRoute("HomePage"); - var model = await _mediator.Send(new GetPasswordRecoveryConfirm { Customer = customer, Token = token }); return View(model); diff --git a/src/Web/Grand.Web/Features/Handlers/Customers/GetPasswordRecoveryConfirmHandler.cs b/src/Web/Grand.Web/Features/Handlers/Customers/GetPasswordRecoveryConfirmHandler.cs index a77e59be5..5c429d13b 100644 --- a/src/Web/Grand.Web/Features/Handlers/Customers/GetPasswordRecoveryConfirmHandler.cs +++ b/src/Web/Grand.Web/Features/Handlers/Customers/GetPasswordRecoveryConfirmHandler.cs @@ -25,11 +25,12 @@ public async Task Handle(GetPasswordRecoveryConfir { var model = new PasswordRecoveryConfirmModel(); - //validate token - if (!request.Customer.IsPasswordRecoveryTokenValid(request.Token)) + //an address without an account reads as a wrong link, so the page does not tell which addresses have one + if (request.Customer == null || !request.Customer.IsPasswordRecoveryTokenValid(request.Token)) { model.DisablePasswordChanging = true; model.Result = _translationService.GetResource("Account.PasswordRecovery.WrongToken"); + return model; } //validate token expiration date diff --git a/src/Web/Grand.Web/Validators/Customer/LoginValidator.cs b/src/Web/Grand.Web/Validators/Customer/LoginValidator.cs index 1d6243eb1..8ce94ac55 100644 --- a/src/Web/Grand.Web/Validators/Customer/LoginValidator.cs +++ b/src/Web/Grand.Web/Validators/Customer/LoginValidator.cs @@ -53,20 +53,15 @@ public LoginValidator( ? await customerService.GetCustomerByUsername(x.Username, storeId) : await customerService.GetCustomerByEmail(x.Email, storeId); + //an address without an account answers as a wrong password, so the form does not tell which addresses + //have one; whether an account is active is told only to someone who knows its password switch (customer) { - case null: - context.AddFailure( - translationService.GetResource("Account.Login.WrongCredentials.CustomerNotExist")); - break; - case { Deleted: true }: - context.AddFailure(translationService.GetResource("Account.Login.WrongCredentials.Deleted")); - break; - case { Active: false }: - context.AddFailure(translationService.GetResource("Account.Login.WrongCredentials.NotActive")); + case null or { Deleted: true }: + context.AddFailure(translationService.GetResource("Account.Login.WrongCredentials")); break; case not null when !await groupService.IsRegistered(customer): - context.AddFailure(translationService.GetResource("Account.Login.WrongCredentials.NotRegistered")); + context.AddFailure(translationService.GetResource("Account.Login.WrongCredentials")); break; case { CannotLoginUntilDateUtc: not null } when customer!.CannotLoginUntilDateUtc.Value > DateTime.UtcNow: @@ -81,6 +76,10 @@ public LoginValidator( context.AddFailure(translationService.GetResource("Account.Login.WrongCredentials")); await mediator.Publish(new CustomerLoginFailedEvent(customer, "wrong credentials"), _); } + else if (!customer.Active) + { + context.AddFailure(translationService.GetResource("Account.Login.WrongCredentials.NotActive")); + } else if (await externalSignInPolicy.PasswordSignIn(customer, SignInSurface.Storefront) is { Allowed: false } decision) { context.AddFailure(decision.Provider != null diff --git a/src/Web/Grand.Web/Validators/Customer/PasswordRecoveryConfirmValidator.cs b/src/Web/Grand.Web/Validators/Customer/PasswordRecoveryConfirmValidator.cs index bbde53966..db55ecb66 100644 --- a/src/Web/Grand.Web/Validators/Customer/PasswordRecoveryConfirmValidator.cs +++ b/src/Web/Grand.Web/Validators/Customer/PasswordRecoveryConfirmValidator.cs @@ -41,35 +41,22 @@ public PasswordRecoveryConfirmValidator( { var customer = await customerService.GetCustomerByEmail(x.Email, storeId); - switch (customer) + //an address without an account that may sign in reads as a wrong link, as in the panels, so the page does + //not tell which addresses have one; so does a link sent before the account had to sign in through the provider + if (customer is not { Deleted: false, Active: true } || + customer.CannotLoginUntilDateUtc > DateTime.UtcNow || + !await groupService.IsRegistered(customer) || + !customer.IsPasswordRecoveryTokenValid(x.Token) || + !(await externalSignInPolicy.PasswordSignIn(customer, SignInSurface.Storefront)).Allowed) { - case null: - context.AddFailure(translationService.GetResource("Account.PasswordRecovery.EmailNotFound")); - break; - case { Deleted: true }: - context.AddFailure(translationService.GetResource("Account.PasswordRecovery.Deleted")); - break; - case { Active: false }: - context.AddFailure(translationService.GetResource("Account.PasswordRecovery.NotActive")); - break; - case { CannotLoginUntilDateUtc: not null } - when customer.CannotLoginUntilDateUtc.Value > DateTime.UtcNow: - context.AddFailure(translationService.GetResource("Account.PasswordRecovery.LockedOut")); - break; - case not null when !await groupService.IsRegistered(customer): - context.AddFailure(translationService.GetResource("Account.PasswordRecovery.NotRegistered")); - break; - } - - //a link sent before the account had to sign in through the provider reads as a wrong link - if (customer is not null && (!customer.IsPasswordRecoveryTokenValid(x.Token) || - !(await externalSignInPolicy.PasswordSignIn(customer, SignInSurface.Storefront)).Allowed)) context.AddFailure(translationService.GetResource("Account.PasswordRecovery.WrongToken")); + return; + } - if (customer is not null && customer.IsPasswordRecoveryLinkExpired(customerSettings)) + if (customer.IsPasswordRecoveryLinkExpired(customerSettings)) context.AddFailure(translationService.GetResource("Account.PasswordRecovery.LinkExpired")); - if (customer is not null && customerSettings.UnduplicatedPasswordsNumber > 0) + if (customerSettings.UnduplicatedPasswordsNumber > 0) { //get some of previous passwords var previousPasswords = diff --git a/src/Web/Grand.Web/Validators/Customer/PasswordRecoveryValidator.cs b/src/Web/Grand.Web/Validators/Customer/PasswordRecoveryValidator.cs index c50c3be8d..2f80abc25 100644 --- a/src/Web/Grand.Web/Validators/Customer/PasswordRecoveryValidator.cs +++ b/src/Web/Grand.Web/Validators/Customer/PasswordRecoveryValidator.cs @@ -1,7 +1,5 @@ using FluentValidation; -using Grand.Business.Core.Interfaces.Customers; using Grand.Domain.Common; -using Grand.Infrastructure; using Grand.Infrastructure.Models; using Grand.Infrastructure.Validators; using Grand.SharedKernel.Captcha; @@ -16,38 +14,15 @@ public class PasswordRecoveryValidator : BaseGrandValidator> validators, IEnumerable> validatorsCaptcha, - ICustomerService customerService, CaptchaSettings captchaSettings, + CaptchaSettings captchaSettings, IHttpContextAccessor contextAccessor, IGoogleReCaptchaValidator googleReCaptchaValidator, - IRequestTranslationService translationService, - IContextAccessor workContextAccessor) + IRequestTranslationService translationService) : base(validators) { - var storeId = workContextAccessor.StoreContext.CurrentStore.Id; - + //nothing about the account behind the address: the page answers the same whether or not it has one RuleFor(x => x.Email).NotEmpty() .WithMessage(translationService.GetResource("Account.PasswordRecovery.Email.Required")); RuleFor(x => x.Email).EmailAddress().WithMessage(translationService.GetResource("Common.WrongEmail")); - RuleFor(x => x).CustomAsync(async (x, context, _) => - { - var customer = await customerService.GetCustomerByEmail(x.Email, storeId); - - switch (customer) - { - case null: - context.AddFailure(translationService.GetResource("Account.PasswordRecovery.EmailNotFound")); - break; - case { Deleted: true }: - context.AddFailure(translationService.GetResource("Account.PasswordRecovery.Deleted")); - break; - case { Active: false }: - context.AddFailure(translationService.GetResource("Account.PasswordRecovery.NotActive")); - break; - case { CannotLoginUntilDateUtc: not null } - when customer.CannotLoginUntilDateUtc.Value > DateTime.UtcNow: - context.AddFailure(translationService.GetResource("Account.PasswordRecovery.LockedOut")); - break; - } - }); if (captchaSettings.Enabled && captchaSettings.ShowOnPasswordRecoveryPage) { RuleFor(x => x.Captcha).NotNull().WithMessage(translationService.GetResource("Account.Captcha.Required"));