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 1/3] 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) { From 0d473168fd4f79fbff9650ae07d9db2a616fa080 Mon Sep 17 00:00:00 2001 From: AnonymousDotNet <18776095145@163.com> Date: Wed, 18 Sep 2024 15:38:44 +0800 Subject: [PATCH 2/3] Modify update password API --- .../BotSharp.Core/Users/Services/UserService.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs index 28b1a8ea..b0830d0c 100644 --- a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs +++ b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs @@ -98,10 +98,9 @@ public class UserService : IUserService return false; } - var salt = Guid.NewGuid().ToString("N"); - record.Password = Utilities.HashTextMd5($"{password}{salt}"); + var newPassword = Utilities.HashTextMd5($"{password}{record.Salt}"); - db.UpdateUserPassword(record.Id, password); + db.UpdateUserPassword(record.Id, newPassword); return true; } From 81dfb1f84a337cc3ba028bd4c1419803cb36b760 Mon Sep 17 00:00:00 2001 From: AnonymousDotNet <18776095145@163.com> Date: Wed, 18 Sep 2024 17:55:20 +0800 Subject: [PATCH 3/3] Handle verification code sent by logged in users --- .../Users/Services/UserService.cs | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs index b0830d0c..65efbfd1 100644 --- a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs +++ b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs @@ -344,24 +344,32 @@ public class UserService : IUserService public async Task SendVerificationCodeResetPassword(User user) { - if (!string.IsNullOrEmpty(user.Email) && !string.IsNullOrEmpty(user.Phone)) - { - return false; - } - var db = _services.GetRequiredService(); User? record = null; - if (!string.IsNullOrEmpty(user.Email)) + if (!string.IsNullOrWhiteSpace(_user.Id)) { - record = db.GetUserByEmail(user.Email); + record = db.GetUserById(_user.Id); + } + else + { + if (!string.IsNullOrEmpty(user.Email) && !string.IsNullOrEmpty(user.Phone)) + { + return false; + } + + if (!string.IsNullOrEmpty(user.Email)) + { + record = db.GetUserByEmail(user.Email); + } + + if (!string.IsNullOrEmpty(user.Phone)) + { + record = db.GetUserByPhone(user.Phone); + } } - if (!string.IsNullOrEmpty(user.Phone)) - { - record = db.GetUserByPhone(user.Phone); - } if (record == null) { return false;