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.

Customer Info - move validator logic from CustomerManageService/AccountController to Validator

supportbd9aa2b3a96a
diff --git a/src/Web/Grand.Web/Controllers/AccountController.cs b/src/Web/Grand.Web/Controllers/AccountController.cs
index b6df37d0a..ec19621a7 100644
--- a/src/Web/Grand.Web/Controllers/AccountController.cs
+++ b/src/Web/Grand.Web/Controllers/AccountController.cs
@@ -561,41 +561,24 @@ namespace Grand.Web.Controllers
[HttpPost]
[AutoValidateAntiforgeryToken]
- public virtual async Task<IActionResult> Info(CustomerInfoModel model,
- [FromServices] ICustomerAttributeParser customerAttributeParser)
+ public virtual async Task<IActionResult> Info(CustomerInfoModel model)
{
if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
return Challenge();
- //custom customer attributes
- var customerAttributes = await _mediator.Send(new GetParseCustomAttributes {
- SelectedAttributes = model.SelectedAttributes,
- CustomerCustomAttribute = _workContext.CurrentCustomer.Attributes.ToList()
- });
- var customerAttributeWarnings = await customerAttributeParser.GetAttributeWarnings(customerAttributes);
-
- foreach (var error in customerAttributeWarnings)
- {
- ModelState.AddModelError("", error);
- }
-
- try
- {
- if (ModelState is { IsValid: true, ErrorCount: 0 })
- {
- await _mediator.Send(new UpdateCustomerInfoCommand {
- Customer = _workContext.CurrentCustomer,
- CustomerAttributes = customerAttributes,
- Model = model,
- OriginalCustomerIfImpersonated = _workContext.OriginalCustomerIfImpersonated,
- Store = _workContext.CurrentStore
- });
- return RedirectToRoute("CustomerInfo");
- }
- }
- catch (Exception exc)
+ if (ModelState.IsValid)
{
- ModelState.AddModelError("", exc.Message);
+ await _mediator.Send(new UpdateCustomerInfoCommand {
+ Customer = _workContext.CurrentCustomer,
+ CustomerAttributes = await _mediator.Send(new GetParseCustomAttributes {
+ SelectedAttributes = model.SelectedAttributes,
+ CustomerCustomAttribute = _workContext.CurrentCustomer.Attributes.ToList()
+ }),
+ Model = model,
+ OriginalCustomerIfImpersonated = _workContext.OriginalCustomerIfImpersonated,
+ Store = _workContext.CurrentStore
+ });
+ return RedirectToRoute("CustomerInfo");
}
//If we got this far, something failed, redisplay form
@@ -605,7 +588,10 @@ namespace Grand.Web.Controllers
ExcludeProperties = true,
Language = _workContext.WorkingLanguage,
Store = _workContext.CurrentStore,
- OverrideCustomCustomerAttributes = customerAttributes
+ OverrideCustomCustomerAttributes = await _mediator.Send(new GetParseCustomAttributes {
+ SelectedAttributes = model.SelectedAttributes,
+ CustomerCustomAttribute = _workContext.CurrentCustomer.Attributes.ToList()
+ })
});
return View(model);
@@ -637,7 +623,6 @@ namespace Grand.Web.Controllers
});
}
- //TODO
public virtual async Task<IActionResult> Export()
{
if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
diff --git a/src/Web/Grand.Web/Validators/Customer/CustomerInfoValidator.cs b/src/Web/Grand.Web/Validators/Customer/CustomerInfoValidator.cs
index 3a3a65f71..e9cac38ec 100644
--- a/src/Web/Grand.Web/Validators/Customer/CustomerInfoValidator.cs
+++ b/src/Web/Grand.Web/Validators/Customer/CustomerInfoValidator.cs
@@ -1,10 +1,14 @@
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.Validators;
using Grand.SharedKernel.Extensions;
+using Grand.Web.Features.Models.Customers;
using Grand.Web.Models.Customer;
+using MediatR;
namespace Grand.Web.Validators.Customer
{
@@ -12,23 +16,30 @@ namespace Grand.Web.Validators.Customer
{
public CustomerInfoValidator(
IEnumerable<IValidatorConsumer<CustomerInfoModel>> validators,
+ IWorkContext workContext,
+ ICustomerService customerService,
+ IMediator mediator, ICustomerAttributeParser customerAttributeParser,
ITranslationService translationService,
ICountryService countryService,
CustomerSettings customerSettings)
: base(validators)
{
- RuleFor(x => x.Email).NotEmpty().WithMessage(translationService.GetResource("Account.Fields.Email.Required"));
+ RuleFor(x => x.Email).NotEmpty()
+ .WithMessage(translationService.GetResource("Account.Fields.Email.Required"));
RuleFor(x => x.Email).EmailAddress().WithMessage(translationService.GetResource("Common.WrongEmail"));
if (customerSettings.FirstLastNameRequired)
{
- RuleFor(x => x.FirstName).NotEmpty().WithMessage(translationService.GetResource("Account.Fields.FirstName.Required"));
- RuleFor(x => x.LastName).NotEmpty().WithMessage(translationService.GetResource("Account.Fields.LastName.Required"));
+ RuleFor(x => x.FirstName).NotEmpty()
+ .WithMessage(translationService.GetResource("Account.Fields.FirstName.Required"));
+ RuleFor(x => x.LastName).NotEmpty()
+ .WithMessage(translationService.GetResource("Account.Fields.LastName.Required"));
}
if (customerSettings.UsernamesEnabled && customerSettings.AllowUsersToChangeUsernames)
{
- RuleFor(x => x.Username).NotEmpty().WithMessage(translationService.GetResource("Account.Fields.Username.Required"));
+ RuleFor(x => x.Username).NotEmpty()
+ .WithMessage(translationService.GetResource("Account.Fields.Username.Required"));
}
//form fields
@@ -41,6 +52,7 @@ namespace Grand.Web.Validators.Customer
.NotEqual("")
.WithMessage(translationService.GetResource("Address.Fields.Country.Required"));
}
+
if (customerSettings.CountryEnabled &&
customerSettings.StateProvinceEnabled &&
customerSettings.StateProvinceRequired)
@@ -55,9 +67,11 @@ namespace Grand.Web.Validators.Customer
{
return false;
}
+
return country.StateProvinces.FirstOrDefault(s => s.Id == y) != null;
}).WithMessage(translationService.GetResource("Account.Fields.StateProvince.Required"));
}
+
if (customerSettings.DateOfBirthEnabled && customerSettings.DateOfBirthRequired)
{
RuleFor(x => x.DateOfBirthDay).Must((x, _) =>
@@ -73,36 +87,82 @@ namespace Grand.Web.Validators.Customer
return !dateOfBirth.HasValue || !customerSettings.DateOfBirthMinimumAge.HasValue ||
CommonHelper.GetDifferenceInYears(dateOfBirth.Value, DateTime.Today) >=
customerSettings.DateOfBirthMinimumAge.Value;
- }).WithMessage(string.Format(translationService.GetResource("Account.Fields.DateOfBirth.MinimumAge"), customerSettings.DateOfBirthMinimumAge));
+ }).WithMessage(string.Format(translationService.GetResource("Account.Fields.DateOfBirth.MinimumAge"),
+ customerSettings.DateOfBirthMinimumAge));
}
+
if (customerSettings.CompanyRequired && customerSettings.CompanyEnabled)
{
- RuleFor(x => x.Company).NotEmpty().WithMessage(translationService.GetResource("Account.Fields.Company.Required"));
+ RuleFor(x => x.Company).NotEmpty()
+ .WithMessage(translationService.GetResource("Account.Fields.Company.Required"));
}
+
if (customerSettings.StreetAddressRequired && customerSettings.StreetAddressEnabled)
{
- RuleFor(x => x.StreetAddress).NotEmpty().WithMessage(translationService.GetResource("Account.Fields.StreetAddress.Required"));
+ RuleFor(x => x.StreetAddress).NotEmpty()
+ .WithMessage(translationService.GetResource("Account.Fields.StreetAddress.Required"));
}
+
if (customerSettings.StreetAddress2Required && customerSettings.StreetAddress2Enabled)
{
- RuleFor(x => x.StreetAddress2).NotEmpty().WithMessage(translationService.GetResource("Account.Fields.StreetAddress2.Required"));
+ RuleFor(x => x.StreetAddress2).NotEmpty()
+ .WithMessage(translationService.GetResource("Account.Fields.StreetAddress2.Required"));
}
+
if (customerSettings.ZipPostalCodeRequired && customerSettings.ZipPostalCodeEnabled)
{
- RuleFor(x => x.ZipPostalCode).NotEmpty().WithMessage(translationService.GetResource("Account.Fields.ZipPostalCode.Required"));
+ RuleFor(x => x.ZipPostalCode).NotEmpty()
+ .WithMessage(translationService.GetResource("Account.Fields.ZipPostalCode.Required"));
}
+
if (customerSettings.CityRequired && customerSettings.CityEnabled)
{
- RuleFor(x => x.City).NotEmpty().WithMessage(translationService.GetResource("Account.Fields.City.Required"));
+ RuleFor(x => x.City).NotEmpty()
+ .WithMessage(translationService.GetResource("Account.Fields.City.Required"));
}
+
if (customerSettings.PhoneRequired && customerSettings.PhoneEnabled)
{
- RuleFor(x => x.Phone).NotEmpty().WithMessage(translationService.GetResource("Account.Fields.Phone.Required"));
+ RuleFor(x => x.Phone).NotEmpty()
+ .WithMessage(translationService.GetResource("Account.Fields.Phone.Required"));
}
+
if (customerSettings.FaxRequired && customerSettings.FaxEnabled)
{
- RuleFor(x => x.Fax).NotEmpty().WithMessage(translationService.GetResource("Account.Fields.Fax.Required"));
+ RuleFor(x => x.Fax).NotEmpty()
+ .WithMessage(translationService.GetResource("Account.Fields.Fax.Required"));
}
+
+ RuleFor(x => x).CustomAsync(async (x, context, _) =>
+ {
+ var customerAttributes = await mediator.Send(new GetParseCustomAttributes
+ { SelectedAttributes = x.SelectedAttributes }, _);
+ var customerAttributeWarnings = await customerAttributeParser.GetAttributeWarnings(customerAttributes);
+ foreach (var error in customerAttributeWarnings)
+ {
+ context.AddFailure(error);
+ }
+
+ if (workContext.CurrentCustomer.Email != x.Email.ToLower() && customerSettings.AllowUsersToChangeEmail)
+ {
+ if (x.Email.Length > 100)
+ context.AddFailure(translationService.GetResource("Account.EmailUsernameErrors.EmailTooLong"));
+
+ var customer2 = await customerService.GetCustomerByEmail(x.Email);
+ if (customer2 != null)
+ context.AddFailure(
+ translationService.GetResource("Account.EmailUsernameErrors.EmailAlreadyExists"));
+ }
+
+ if (customerSettings.UsernamesEnabled && customerSettings.AllowUsersToChangeUsernames &&
+ workContext.CurrentCustomer.Username != x.Username.ToLower())
+ {
+ var customer2 = await customerService.GetCustomerByUsername(x.Username);
+ if (customer2 != null)
+ context.AddFailure(
+ translationService.GetResource("Account.EmailUsernameErrors.UsernameAlreadyExists"));
+ }
+ });
}
}
}
\ No newline at end of file