From c72ea4af2f323c60ac487e77abcfb3a21b8a178d Mon Sep 17 00:00:00 2001 From: YouWeiDH Date: Sat, 12 Oct 2024 15:13:46 +0800 Subject: [PATCH 1/2] hdong: Fix to avoid Email/Phone duplicate when user register again.(check first:phone, then email, then userName) --- .../Users/IUserService.cs | 1 + .../Users/Services/UserService.cs | 37 +++++++++++++++++-- .../Controllers/UserController.cs | 7 ++++ 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Users/IUserService.cs b/src/Infrastructure/BotSharp.Abstraction/Users/IUserService.cs index 5a3315b7..421e217b 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Users/IUserService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Users/IUserService.cs @@ -13,6 +13,7 @@ public interface IUserService Task GetMyProfile(); Task VerifyUserNameExisting(string userName); Task VerifyEmailExisting(string email); + Task VerifyPhoneExisting(string phone); Task SendVerificationCodeResetPasswordNoLogin(User user); Task SendVerificationCodeResetPasswordLogin(); Task ResetUserPassword(User user); diff --git a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs index 61fdd758..5cd329d5 100644 --- a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs +++ b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs @@ -45,7 +45,19 @@ public class UserService : IUserService } var db = _services.GetRequiredService(); - var record = db.GetUserByUserName(user.UserName); + + + var record = db.GetUserByPhone(user.Phone); + + if (record == null) + { + record = db.GetUserByEmail(user.Email); + } + + if (record == null) + { + record = db.GetUserByUserName(user.UserName); + } if (record != null) { @@ -428,6 +440,23 @@ public class UserService : IUserService return false; } + public async Task VerifyPhoneExisting(string phone) + { + if (string.IsNullOrEmpty(phone)) + { + return true; + } + + var db = _services.GetRequiredService(); + var UserByphone = db.GetUserByPhone(phone); + if (UserByphone != null && UserByphone.Verified) + { + return true; + } + + return false; + } + public async Task SendVerificationCodeResetPasswordNoLogin(User user) { var db = _services.GetRequiredService(); @@ -540,7 +569,8 @@ public class UserService : IUserService var curUser = await GetMyProfile(); var db = _services.GetRequiredService(); var record = db.GetUserById(curUser.Id); - if (record == null) + var existEmail = db.GetUserByEmail(email); + if (record == null || existEmail != null) { return false; } @@ -554,8 +584,9 @@ public class UserService : IUserService var curUser = await GetMyProfile(); var db = _services.GetRequiredService(); var record = db.GetUserById(curUser.Id); + var existPhone = db.GetUserByPhone(phone); - if (record == null) + if (record == null || existPhone != null) { return false; } diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs index bf15a33b..67c0f4e3 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs @@ -109,6 +109,13 @@ public class UserController : ControllerBase return await _userService.VerifyEmailExisting(email); } + [AllowAnonymous] + [HttpGet("/user/phone/existing")] + public async Task VerifyPhoneExisting([FromQuery] string phone) + { + return await _userService.VerifyPhoneExisting(phone); + } + [AllowAnonymous] [HttpPost("/user/verifycode-out")] public async Task SendVerificationCodeResetPassword([FromBody] UserCreationModel user) From 779e51301c56f791b66c04e415d48165bafac0de Mon Sep 17 00:00:00 2001 From: YouWeiDH Date: Sat, 12 Oct 2024 15:56:00 +0800 Subject: [PATCH 2/2] hdong: avoid register phone by Affiliate effect qt. --- .../Repository/MongoRepository.User.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.User.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.User.cs index db40705d..55c454bd 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.User.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.User.cs @@ -13,7 +13,7 @@ public partial class MongoRepository public User? GetUserByPhone(string phone) { - var user = _dc.Users.AsQueryable().FirstOrDefault(x => x.Phone == phone); + var user = _dc.Users.AsQueryable().FirstOrDefault(x => x.Phone == phone && x.Type != UserType.Affiliate); return user != null ? user.ToUser() : null; }