From f9466ab3986db2088e6e576bca6d42ddc1af2a30 Mon Sep 17 00:00:00 2001 From: YouWeiDH Date: Thu, 14 Nov 2024 20:42:25 +0800 Subject: [PATCH] hdong: fix admin account conflict with common account. --- .../Repositories/IBotSharpRepository.cs | 1 + .../BotSharp.Core/Users/Services/UserService.cs | 2 +- .../Repository/MongoRepository.User.cs | 17 ++++++++++++++++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs index 21b6bb8d..9282506d 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs @@ -19,6 +19,7 @@ public interface IBotSharpRepository : IHaveServiceProvider #region User User? GetUserByEmail(string email) => throw new NotImplementedException(); User? GetUserByPhone(string phone, string regionCode = "CN") => throw new NotImplementedException(); + User? GetAdminUserByPhone(string phone, string regionCode = "CN") => throw new NotImplementedException(); User? GetAffiliateUserByPhone(string phone) => throw new NotImplementedException(); User? GetUserById(string id) => throw new NotImplementedException(); List GetUserByIds(List ids) => throw new NotImplementedException(); diff --git a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs index fa757e2f..ba97bae8 100644 --- a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs +++ b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs @@ -172,7 +172,7 @@ public class UserService : IUserService var base64 = Encoding.UTF8.GetString(Convert.FromBase64String(authorization)); var (id, password) = base64.SplitAsTuple(":"); var db = _services.GetRequiredService(); - var record = db.GetUserByPhone(id); + var record = db.GetAdminUserByPhone(id); var isCanLogin = record != null && !record.IsDisabled && record.Type == UserType.Internal && new List { diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.User.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.User.cs index 4af170b0..bfaecbf5 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.User.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.User.cs @@ -22,7 +22,22 @@ public partial class MongoRepository string phoneSecond = phone.StartsWith("+86") ? phone.Replace("+86", "") : $"+86{phone}"; - var user = _dc.Users.AsQueryable().FirstOrDefault(x => (x.Phone == phone || x.Phone == phoneSecond) && x.Type != UserType.Affiliate && (x.RegionCode == regionCode || string.IsNullOrWhiteSpace(x.RegionCode))); + var user = _dc.Users.AsQueryable().FirstOrDefault(x => (x.Phone == phone || x.Phone == phoneSecond) && x.Type != UserType.Affiliate + && (x.RegionCode == regionCode || string.IsNullOrWhiteSpace(x.RegionCode))); + return user != null ? user.ToUser() : null; + } + + public User? GetAdminUserByPhone(string phone, string regionCode = "CN") + { + if (string.IsNullOrWhiteSpace(phone)) + { + return null; + } + + string phoneSecond = phone.StartsWith("+86") ? phone.Replace("+86", "") : $"+86{phone}"; + + var user = _dc.Users.AsQueryable().FirstOrDefault(x => (x.Phone == phone || x.Phone == phoneSecond) && x.Type != UserType.Affiliate + && (x.RegionCode == regionCode || string.IsNullOrWhiteSpace(x.RegionCode)) && (x.Role == "admin" || x.Role == "root")); return user != null ? user.ToUser() : null; }