Skip to content

Commit 7af0261

Browse files
authored
Merge pull request #70 from Qtoss-AI/hdongDev
Fix userName can't login in email type.
2 parents 3459c96 + 6dc1b53 commit 7af0261

File tree

4 files changed

+94
-39
lines changed

4 files changed

+94
-39
lines changed

src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public interface IBotSharpRepository : IHaveServiceProvider
2828
#region User
2929
User? GetUserByEmail(string email) => throw new NotImplementedException();
3030
User? GetUserByPhone(string phone, string type = UserType.Client, string regionCode = "CN") => throw new NotImplementedException();
31+
User? GetUserByPhoneV2(string phone, string source = UserType.Internal, string regionCode = "CN") => throw new NotImplementedException();
3132
User? GetAffiliateUserByPhone(string phone) => throw new NotImplementedException();
3233
User? GetUserById(string id) => throw new NotImplementedException();
3334
List<User> GetUserByIds(List<string> ids) => throw new NotImplementedException();

src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.User.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,23 @@ public partial class FileRepository
2828
return query.FirstOrDefault();
2929
}
3030

31+
public User? GetUserByPhoneV2(string phone, string? source = UserType.Internal, string regionCode = "CN")
32+
{
33+
var query = Users.Where(x => x.Phone == phone);
34+
35+
if (!string.IsNullOrEmpty(source))
36+
{
37+
query = query.Where(x => x.Type == source);
38+
}
39+
40+
if (!string.IsNullOrEmpty(regionCode))
41+
{
42+
query = query.Where(x => x.RegionCode == regionCode);
43+
}
44+
45+
return query.FirstOrDefault();
46+
}
47+
3148
public User? GetAffiliateUserByPhone(string phone)
3249
{
3350
return Users.FirstOrDefault(x => x.Phone == phone && x.Type == UserType.Affiliate);
@@ -172,7 +189,9 @@ public List<User> SearchLoginUsers(User filter, string source = UserSource.Inter
172189
searchResult.Add(user);
173190
}
174191
}
175-
else if (!string.IsNullOrWhiteSpace(filter.UserName))
192+
193+
194+
if (searchResult.Count == 0 && !string.IsNullOrWhiteSpace(filter.UserName))
176195
{
177196
var curUser = Users.AsQueryable().FirstOrDefault(x => x.Source == source && x.UserName == filter.UserName);
178197
User user = curUser != null ? curUser : null;

src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ record = db.GetUserByUserName(user.UserName);
5555

5656
if (record == null && !string.IsNullOrWhiteSpace(user.Phone))
5757
{
58+
//if (user.Type != "internal")
59+
//{
60+
// record = db.GetUserByPhoneV2(user.Phone, regionCode: (string.IsNullOrWhiteSpace(user.RegionCode) ? "CN" : user.RegionCode));
61+
//}
62+
5863
record = db.GetUserByPhone(user.Phone, regionCode: (string.IsNullOrWhiteSpace(user.RegionCode) ? "CN" : user.RegionCode));
5964
}
6065

@@ -477,11 +482,17 @@ public async Task<Token> ActiveUser(UserActivationModel model)
477482
var id = model.UserName;
478483
var db = _services.GetRequiredService<IBotSharpRepository>();
479484
var record = id.Contains("@") ? db.GetUserByEmail(id) : db.GetUserByUserName(id);
485+
480486
if (record == null)
481487
{
482488
record = db.GetUserByPhone(id, regionCode: (string.IsNullOrWhiteSpace(model.RegionCode) ? "CN" : model.RegionCode));
483489
}
484490

491+
//if (record == null)
492+
//{
493+
// record = db.GetUserByPhoneV2(id, regionCode: (string.IsNullOrWhiteSpace(model.RegionCode) ? "CN" : model.RegionCode));
494+
//}
495+
485496
if (record == null)
486497
{
487498
return default;
@@ -621,21 +632,12 @@ public async Task<bool> SendVerificationCodeNoLogin(User user)
621632
public async Task<User> ResetVerificationCode(User user)
622633
{
623634
var db = _services.GetRequiredService<IBotSharpRepository>();
624-
User record = null;
625-
if (!string.IsNullOrEmpty(user.Email) && !string.IsNullOrEmpty(user.Phone))
635+
if (!string.IsNullOrWhiteSpace(user.Email) && !string.IsNullOrWhiteSpace(user.Phone))
626636
{
627637
return null;
628638
}
629639

630-
if (!string.IsNullOrEmpty(user.Phone))
631-
{
632-
record = db.GetUserByPhone(user.Phone, regionCode: user.RegionCode);
633-
}
634-
635-
if (!string.IsNullOrEmpty(user.Email))
636-
{
637-
record = db.GetUserByEmail(user.Email);
638-
}
640+
User? record = GetLoginUserByUniqueFilter(user, db);
639641

640642
if (record == null)
641643
{
@@ -650,6 +652,36 @@ record = db.GetUserByEmail(user.Email);
650652
return record;
651653
}
652654

655+
private static User? GetLoginUserByUniqueFilter(User user, IBotSharpRepository db)
656+
{
657+
User? record = null;
658+
if (!string.IsNullOrWhiteSpace(user.Id))
659+
{
660+
record = db.GetUserById(user.Id);
661+
}
662+
663+
if (record == null && !string.IsNullOrWhiteSpace(user.Phone))
664+
{
665+
record = db.GetUserByPhone(user.Phone, regionCode: string.IsNullOrWhiteSpace(user.RegionCode) ? "CN" : user.RegionCode);
666+
//if (record == null)
667+
//{
668+
// record = db.GetUserByPhoneV2(user.Phone, regionCode: string.IsNullOrWhiteSpace(user.RegionCode) ? "CN" : user.RegionCode);
669+
//}
670+
}
671+
672+
if (record == null && !string.IsNullOrWhiteSpace(user.Email))
673+
{
674+
record = db.GetUserByEmail(user.Email);
675+
}
676+
677+
if (record == null && !string.IsNullOrWhiteSpace(user.UserName))
678+
{
679+
record = db.GetUserByUserName(user.UserName);
680+
}
681+
682+
return record;
683+
}
684+
653685
public async Task<bool> SendVerificationCodeLogin()
654686
{
655687
var db = _services.GetRequiredService<IBotSharpRepository>();
@@ -689,17 +721,7 @@ public async Task<bool> ResetUserPassword(User user)
689721
}
690722
var db = _services.GetRequiredService<IBotSharpRepository>();
691723

692-
User? record = null;
693-
694-
if (!string.IsNullOrEmpty(user.Email))
695-
{
696-
record = db.GetUserByEmail(user.Email);
697-
}
698-
699-
if (!string.IsNullOrEmpty(user.Phone))
700-
{
701-
record = db.GetUserByPhone(user.Phone, regionCode: (string.IsNullOrWhiteSpace(user.RegionCode) ? "CN" : user.RegionCode));
702-
}
724+
User? record = GetLoginUserByUniqueFilter(user, db);
703725

704726
if (record == null)
705727
{
@@ -724,20 +746,7 @@ public async Task<bool> SetUserPassword(User user)
724746
}
725747
var db = _services.GetRequiredService<IBotSharpRepository>();
726748

727-
User? record = null;
728-
729-
if (!string.IsNullOrEmpty(user.Id))
730-
{
731-
record = db.GetUserById(user.Id);
732-
}
733-
else if (!string.IsNullOrEmpty(user.Phone))
734-
{
735-
record = db.GetUserByPhone(user.Phone, regionCode: (string.IsNullOrWhiteSpace(user.RegionCode) ? "CN" : user.RegionCode));
736-
}
737-
else if (!string.IsNullOrEmpty(user.Email))
738-
{
739-
record = db.GetUserByEmail(user.Email);
740-
}
749+
User? record = GetLoginUserByUniqueFilter(user, db);
741750

742751
if (record == null)
743752
{

src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.User.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,30 @@ public partial class MongoRepository
3737
return user != null ? user.ToUser() : null;
3838
}
3939

40+
public User? GetUserByPhoneV2(string phone, string source = UserType.Internal, string regionCode = "CN")
41+
{
42+
string phoneSecond = string.Empty;
43+
// if phone number length is less than 4, return null
44+
if (string.IsNullOrWhiteSpace(phone) || phone?.Length < 4)
45+
{
46+
return null;
47+
}
48+
49+
if (regionCode == "CN")
50+
{
51+
phoneSecond = (phone ?? "").StartsWith("+86") ? (phone ?? "").Replace("+86", "") : ($"+86{phone ?? ""}");
52+
}
53+
else
54+
{
55+
phoneSecond = (phone ?? "").Substring(regionCode == "US" ? 2 : 3);
56+
}
57+
58+
var user = _dc.Users.AsQueryable().FirstOrDefault(x => (x.Phone == phone || x.Phone == phoneSecond)
59+
&& (x.RegionCode == regionCode || string.IsNullOrWhiteSpace(x.RegionCode))
60+
&& (x.Source == source));
61+
return user != null ? user.ToUser() : null;
62+
}
63+
4064
public User? GetAffiliateUserByPhone(string phone)
4165
{
4266
var user = _dc.Users.AsQueryable().FirstOrDefault(x => x.Phone == phone && x.Type == UserType.Affiliate);
@@ -282,14 +306,16 @@ public List<User> SearchLoginUsers(User filter, string source = UserSource.Inter
282306
}
283307
else if (!string.IsNullOrWhiteSpace(filter.Email))
284308
{
285-
var curUser = _dc.Users.AsQueryable().FirstOrDefault(x => x.Source == source && x.Email == filter.Email.ToString());
309+
var curUser = _dc.Users.AsQueryable().FirstOrDefault(x => x.Source == source && x.Email == filter.Email.ToLower());
286310
User user = curUser != null ? curUser.ToUser() : null;
287311
if (user != null)
288312
{
289313
searchResult.Add(user);
290314
}
291315
}
292-
else if (!string.IsNullOrWhiteSpace(filter.UserName))
316+
317+
318+
if (searchResult.Count == 0 && !string.IsNullOrWhiteSpace(filter.UserName))
293319
{
294320
var curUser = _dc.Users.AsQueryable().FirstOrDefault(x => x.Source == source && x.UserName == filter.UserName);
295321
User user = curUser != null ? curUser.ToUser() : null;

0 commit comments

Comments
 (0)