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.

VendorController - move validator logic from Controller to Validator

support01b36b6d2949
diff --git a/src/Web/Grand.Web/Controllers/VendorController.cs b/src/Web/Grand.Web/Controllers/VendorController.cs
index df32120cd..53eecd2ca 100644
--- a/src/Web/Grand.Web/Controllers/VendorController.cs
+++ b/src/Web/Grand.Web/Controllers/VendorController.cs
@@ -131,22 +131,15 @@ namespace Grand.Web.Controllers
[HttpPost, ActionName("ApplyVendor")]
[AutoValidateAntiforgeryToken]
- [ValidateCaptcha]
[DenySystemAccount]
- public virtual async Task<IActionResult> ApplyVendorSubmit(ApplyVendorModel model, bool captchaValid, IFormFile uploadedFile)
+ public virtual async Task<IActionResult> ApplyVendorSubmit(ApplyVendorModel model, IFormFile uploadedFile)
{
if (!_vendorSettings.AllowCustomersToApplyForVendorAccount)
return RedirectToRoute("HomePage");
if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
return Challenge();
-
- //validate CAPTCHA
- if (_captchaSettings.Enabled && _captchaSettings.ShowOnApplyVendorPage && !captchaValid)
- {
- ModelState.AddModelError("", _captchaSettings.GetWrongCaptchaMessage(_translationService));
- }
-
+
var contentType = string.Empty;
byte[] vendorPictureBinary = null;
@@ -296,7 +289,7 @@ namespace Grand.Web.Controllers
if (prevPicture == null)
vendor.PictureId = "";
- if (ModelState is { IsValid: true, ErrorCount: 0 })
+ if (ModelState.IsValid)
{
var description = FormatText.ConvertText(model.Description);
@@ -363,11 +356,10 @@ namespace Grand.Web.Controllers
}
- [HttpPost, ActionName("ContactVendor")]
+ [HttpPost]
[AutoValidateAntiforgeryToken]
- [ValidateCaptcha]
[DenySystemAccount]
- public virtual async Task<IActionResult> ContactVendor(ContactVendorModel model, bool captchaValid)
+ public virtual async Task<IActionResult> ContactVendor(ContactVendorModel model)
{
if (!_vendorSettings.AllowCustomersToContactVendors)
return Content("");
@@ -376,14 +368,6 @@ namespace Grand.Web.Controllers
if (vendor is not { Active: true } || vendor.Deleted)
return Content("");
- //validate CAPTCHA
- if (_captchaSettings.Enabled && _captchaSettings.ShowOnContactUsPage && !captchaValid)
- {
- ModelState.AddModelError("", _captchaSettings.GetWrongCaptchaMessage(_translationService));
- }
-
- model.VendorName = vendor.GetTranslation(x => x.Name, _workContext.WorkingLanguage.Id);
-
if (ModelState.IsValid)
{
model = await _mediator.Send(new ContactVendorSendCommand { Model = model, Vendor = vendor, Store = _workContext.CurrentStore, IpAddress = HttpContext.Connection.RemoteIpAddress?.ToString() });
diff --git a/src/Web/Grand.Web/Models/Vendors/ApplyVendorModel.cs b/src/Web/Grand.Web/Models/Vendors/ApplyVendorModel.cs
index f8ce6f0be..e5ecab015 100644
--- a/src/Web/Grand.Web/Models/Vendors/ApplyVendorModel.cs
+++ b/src/Web/Grand.Web/Models/Vendors/ApplyVendorModel.cs
@@ -1,5 +1,6 @@
using Grand.Infrastructure.ModelBinding;
using Grand.Infrastructure.Models;
+using Grand.Web.Models.Common;
using System.ComponentModel.DataAnnotations;
namespace Grand.Web.Models.Vendors
@@ -22,6 +23,7 @@ namespace Grand.Web.Models.Vendors
[GrandResourceDisplayName("Vendors.ApplyAccount.Description")]
public string Description { get; set; }
public bool DisplayCaptcha { get; set; }
+ public ICaptchaValidModel Captcha { get; set; } = new CaptchaModel();
public bool TermsOfServiceEnabled { get; set; }
public bool TermsOfServicePopup { get; set; }
public bool DisableFormInput { get; set; }
diff --git a/src/Web/Grand.Web/Models/Vendors/ContactVendorModel.cs b/src/Web/Grand.Web/Models/Vendors/ContactVendorModel.cs
index 5628575d3..e69682141 100644
--- a/src/Web/Grand.Web/Models/Vendors/ContactVendorModel.cs
+++ b/src/Web/Grand.Web/Models/Vendors/ContactVendorModel.cs
@@ -1,5 +1,6 @@
using Grand.Infrastructure.ModelBinding;
using Grand.Infrastructure.Models;
+using Grand.Web.Models.Common;
using System.ComponentModel.DataAnnotations;
namespace Grand.Web.Models.Vendors
@@ -27,5 +28,6 @@ namespace Grand.Web.Models.Vendors
public string Result { get; set; }
public bool DisplayCaptcha { get; set; }
+ public ICaptchaValidModel Captcha { get; set; } = new CaptchaModel();
}
}
\ No newline at end of file
diff --git a/src/Web/Grand.Web/Validators/Vendors/ApplyVendorValidator.cs b/src/Web/Grand.Web/Validators/Vendors/ApplyVendorValidator.cs
index 392d8ad3c..8d09ee415 100644
--- a/src/Web/Grand.Web/Validators/Vendors/ApplyVendorValidator.cs
+++ b/src/Web/Grand.Web/Validators/Vendors/ApplyVendorValidator.cs
@@ -2,8 +2,12 @@
using Grand.Business.Core.Interfaces.Common.Directory;
using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Domain.Vendors;
+using Grand.Infrastructure.Models;
using Grand.Infrastructure.Validators;
+using Grand.Web.Common.Security.Captcha;
+using Grand.Web.Common.Validators;
using Grand.Web.Models.Vendors;
+using Microsoft.AspNetCore.Http;
namespace Grand.Web.Validators.Vendors
{
@@ -11,8 +15,11 @@ namespace Grand.Web.Validators.Vendors
{
public ApplyVendorValidator(
IEnumerable<IValidatorConsumer<ApplyVendorModel>> validators,
+ IEnumerable<IValidatorConsumer<ICaptchaValidModel>> validatorsCaptcha,
IEnumerable<IValidatorConsumer<VendorAddressModel>> addressvalidators,
ITranslationService translationService, ICountryService countryService,
+ CaptchaSettings captchaSettings,
+ IHttpContextAccessor contextAccessor, GoogleReCaptchaValidator googleReCaptchaValidator,
VendorSettings addressSettings)
: base(validators)
{
@@ -20,6 +27,13 @@ namespace Grand.Web.Validators.Vendors
RuleFor(x => x.Email).NotEmpty().WithMessage(translationService.GetResource("Vendors.ApplyAccount.Email.Required"));
RuleFor(x => x.Email).EmailAddress().WithMessage(translationService.GetResource("Common.WrongEmail"));
RuleFor(x => x.Address).SetValidator(new VendorAddressValidator(addressvalidators, translationService, countryService, addressSettings));
+
+ if (captchaSettings.Enabled && captchaSettings.ShowOnApplyVendorPage)
+ {
+ RuleFor(x => x.Captcha).NotNull().WithMessage(translationService.GetResource("Account.Captcha.Required"));
+ RuleFor(x => x.Captcha).SetValidator(new CaptchaValidator(validatorsCaptcha, contextAccessor, googleReCaptchaValidator));
+ }
+
}
}
}
\ No newline at end of file
diff --git a/src/Web/Grand.Web/Validators/Vendors/ContactVendorValidator.cs b/src/Web/Grand.Web/Validators/Vendors/ContactVendorValidator.cs
index 870478f25..c9ab506a1 100644
--- a/src/Web/Grand.Web/Validators/Vendors/ContactVendorValidator.cs
+++ b/src/Web/Grand.Web/Validators/Vendors/ContactVendorValidator.cs
@@ -1,8 +1,12 @@
using FluentValidation;
using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Domain.Common;
+using Grand.Infrastructure.Models;
using Grand.Infrastructure.Validators;
+using Grand.Web.Common.Security.Captcha;
+using Grand.Web.Common.Validators;
using Grand.Web.Models.Vendors;
+using Microsoft.AspNetCore.Http;
namespace Grand.Web.Validators.Vendors
{
@@ -10,6 +14,9 @@ namespace Grand.Web.Validators.Vendors
{
public ContactVendorValidator(
IEnumerable<IValidatorConsumer<ContactVendorModel>> validators,
+ IEnumerable<IValidatorConsumer<ICaptchaValidModel>> validatorsCaptcha,
+ CaptchaSettings captchaSettings,
+ IHttpContextAccessor contextAccessor, GoogleReCaptchaValidator googleReCaptchaValidator,
ITranslationService translationService, CommonSettings commonSettings)
: base(validators)
{
@@ -21,5 +28,11 @@ namespace Grand.Web.Validators.Vendors
RuleFor(x => x.Subject).NotEmpty().WithMessage(translationService.GetResource("ContactVendor.Subject.Required"));
}
RuleFor(x => x.Enquiry).NotEmpty().WithMessage(translationService.GetResource("ContactVendor.Enquiry.Required"));
+
+ if (captchaSettings.Enabled && captchaSettings.ShowOnContactUsPage)
+ {
+ RuleFor(x => x.Captcha).NotNull().WithMessage(translationService.GetResource("Account.Captcha.Required"));
+ RuleFor(x => x.Captcha).SetValidator(new CaptchaValidator(validatorsCaptcha, contextAccessor, googleReCaptchaValidator));
+ }
}}
}
\ No newline at end of file