From b89d88ffb286641bc876a2b4ba9d86ac34d992b9 Mon Sep 17 00:00:00 2001 From: AnonymousDotNet <18776095145@163.com> Date: Tue, 17 Sep 2024 18:31:38 +0800 Subject: [PATCH] Add update password API --- .../Users/IUserService.cs | 1 + .../Users/Services/UserService.cs | 22 +++++++++++++++++++ .../Controllers/UserController.cs | 10 +++++++++ 3 files changed, 33 insertions(+) diff --git a/src/Infrastructure/BotSharp.Abstraction/Users/IUserService.cs b/src/Infrastructure/BotSharp.Abstraction/Users/IUserService.cs index f35656ca..d854e2fa 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Users/IUserService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Users/IUserService.cs @@ -16,4 +16,5 @@ public interface IUserService Task ResetUserPassword(User user); Task ModifyUserEmail(string email); Task ModifyUserPhone(string phone); + Task UpdatePassword(string newPassword, string verificationCode); } \ No newline at end of file diff --git a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs index 42b708d6..28b1a8ea 100644 --- a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs +++ b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs @@ -83,6 +83,28 @@ public class UserService : IUserService return record; } + public async Task UpdatePassword(string password, string verificationCode) + { + var db = _services.GetRequiredService(); + var record = db.GetUserByUserName(_user.UserName); + + if (record == null) + { + return false; + } + + if (record.VerificationCode != verificationCode) + { + return false; + } + + var salt = Guid.NewGuid().ToString("N"); + record.Password = Utilities.HashTextMd5($"{password}{salt}"); + + db.UpdateUserPassword(record.Id, password); + return true; + } + public async Task GetToken(string authorization) { var base64 = Encoding.UTF8.GetString(Convert.FromBase64String(authorization)); diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs index ed9604ac..6a9b0055 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs @@ -121,6 +121,16 @@ public class UserController : ControllerBase return await _userService.ResetUserPassword(user.ToUser()); } + [HttpPost("/user/updatepassword")] + public async Task UpdatePassword([FromBody] User user) + { + if (string.IsNullOrWhiteSpace(user.Password) || string.IsNullOrWhiteSpace(user.VerificationCode)) + { + return false; + } + return await _userService.UpdatePassword(user.Password, user.VerificationCode); + } + [HttpPost("/user/email/modify")] public async Task ModifyUserEmail([FromQuery] string email) {