Merge pull request #16681 from tntwist/fix-new-password

fix(): ensure new password is different from current password
pull/16699/head
maliming 2 years ago committed by GitHub
commit 072a238411
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,11 +1,16 @@
using System.ComponentModel.DataAnnotations; using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using Volo.Abp.Account.Localization;
using Volo.Abp.Auditing; using Volo.Abp.Auditing;
using Volo.Abp.Identity; using Volo.Abp.Identity;
using Volo.Abp.Validation; using Volo.Abp.Validation;
namespace Volo.Abp.Account; namespace Volo.Abp.Account;
public class ChangePasswordInput public class ChangePasswordInput : IValidatableObject
{ {
[DisableAuditing] [DisableAuditing]
[DynamicStringLength(typeof(IdentityUserConsts), nameof(IdentityUserConsts.MaxPasswordLength))] [DynamicStringLength(typeof(IdentityUserConsts), nameof(IdentityUserConsts.MaxPasswordLength))]
@ -15,4 +20,17 @@ public class ChangePasswordInput
[DisableAuditing] [DisableAuditing]
[DynamicStringLength(typeof(IdentityUserConsts), nameof(IdentityUserConsts.MaxPasswordLength))] [DynamicStringLength(typeof(IdentityUserConsts), nameof(IdentityUserConsts.MaxPasswordLength))]
public string NewPassword { get; set; } public string NewPassword { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (CurrentPassword == NewPassword)
{
var localizer = validationContext.GetRequiredService<IStringLocalizer<AccountResource>>();
yield return new ValidationResult(
localizer["NewPasswordSameAsOld"],
new[] { nameof(CurrentPassword), nameof(NewPassword) }
);
}
}
} }

@ -48,6 +48,12 @@ public partial class AccountManage
return; return;
} }
if (ChangePasswordModel.CurrentPassword == ChangePasswordModel.NewPassword)
{
await UiMessageService.Warn(L["NewPasswordSameAsOld"]);
return;
}
await ProfileAppService.ChangePasswordAsync(new ChangePasswordInput await ProfileAppService.ChangePasswordAsync(new ChangePasswordInput
{ {
CurrentPassword = ChangePasswordModel.CurrentPassword, CurrentPassword = ChangePasswordModel.CurrentPassword,

@ -4,6 +4,7 @@ using Microsoft.Extensions.DependencyInjection;
using NSubstitute; using NSubstitute;
using Shouldly; using Shouldly;
using Volo.Abp.Users; using Volo.Abp.Users;
using Volo.Abp.Validation;
using Xunit; using Xunit;
namespace Volo.Abp.Account; namespace Volo.Abp.Account;
@ -72,6 +73,27 @@ public class ProfileAppService_Tests : AbpAccountApplicationTestBase
result.Name.ShouldBe(input.Name); result.Name.ShouldBe(input.Name);
} }
[Fact]
public async Task ChangePasswordAsync_FailsForSamePassword()
{
//Arrange
_currentUser.Id.Returns(_testData.UserJohnId);
_currentUser.IsAuthenticated.Returns(true);
//Act
var ex = await _profileAppService.ChangePasswordAsync(new()
{
CurrentPassword = "SomePassword123!",
NewPassword = "SomePassword123!"
}).ShouldThrowAsync<AbpValidationException>();
//Assert
ex.ValidationErrors.ShouldNotBeEmpty();
var firstError = ex.ValidationErrors[0];
firstError.MemberNames.ShouldContain(nameof(ChangePasswordInput.CurrentPassword));
firstError.MemberNames.ShouldContain(nameof(ChangePasswordInput.NewPassword));
}
private static string CreateRandomEmail() private static string CreateRandomEmail()
{ {
return CreateRandomString() + "@abp.io"; return CreateRandomString() + "@abp.io";

Loading…
Cancel
Save