diff options
author | crupest <crupest@outlook.com> | 2019-04-22 15:47:52 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2019-04-22 15:47:52 +0800 |
commit | 80d686a8b875cce854e4291cbe4a53e7a03e3eff (patch) | |
tree | 5285f03bbc10efe6319e9487bc1de453a8efd859 /Timeline/Services/UserService.cs | |
parent | 407f97db0be86aa071802b67bfdeadc7703528c9 (diff) | |
download | timeline-80d686a8b875cce854e4291cbe4a53e7a03e3eff.tar.gz timeline-80d686a8b875cce854e4291cbe4a53e7a03e3eff.tar.bz2 timeline-80d686a8b875cce854e4291cbe4a53e7a03e3eff.zip |
Add change password api.
Diffstat (limited to 'Timeline/Services/UserService.cs')
-rw-r--r-- | Timeline/Services/UserService.cs | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/Timeline/Services/UserService.cs b/Timeline/Services/UserService.cs index 8615d0c5..75ad3331 100644 --- a/Timeline/Services/UserService.cs +++ b/Timeline/Services/UserService.cs @@ -49,6 +49,22 @@ namespace Timeline.Services NotExists } + public enum ChangePasswordResult + { + /// <summary> + /// Success to change password. + /// </summary> + Success, + /// <summary> + /// The user does not exists. + /// </summary> + NotExists, + /// <summary> + /// Old password is wrong. + /// </summary> + BadOldPassword + } + public interface IUserService { /// <summary> @@ -113,6 +129,17 @@ namespace Timeline.Services /// <returns><see cref="DeleteUserResult.Deleted"/> if the user is deleted. /// <see cref="DeleteUserResult.NotExists"/> if the user doesn't exist.</returns> Task<DeleteUserResult> DeleteUser(string username); + + /// <summary> + /// Try to change a user's password with old password. + /// </summary> + /// <param name="username">The name of user to change password of.</param> + /// <param name="oldPassword">The user's old password.</param> + /// <param name="newPassword">The user's new password.</param> + /// <returns><see cref="ChangePasswordResult.Success"/> if success. + /// <see cref="ChangePasswordResult.NotExists"/> if user does not exist. + /// <see cref="ChangePasswordResult.BadOldPassword"/> if old password is wrong.</returns> + Task<ChangePasswordResult> ChangePassword(string username, string oldPassword, string newPassword); } public class UserService : IUserService @@ -252,5 +279,20 @@ namespace Timeline.Services await _databaseContext.SaveChangesAsync(); return DeleteUserResult.Deleted; } + + public async Task<ChangePasswordResult> ChangePassword(string username, string oldPassword, string newPassword) + { + var user = await _databaseContext.Users.Where(u => u.Name == username).SingleOrDefaultAsync(); + if (user == null) + return ChangePasswordResult.NotExists; + + var verifyResult = _passwordService.VerifyPassword(user.EncryptedPassword, oldPassword); + if (!verifyResult) + return ChangePasswordResult.BadOldPassword; + + user.EncryptedPassword = _passwordService.HashPassword(newPassword); + await _databaseContext.SaveChangesAsync(); + return ChangePasswordResult.Success; + } } } |