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.

14.9 KB
Raw Blame
using FluentValidation;
using Grand.Business.Core.Interfaces.Common.Directory;
using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Business.Core.Interfaces.Customers;
using Grand.Domain.Customers;
using Grand.Infrastructure;
using Grand.Infrastructure.Configuration;
using Grand.Infrastructure.Validators;
using Grand.SharedKernel.Extensions;
using Grand.Web.AdminShared.Models.Customers;
using System.Text.RegularExpressions;

namespace Grand.Web.AdminShared.Validators.Customers;

public class CustomerValidator : BaseGrandValidator<CustomerModel>
{
    public CustomerValidator(
        IEnumerable<IValidatorConsumer<CustomerModel>> validators,
        ITranslationService translationService,
        ICountryService countryService,
        IContextAccessor contextAccessor,
        ICustomerService customerService,
        IGroupService groupService,
        CustomerSettings customerSettings,
        CustomerConfig customerConfig)
        : base(validators)
    {
        //when per-store customer identity is enabled, uniqueness is scoped to the customer's store
        string StoreScope(CustomerModel m) => customerConfig.RegisterCustomersPerStore ? m.StoreId : "";

        CustomerCreateValidator();
        CustomerEditValidator();

        //customer email
        RuleFor(x => x.Email).NotEmpty().EmailAddress()
            .WithMessage(translationService.GetResource("Admin.Customers.Customers.Fields.Email.Required"));

        //store - required only when the editor is not an administrator (admins may manage store-independent
        //back-office/system accounts that have no store)
        RuleFor(x => x.StoreId).NotEmpty()
            .WithMessage(translationService.GetResource("Admin.Customers.Customers.Fields.Store.Required"))
            .WhenAsync(async (_, _) => !await groupService.IsAdmin(contextAccessor.WorkContext.CurrentCustomer));

        //a store manager can only assign a customer to his own store
        RuleFor(x => x.StoreId).MustAsync(async (storeId, _) =>
                !await groupService.IsStoreManager(contextAccessor.WorkContext.CurrentCustomer) ||
                storeId == contextAccessor.StoreContext.CurrentStore.Id)
            .WithMessage(
                translationService.GetResource("Admin.Customers.Customers.Fields.Store.MustBeCurrentStore"));

        //form fields
        if (customerSettings.CountryEnabled && customerSettings.CountryRequired)
            RuleFor(x => x.CountryId)
                .NotEqual("")
                .WithMessage(translationService.GetResource("Account.Fields.Country.Required"));

        if (customerSettings.CountryEnabled &&
            customerSettings.StateProvinceEnabled &&
            customerSettings.StateProvinceRequired)
            RuleFor(x => x.StateProvinceId).MustAsync(async (x, y, _) =>
            {
                var countryId = !string.IsNullOrEmpty(x.CountryId) ? x.CountryId : "";
                var country = await countryService.GetCountryById(countryId);
                if (country != null && country.StateProvinces.Any())
                {
                    //if yes, then ensure that state is selected
                    if (string.IsNullOrEmpty(y)) return false;

                    if (country.StateProvinces.FirstOrDefault(x => x.Id == y) != null)
                        return true;
                }

                return false;
            }).WithMessage(translationService.GetResource("Account.Fields.StateProvince.Required"));

        if (customerSettings.CompanyRequired && customerSettings.CompanyEnabled)
            RuleFor(x => x.Company).NotEmpty()
                .WithMessage(translationService.GetResource("Admin.Customers.Customers.Fields.Company.Required"));
        if (customerSettings.StreetAddressRequired && customerSettings.StreetAddressEnabled)
            RuleFor(x => x.StreetAddress).NotEmpty().WithMessage(
                translationService.GetResource("Admin.Customers.Customers.Fields.StreetAddress.Required"));
        if (customerSettings.StreetAddress2Required && customerSettings.StreetAddress2Enabled)
            RuleFor(x => x.StreetAddress2).NotEmpty().WithMessage(
                translationService.GetResource("Admin.Customers.Customers.Fields.StreetAddress2.Required"));
        if (customerSettings.ZipPostalCodeRequired && customerSettings.ZipPostalCodeEnabled)
            RuleFor(x => x.ZipPostalCode).NotEmpty().WithMessage(
                translationService.GetResource("Admin.Customers.Customers.Fields.ZipPostalCode.Required"));
        if (customerSettings.CityRequired && customerSettings.CityEnabled)
            RuleFor(x => x.City).NotEmpty()
                .WithMessage(translationService.GetResource("Admin.Customers.Customers.Fields.City.Required"));
        if (customerSettings.PhoneRequired && customerSettings.PhoneEnabled)
            RuleFor(x => x.Phone).NotEmpty()
                .WithMessage(translationService.GetResource("Admin.Customers.Customers.Fields.Phone.Required"));
        if (customerSettings.FaxRequired && customerSettings.FaxEnabled)
            RuleFor(x => x.Fax).NotEmpty()
                .WithMessage(translationService.GetResource("Admin.Customers.Customers.Fields.Fax.Required"));

        RuleFor(x => x).Custom((x, context) =>
        {
            if (!string.IsNullOrEmpty(x.Password) && !string.IsNullOrEmpty(customerSettings.PasswordRegularExpression))
            {
                var passwordRegex = new Regex(customerSettings.PasswordRegularExpression, RegexOptions.Compiled, TimeSpan.FromSeconds(1));
                if (!passwordRegex.Match(x.Password).Success)
                    context.AddFailure(translationService.GetResource("Account.Fields.Password.Validation"));
            }
            if (string.IsNullOrWhiteSpace(x.Username) && customerSettings.UsernamesEnabled)
                context.AddFailure("The username cannot be empty");
        });

        async Task<string> ValidateCustomerGroups(IList<CustomerGroup> customerGroups, CustomerModel customerModel)
        {
            ArgumentNullException.ThrowIfNull(customerGroups);

            //ensure a customer is not added to both 'Guests' and 'Registered' customer groups
            //ensure that a customer is in at least one required role ('Guests' and 'Registered')
            var isInGuestsGroup =
                customerGroups.FirstOrDefault(cr => cr.SystemName == SystemCustomerGroupNames.Guests) != null;
            var isInRegisteredGroup =
                customerGroups.FirstOrDefault(cr => cr.SystemName == SystemCustomerGroupNames.Registered) != null;
            var isAdminGroup =
                customerGroups.FirstOrDefault(cr => cr.SystemName == SystemCustomerGroupNames.Administrators) != null;
            var isVendorGroup =
                customerGroups.FirstOrDefault(cr => cr.SystemName == SystemCustomerGroupNames.Vendors) != null;
            var isStaffGroup = customerGroups.FirstOrDefault(cr => cr.SystemName == SystemCustomerGroupNames.StoreManager) !=
                               null;
            var isSalesGroup =
                customerGroups.FirstOrDefault(cr => cr.SystemName == SystemCustomerGroupNames.SalesManager) != null;

            switch (isInGuestsGroup)
            {
                case true when isInRegisteredGroup:
                    return "The customer cannot be in both 'Guests' and 'Registered' customer groups";
                case false when !isInRegisteredGroup:
                    return "Add the customer to 'Guests' or 'Registered' customer group";
            }

            if (await groupService.IsSalesManager(contextAccessor.WorkContext.CurrentCustomer) &&
                (isInGuestsGroup || customerGroups.Count != 1))
                return "Sales manager can assign role 'Registered' only";

            if (!await groupService.IsAdmin(contextAccessor.WorkContext.CurrentCustomer) && isAdminGroup)
                return "Only administrators can assign role 'Administrators'";

            switch (isAdminGroup)
            {
                case true when !string.IsNullOrEmpty(customerModel.VendorId):
                    return "A customer who is associated with a vendor can't be assigned the 'Administrator' role";
                case true when !string.IsNullOrEmpty(customerModel.StaffStoreId):
                    return "A customer who is associated with a staff can't be assigned the 'Administrator' role";
            }

            switch (isVendorGroup)
            {
                case true when string.IsNullOrEmpty(customerModel.VendorId):
                    return translationService.GetResource(
                        "Admin.Customers.Customers.CannotBeInVendoGroupWithoutVendorAssociated");
                case true when string.IsNullOrEmpty(customerModel.VendorId):
                    return translationService.GetResource(
                        "Admin.Customers.Customers.CannotBeInVendoGroupWithoutVendorAssociated");
                case true when isStaffGroup:
                    return translationService.GetResource("Admin.Customers.Customers.VendorShouldNotbeStaff");
            }

            if (isStaffGroup && string.IsNullOrEmpty(customerModel.StaffStoreId))
                return translationService.GetResource(
                    "Admin.Customers.Customers.CannotBeInStaffGroupWithoutStaffAssociated");

            if (isSalesGroup && string.IsNullOrEmpty(customerModel.SeId))
                return translationService.GetResource(
                    "Admin.Customers.Customers.CannotBeInSalesManagerGroupWithoutSalesEmployeeAssociated");

            //no errors
            return "";
        }

        void CustomerCreateValidator()
        {
            When(x => string.IsNullOrEmpty(x.Id), () =>
            {
                RuleFor(x => x).CustomAsync(async (x, context, y) =>
                {
                    if (!string.IsNullOrWhiteSpace(x.Email))
                    {
                        if (!CommonHelper.IsValidEmail(x.Email))
                            context.AddFailure(
                                translationService.GetResource("Account.EmailUsernameErrors.NewEmailIsNotValid"));

                        if (x.Email.Length > 100)
                            context.AddFailure(
                                translationService.GetResource("Account.EmailUsernameErrors.EmailTooLong"));

                        var customerByEmail = await customerService.GetCustomerByEmail(x.Email, StoreScope(x));
                        if (customerByEmail != null)
                            context.AddFailure("Email is already registered");
                    }

                    if (!string.IsNullOrWhiteSpace(x.Owner))
                    {
                        var customerOwner = await customerService.GetCustomerByEmail(x.Owner, StoreScope(x));
                        if (customerOwner == null)
                            context.AddFailure("Owner email is not exists");
                    }

                    if (!string.IsNullOrWhiteSpace(x.Username) && customerSettings.UsernamesEnabled)
                    {
                        var customerByUsername = await customerService.GetCustomerByUsername(x.Username, StoreScope(x));
                        if (customerByUsername != null)
                            context.AddFailure("Username is already registered");

                        if (x.Username.Length > 100)
                            context.AddFailure("Username is too long");
                    }

                    //validate customer groups
                    var allCustomerGroups = await groupService.GetAllCustomerGroups(showHidden: true);
                    var newCustomerGroups = allCustomerGroups.Where(customerGroup =>
                        x.CustomerGroups != null && x.CustomerGroups.Contains(customerGroup.Id)).ToList();
                    var customerGroupsError = await ValidateCustomerGroups(newCustomerGroups, x);
                    if (!string.IsNullOrEmpty(customerGroupsError)) context.AddFailure(customerGroupsError);
                });
            });
        }

        void CustomerEditValidator()
        {
            When(x => !string.IsNullOrEmpty(x.Id), () =>
            {
                RuleFor(x => x).CustomAsync(async (x, context, y) =>
                {
                    //validate customer groups
                    var allCustomerGroups = await groupService.GetAllCustomerGroups(showHidden: true);
                    var newCustomerGroups = allCustomerGroups.Where(customerGroup =>
                        x.CustomerGroups != null && x.CustomerGroups.Contains(customerGroup.Id)).ToList();

                    var customerGroupsError = await ValidateCustomerGroups(newCustomerGroups, x);
                    if (!string.IsNullOrEmpty(customerGroupsError)) context.AddFailure(customerGroupsError);

                    if (!string.IsNullOrWhiteSpace(x.Owner))
                    {
                        var customerByOwner = await customerService.GetCustomerByEmail(x.Owner, StoreScope(x));
                        if (customerByOwner == null)
                            context.AddFailure("Owner email is not exists");

                        if (string.Equals(x.Owner, x.Email, StringComparison.CurrentCultureIgnoreCase))
                            context.AddFailure("You can't assign own email");
                    }

                    var customer = await customerService.GetCustomerById(x.Id);
                    if (await groupService.IsSalesManager(contextAccessor.WorkContext.CurrentCustomer) &&
                        customer?.Id == contextAccessor.WorkContext.CurrentCustomer.Id)
                        context.AddFailure("You can't edit own data from admin panel");

                    if (customer != null && customer.Email != x.Email.ToLower())
                    {
                        if (!CommonHelper.IsValidEmail(x.Email))
                            context.AddFailure(
                                translationService.GetResource("Account.EmailUsernameErrors.NewEmailIsNotValid"));

                        if (x.Email.Length > 100)
                            context.AddFailure(
                                translationService.GetResource("Account.EmailUsernameErrors.EmailTooLong"));

                        var customer2 = await customerService.GetCustomerByEmail(x.Email, StoreScope(x));
                        if (customer2 != null && customer.Id != customer2.Id)
                            context.AddFailure(
                                translationService.GetResource("Account.EmailUsernameErrors.EmailAlreadyExists"));
                    }

                    if (customer != null && !string.IsNullOrWhiteSpace(x.Username) && customerSettings.UsernamesEnabled)
                    {
                        if (x.Username.Length > 100)
                            context.AddFailure("Username is too long");

                        var user2 = await customerService.GetCustomerByUsername(x.Username, StoreScope(x));
                        if (user2 != null && customer.Id != user2.Id)
                            context.AddFailure("The username is already in use");
                    }
                });
            });
        }
    }
}