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.

Delete Account - move validator logic from AccountController to Validator

support04c1879d9e0a
diff --git a/src/Web/Grand.Web/Controllers/AccountController.cs b/src/Web/Grand.Web/Controllers/AccountController.cs
index ec19621a7..f3885cd58 100644
--- a/src/Web/Grand.Web/Controllers/AccountController.cs
+++ b/src/Web/Grand.Web/Controllers/AccountController.cs
@@ -777,7 +777,7 @@ namespace Grand.Web.Controllers
[HttpPost]
[AutoValidateAntiforgeryToken]
- public virtual async Task<IActionResult> AddressEdit(CustomerAddressEditModel model, string addressId,
+ public virtual async Task<IActionResult> AddressEdit(CustomerAddressEditModel model,
[FromServices] AddressSettings addressSettings,
[FromServices] IAddressAttributeParser addressAttributeParser)
{
@@ -786,7 +786,7 @@ namespace Grand.Web.Controllers
var customer = _workContext.CurrentCustomer;
//find address (ensure that it belongs to the current customer)
- var address = customer.Addresses.FirstOrDefault(a => a.Id == addressId);
+ var address = customer.Addresses.FirstOrDefault(a => a.Id == model.AddressId);
if (address == null)
//address is not found
return RedirectToRoute("CustomerAddresses");
@@ -923,57 +923,20 @@ namespace Grand.Web.Controllers
if (!ModelState.IsValid) return View(model);
- var loginResult = await _customerManagerService.LoginCustomer(
- _customerSettings.UsernamesEnabled
- ? _workContext.CurrentCustomer.Username
- : _workContext.CurrentCustomer.Email, model.Password);
-
- switch (loginResult)
- {
- case CustomerLoginResults.Successful:
- {
- //delete account
- await _mediator.Send(new DeleteAccountCommand {
- Customer = _workContext.CurrentCustomer, Store = _workContext.CurrentStore,
- IpAddress = HttpContext.Connection?.RemoteIpAddress?.ToString()
- });
+ //delete account
+ await _mediator.Send(new DeleteAccountCommand {
+ Customer = _workContext.CurrentCustomer, Store = _workContext.CurrentStore,
+ IpAddress = HttpContext.Connection?.RemoteIpAddress?.ToString()
+ });
- //standard logout
- await _authenticationService.SignOut();
+ //standard logout
+ await _authenticationService.SignOut();
- //Show success full message
- Success(_translationService.GetResource("Account.Delete.Success"));
+ //Show success full message
+ Success(_translationService.GetResource("Account.Delete.Success"));
- return RedirectToRoute("HomePage");
- }
- case CustomerLoginResults.CustomerNotExist:
- ModelState.AddModelError("",
- _translationService.GetResource("Account.Login.WrongCredentials.CustomerNotExist"));
- break;
- case CustomerLoginResults.Deleted:
- ModelState.AddModelError("",
- _translationService.GetResource("Account.Login.WrongCredentials.Deleted"));
- break;
- case CustomerLoginResults.NotActive:
- ModelState.AddModelError("",
- _translationService.GetResource("Account.Login.WrongCredentials.NotActive"));
- break;
- case CustomerLoginResults.NotRegistered:
- ModelState.AddModelError("",
- _translationService.GetResource("Account.Login.WrongCredentials.NotRegistered"));
- break;
- case CustomerLoginResults.LockedOut:
- ModelState.AddModelError("",
- _translationService.GetResource("Account.Login.WrongCredentials.LockedOut"));
- break;
- case CustomerLoginResults.WrongPassword:
- default:
- ModelState.AddModelError("", _translationService.GetResource("Account.Login.WrongCredentials"));
- break;
- }
+ return RedirectToRoute("HomePage");
- //If we got this far, something failed, redisplay form
- return View(model);
}
#endregion
diff --git a/src/Web/Grand.Web/Models/Customer/CustomerAddressEditModel.cs b/src/Web/Grand.Web/Models/Customer/CustomerAddressEditModel.cs
index d2ffc0da9..28113c452 100644
--- a/src/Web/Grand.Web/Models/Customer/CustomerAddressEditModel.cs
+++ b/src/Web/Grand.Web/Models/Customer/CustomerAddressEditModel.cs
@@ -10,5 +10,6 @@ namespace Grand.Web.Models.Customer
Address = new AddressModel();
}
public AddressModel Address { get; set; }
+ public string AddressId { get; set; }
}
}
\ No newline at end of file
diff --git a/src/Web/Grand.Web/Validators/Customer/DeleteAccountValidator.cs b/src/Web/Grand.Web/Validators/Customer/DeleteAccountValidator.cs
index fa654eee9..da8e49f16 100644
--- a/src/Web/Grand.Web/Validators/Customer/DeleteAccountValidator.cs
+++ b/src/Web/Grand.Web/Validators/Customer/DeleteAccountValidator.cs
@@ -1,5 +1,9 @@
using FluentValidation;
using Grand.Business.Core.Interfaces.Common.Localization;
+using Grand.Business.Core.Interfaces.Common.Security;
+using Grand.Business.Core.Interfaces.Customers;
+using Grand.Domain.Customers;
+using Grand.Infrastructure;
using Grand.Infrastructure.Validators;
using Grand.Web.Models.Customer;
@@ -9,9 +13,25 @@ namespace Grand.Web.Validators.Customer
{
public DeleteAccountValidator(
IEnumerable<IValidatorConsumer<DeleteAccountModel>> validators,
+ IEncryptionService encryptionService, CustomerSettings customerSettings, IWorkContext workContext,
ITranslationService translationService)
: base(validators)
{
RuleFor(x => x.Password).NotEmpty().WithMessage(translationService.GetResource("Account.DeleteAccount.Fields.Password.Required"));
+ RuleFor(x => x).Custom((x, context) =>
+ {
+ var pwd = workContext.CurrentCustomer.PasswordFormatId switch {
+ PasswordFormat.Clear => x.Password,
+ PasswordFormat.Encrypted => encryptionService.EncryptText(x.Password, workContext.CurrentCustomer.PasswordSalt),
+ PasswordFormat.Hashed => encryptionService.CreatePasswordHash(x.Password, workContext.CurrentCustomer.PasswordSalt,
+ customerSettings.HashedPasswordFormat),
+ _ => throw new Exception("PasswordFormat not supported")
+ };
+ var isValid = pwd == workContext.CurrentCustomer.Password;
+ if (!isValid)
+ {
+ context.AddFailure(translationService.GetResource("Account.Login.WrongCredentials"));
+ }
+ });
}}
}
\ No newline at end of file