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.

Change password - move validator logic from CustomerManageService to Validator

supportee9da51b06b8
diff --git a/src/Web/Grand.Web/Controllers/AccountController.cs b/src/Web/Grand.Web/Controllers/AccountController.cs
index b5d8245ff..902d8f9d4 100644
--- a/src/Web/Grand.Web/Controllers/AccountController.cs
+++ b/src/Web/Grand.Web/Controllers/AccountController.cs
@@ -899,13 +899,10 @@ namespace Grand.Web.Controllers
if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
return Challenge();
- var model = new ChangePasswordModel();
-
- var passwordIsExpired = await _mediator.Send(new GetPasswordIsExpiredQuery
- { Customer = _workContext.CurrentCustomer });
- if (passwordIsExpired)
- ModelState.AddModelError(string.Empty,
- _translationService.GetResource("Account.ChangePassword.PasswordIsExpired"));
+ var model = new ChangePasswordModel {
+ PasswordIsExpired = await _mediator.Send(new GetPasswordIsExpiredQuery
+ { Customer = _workContext.CurrentCustomer })
+ };
return View(model);
}
@@ -916,28 +913,17 @@ namespace Grand.Web.Controllers
{
if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
return Challenge();
-
- var customer = _workContext.CurrentCustomer;
-
+
if (!ModelState.IsValid) return View(model);
- var changePasswordRequest = new ChangePasswordRequest(customer.Email,
+ var changePasswordRequest = new ChangePasswordRequest(_workContext.CurrentCustomer.Email,
true, _customerSettings.DefaultPasswordFormat, model.NewPassword, model.OldPassword);
- var changePasswordResult = await _customerManagerService.ChangePassword(changePasswordRequest);
- if (changePasswordResult.Success)
- {
- //sign in
- await _authenticationService.SignIn(customer, true);
+ _ = await _customerManagerService.ChangePassword(changePasswordRequest);
- model.Result = _translationService.GetResource("Account.ChangePassword.Success");
- return View(model);
- }
-
- //errors
- foreach (var error in changePasswordResult.Errors)
- ModelState.AddModelError("", error);
+ //sign in
+ await _authenticationService.SignIn(_workContext.CurrentCustomer, true);
- //If we got this far, something failed, redisplay form
+ model.Result = _translationService.GetResource("Account.ChangePassword.Success");
return View(model);
}
diff --git a/src/Web/Grand.Web/Models/Customer/ChangePasswordModel.cs b/src/Web/Grand.Web/Models/Customer/ChangePasswordModel.cs
index 3abb82854..aeb0e9aec 100644
--- a/src/Web/Grand.Web/Models/Customer/ChangePasswordModel.cs
+++ b/src/Web/Grand.Web/Models/Customer/ChangePasswordModel.cs
@@ -20,5 +20,6 @@ namespace Grand.Web.Models.Customer
public string Result { get; set; }
+ public bool PasswordIsExpired { get; set; }
}
}
\ No newline at end of file
diff --git a/src/Web/Grand.Web/Validators/Customer/ChangePasswordValidator.cs b/src/Web/Grand.Web/Validators/Customer/ChangePasswordValidator.cs
index 9939adea1..cbc65d289 100644
--- a/src/Web/Grand.Web/Validators/Customer/ChangePasswordValidator.cs
+++ b/src/Web/Grand.Web/Validators/Customer/ChangePasswordValidator.cs
@@ -1,6 +1,10 @@
using FluentValidation;
+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.Customers;
+using Grand.Infrastructure;
using Grand.Infrastructure.Validators;
using Grand.Web.Models.Customer;
@@ -10,17 +14,88 @@ namespace Grand.Web.Validators.Customer
{
public ChangePasswordValidator(
IEnumerable<IValidatorConsumer<ChangePasswordModel>> validators,
+ ICustomerService customerService, IWorkContext workContext, IGroupService groupService,
+ IEncryptionService encryptionService, ICustomerHistoryPasswordService customerHistoryPasswordService,
+ ICustomerManagerService customerManagerService,
ITranslationService translationService, CustomerSettings customerSettings)
: base(validators)
{
- RuleFor(x => x.OldPassword).NotEmpty().WithMessage(translationService.GetResource("Account.ChangePassword.Fields.OldPassword.Required"));
- RuleFor(x => x.NewPassword).NotEmpty().WithMessage(translationService.GetResource("Account.ChangePassword.Fields.NewPassword.Required"));
+ RuleFor(x => x.OldPassword).NotEmpty()
+ .WithMessage(translationService.GetResource("Account.ChangePassword.Fields.OldPassword.Required"));
+ RuleFor(x => x.NewPassword).NotEmpty()
+ .WithMessage(translationService.GetResource("Account.ChangePassword.Fields.NewPassword.Required"));
if (!string.IsNullOrEmpty(customerSettings.PasswordRegularExpression))
- RuleFor(x => x.NewPassword).Matches(customerSettings.PasswordRegularExpression).WithMessage(string.Format(translationService.GetResource("Account.ChangePassword.Fields.NewPassword.Validation")));
+ RuleFor(x => x.NewPassword).Matches(customerSettings.PasswordRegularExpression).WithMessage(
+ string.Format(
+ translationService.GetResource("Account.ChangePassword.Fields.NewPassword.Validation")));
- RuleFor(x => x.ConfirmNewPassword).NotEmpty().WithMessage(translationService.GetResource("Account.ChangePassword.Fields.ConfirmNewPassword.Required"));
- RuleFor(x => x.ConfirmNewPassword).Equal(x => x.NewPassword).WithMessage(translationService.GetResource("Account.ChangePassword.Fields.NewPassword.EnteredPasswordsDoNotMatch"));
+ RuleFor(x => x.ConfirmNewPassword).NotEmpty()
+ .WithMessage(
+ translationService.GetResource("Account.ChangePassword.Fields.ConfirmNewPassword.Required"));
+ RuleFor(x => x.ConfirmNewPassword).Equal(x => x.NewPassword).WithMessage(
+ translationService.GetResource("Account.ChangePassword.Fields.NewPassword.EnteredPasswordsDoNotMatch"));
+
+ RuleFor(x => x).CustomAsync(async (x, context, _) =>
+ {
+ var customer = await customerService.GetCustomerById(workContext.CurrentCustomer.Id);
+
+ switch (customer)
+ {
+ case null:
+ context.AddFailure(
+ translationService.GetResource("Account.ChangePassword.Errors.EmailIsNotProvided"));
+ break;
+ case { Deleted: true }:
+ context.AddFailure(
+ translationService.GetResource("Account.ChangePassword.Errors.CustomerDeleted"));
+ break;
+ case { Active: false }:
+ context.AddFailure(
+ translationService.GetResource("Account.ChangePassword.Errors.Customer.CustomerNotActive"));
+ break;
+ case { CannotLoginUntilDateUtc: { } } when customer.CannotLoginUntilDateUtc.Value > DateTime.UtcNow:
+ context.AddFailure(
+ translationService.GetResource("Account.ChangePassword.Errors.Customer.CustomerLockedOut"));
+ break;
+ case { } when !await groupService.IsRegistered(customer):
+ context.AddFailure(
+ translationService.GetResource(
+ "Account.ChangePassword.Errors.Customer.CustomerNotRegistered"));
+ break;
+ }
+
+ if (customer is { })
+ {
+ var oldPwd = customer.PasswordFormatId switch {
+ PasswordFormat.Encrypted => encryptionService.EncryptText(x.OldPassword,
+ customer.PasswordSalt),
+ PasswordFormat.Hashed => encryptionService.CreatePasswordHash(x.OldPassword,
+ customer.PasswordSalt, customerSettings.HashedPasswordFormat),
+ _ => x.OldPassword
+ };
+ if (oldPwd != customer.Password)
+ {
+ context.AddFailure(
+ translationService.GetResource("Account.ChangePassword.Errors.OldPasswordDoesntMatch"));
+ }
+ }
+
+ if (customer is { } && customerSettings.UnduplicatedPasswordsNumber > 0)
+ {
+ //get some of previous passwords
+ var previousPasswords = await customerHistoryPasswordService.GetPasswords(customer.Id,
+ passwordsToReturn: customerSettings.UnduplicatedPasswordsNumber);
+ var newPasswordMatchesWithPrevious = previousPasswords.Any(password =>
+ customerManagerService.PasswordMatch(customerSettings.DefaultPasswordFormat, password.Password,
+ x.NewPassword, password.PasswordSalt));
+ if (newPasswordMatchesWithPrevious)
+ {
+ context.AddFailure(
+ translationService.GetResource("Account.ChangePassword.Errors.PasswordMatchesWithPrevious"));
+ }
+ }
+ });
}
}
}
\ No newline at end of file
diff --git a/src/Web/Grand.Web/Views/Account/ChangePassword.cshtml b/src/Web/Grand.Web/Views/Account/ChangePassword.cshtml
index 0c8dc3033..4d5fac996 100644
--- a/src/Web/Grand.Web/Views/Account/ChangePassword.cshtml
+++ b/src/Web/Grand.Web/Views/Account/ChangePassword.cshtml
@@ -20,8 +20,12 @@
@Model.Result
</section>
}
- <div asp-validation-summary="ModelOnly" class="message-error"></div>
-
+ <div asp-validation-summary="All" class="message-error"></div>
+ @if (Model.PasswordIsExpired)
+ {
+ <div class="message-error">@Loc["Account.ChangePassword.PasswordIsExpired"]</div>
+ <input type="hidden" [email protected]/>
+ }
<fieldset>
<div class="form-fields">
<fieldset class="form-group">