Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

2.8 KB
Raw Blame
using FluentValidation;
using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Business.Core.Interfaces.Customers;
using Grand.Domain.Common;
using Grand.Infrastructure;
using Grand.Infrastructure.Configuration;
using Grand.Infrastructure.Models;
using Grand.Infrastructure.Validators;
using Grand.SharedKernel.Captcha;
using Grand.Web.Common.Validators;
using Grand.Web.Models.Customer;

namespace Grand.Web.Validators.Customer;

public class PasswordRecoveryValidator : BaseGrandValidator<PasswordRecoveryModel>
{
    public PasswordRecoveryValidator(
        IEnumerable<IValidatorConsumer<PasswordRecoveryModel>> validators,
        IEnumerable<IValidatorConsumer<ICaptchaValidModel>> validatorsCaptcha,
        ICustomerService customerService, CaptchaSettings captchaSettings,
        IHttpContextAccessor contextAccessor, IGoogleReCaptchaValidator googleReCaptchaValidator,
        ITranslationService translationService,
        IContextAccessor workContextAccessor, CustomerConfig customerConfig)
        : base(validators)
    {
        var storeId = customerConfig.RegisterCustomersPerStore
            ? workContextAccessor.StoreContext.CurrentStore.Id
            : "";

        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"));
            RuleFor(x => x.Captcha)
                .SetValidator(new CaptchaValidator(validatorsCaptcha, contextAccessor, googleReCaptchaValidator));
        }
    }
}