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.

4.6 KB
Raw Blame
using FluentValidation;
using Grand.Business.Core.Events.Customers;
using Grand.Business.Core.Interfaces.Common.Directory;
using Grand.Business.Core.Interfaces.Common.Localization;
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.Configuration;
using Grand.Infrastructure.Models;
using Grand.Infrastructure.Validators;
using Grand.SharedKernel.Captcha;
using Grand.Web.Common.Validators;
using Grand.Web.Models.Customer;
using Grand.Mediator;

namespace Grand.Web.Validators.Customer;

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

        if (!customerSettings.UsernamesEnabled)
        {
            RuleFor(x => x.Email).NotEmpty()
                .WithMessage(translationService.GetResource("Account.Login.Fields.Email.Required"));

            RuleFor(x => x.Email).EmailAddress().WithMessage(translationService.GetResource("Common.WrongEmail"));
        }
        else
        {
            RuleFor(x => x.Username).NotEmpty()
                .WithMessage(translationService.GetResource("Account.Login.Fields.UserName.Required"));
        }

        RuleFor(x => x.Password).NotEmpty()
            .WithMessage(translationService.GetResource("Account.Login.Fields.Password.Required"));

        RuleFor(x => x).CustomAsync(async (x, context, _) =>
        {
            var customer = customerSettings.UsernamesEnabled
                ? await customerService.GetCustomerByUsername(x.Username, storeId)
                : await customerService.GetCustomerByEmail(x.Email, storeId);

            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"));
                    break;
                case not null when !await groupService.IsRegistered(customer):
                    context.AddFailure(translationService.GetResource("Account.Login.WrongCredentials.NotRegistered"));
                    break;
                case { CannotLoginUntilDateUtc: not null }
                    when customer!.CannotLoginUntilDateUtc.Value > DateTime.UtcNow:
                    context.AddFailure(translationService.GetResource("Account.Login.WrongCredentials.LockedOut"));
                    break;
                case not null:
                    {
                        var isValid = encryptionService.VerifyPassword(x.Password, customer.PasswordFormatId,
                            customer.Password, customer.PasswordSalt, customerSettings.HashedPasswordFormat);
                        if (!isValid)
                        {
                            context.AddFailure(translationService.GetResource("Account.Login.WrongCredentials"));
                            await contextAccessor.HttpContext!.RequestServices.GetRequiredService<IMediator>()
                                .Publish(new CustomerLoginFailedEvent(customer), _);
                        }

                        break;
                    }
            }
        });

        if (captchaSettings.Enabled && captchaSettings.ShowOnLoginPage)
        {
            RuleFor(x => x.Captcha).NotNull().WithMessage(translationService.GetResource("Account.Captcha.Required"));
            RuleFor(x => x.Captcha)
                .SetValidator(new CaptchaValidator(validatorsCaptcha, contextAccessor, googleReCaptchaValidator));
        }
    }
}