Skip to content

Commit 3419aa0

Browse files
committed
1 parent 435fb70 commit 3419aa0

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

aspnet-core/src/AbpCompanyName.AbpProjectName.Application/Users/UserAppService.cs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Text.RegularExpressions;
45
using System.Threading.Tasks;
@@ -188,43 +189,48 @@ protected virtual void CheckErrors(IdentityResult identityResult)
188189

189190
public async Task<bool> ChangePassword(ChangePasswordDto input)
190191
{
191-
if (_abpSession.UserId == null)
192+
await _userManager.InitializeOptionsAsync(AbpSession.TenantId);
193+
194+
var user = await _userManager.FindByIdAsync(AbpSession.GetUserId().ToString());
195+
if (user == null)
192196
{
193-
throw new UserFriendlyException("Please log in before attemping to change password.");
197+
throw new Exception("There is no current user!");
194198
}
195-
long userId = _abpSession.UserId.Value;
196-
var user = await _userManager.GetUserByIdAsync(userId);
197-
var loginAsync = await _logInManager.LoginAsync(user.UserName, input.CurrentPassword, shouldLockout: false);
198-
if (loginAsync.Result != AbpLoginResultType.Success)
199+
200+
if (await _userManager.CheckPasswordAsync(user, input.CurrentPassword))
199201
{
200-
throw new UserFriendlyException("Your 'Existing Password' did not match the one on record. Please try again or contact an administrator for assistance in resetting your password.");
202+
CheckErrors(await _userManager.ChangePasswordAsync(user, input.NewPassword));
201203
}
202-
if (!new Regex(AccountAppService.PasswordRegex).IsMatch(input.NewPassword))
204+
else
203205
{
204-
throw new UserFriendlyException("Passwords must be at least 8 characters, contain a lowercase, uppercase, and number.");
206+
CheckErrors(IdentityResult.Failed(new IdentityError
207+
{
208+
Description = "Incorrect password."
209+
}));
205210
}
206-
user.Password = _passwordHasher.HashPassword(user, input.NewPassword);
207-
CurrentUnitOfWork.SaveChanges();
211+
208212
return true;
209213
}
210214

211215
public async Task<bool> ResetPassword(ResetPasswordDto input)
212216
{
213217
if (_abpSession.UserId == null)
214218
{
215-
throw new UserFriendlyException("Please log in before attemping to reset password.");
219+
throw new UserFriendlyException("Please log in before attempting to reset password.");
216220
}
217-
long currentUserId = _abpSession.UserId.Value;
218-
var currentUser = await _userManager.GetUserByIdAsync(currentUserId);
221+
222+
var currentUser = await _userManager.GetUserByIdAsync(_abpSession.GetUserId());
219223
var loginAsync = await _logInManager.LoginAsync(currentUser.UserName, input.AdminPassword, shouldLockout: false);
220224
if (loginAsync.Result != AbpLoginResultType.Success)
221225
{
222226
throw new UserFriendlyException("Your 'Admin Password' did not match the one on record. Please try again.");
223227
}
228+
224229
if (currentUser.IsDeleted || !currentUser.IsActive)
225230
{
226231
return false;
227232
}
233+
228234
var roles = await _userManager.GetRolesAsync(currentUser);
229235
if (!roles.Contains(StaticRoleNames.Tenants.Admin))
230236
{
@@ -235,7 +241,7 @@ public async Task<bool> ResetPassword(ResetPasswordDto input)
235241
if (user != null)
236242
{
237243
user.Password = _passwordHasher.HashPassword(user, input.NewPassword);
238-
CurrentUnitOfWork.SaveChanges();
244+
await CurrentUnitOfWork.SaveChangesAsync();
239245
}
240246

241247
return true;

0 commit comments

Comments
 (0)