New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Revise how to change the password for external logins #4975
Changes from 4 commits
33f3916
d737b93
09ba60f
8c3b9b9
36fa40e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,18 @@ | ||
namespace Volo.Abp.Identity | ||
using System.ComponentModel.DataAnnotations; | ||
using Volo.Abp.Auditing; | ||
using Volo.Abp.Validation; | ||
|
||
namespace Volo.Abp.Identity | ||
{ | ||
public class ChangePasswordInput | ||
{ | ||
[DisableAuditing] | ||
[DynamicStringLength(typeof(IdentityUserConsts), nameof(IdentityUserConsts.MaxPasswordLength))] | ||
public string CurrentPassword { get; set; } | ||
|
||
[Required] | ||
[DisableAuditing] | ||
[DynamicStringLength(typeof(IdentityUserConsts), nameof(IdentityUserConsts.MaxPasswordLength))] | ||
public string NewPassword { get; set; } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
using System.Threading.Tasks; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Identity; | ||
using Volo.Abp.Identity.Settings; | ||
|
@@ -20,9 +21,13 @@ public ProfileAppService(IdentityUserManager userManager) | |
|
||
public virtual async Task<ProfileDto> GetAsync() | ||
{ | ||
return ObjectMapper.Map<IdentityUser, ProfileDto>( | ||
await UserManager.GetByIdAsync(CurrentUser.GetId()) | ||
); | ||
var currentUser = await UserManager.GetByIdAsync(CurrentUser.GetId()); | ||
|
||
var profile = ObjectMapper.Map<IdentityUser, ProfileDto>(currentUser); | ||
profile.IsExternalLoggedIn = currentUser.IsExternal; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you rename |
||
profile.HasPassword = currentUser.PasswordHash != null; | ||
|
||
return profile; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After changing the mapping to fully use automapper, you can rollback changes in this method. |
||
} | ||
|
||
public virtual async Task<ProfileDto> UpdateAsync(UpdateProfileDto input) | ||
|
@@ -56,6 +61,20 @@ public virtual async Task<ProfileDto> UpdateAsync(UpdateProfileDto input) | |
public virtual async Task ChangePasswordAsync(ChangePasswordInput input) | ||
{ | ||
var currentUser = await UserManager.GetByIdAsync(CurrentUser.GetId()); | ||
|
||
if (currentUser.IsExternal) | ||
{ | ||
throw new BusinessException(code: IdentityErrorCodes.ExternalUserPasswordChange); | ||
} | ||
|
||
if (currentUser.PasswordHash == null) | ||
{ | ||
(await UserManager.RemovePasswordAsync(currentUser)).CheckErrors(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it really need to |
||
(await UserManager.AddPasswordAsync(currentUser, input.NewPassword)).CheckErrors(); | ||
|
||
return; | ||
} | ||
|
||
(await UserManager.ChangePasswordAsync(currentUser, input.CurrentPassword, input.NewPassword)).CheckErrors(); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of ignoring
HasPassword
, you can map it with a custom lambda expression that checksPasswordHash != null
. In this way, you don't need to manual mapping later.