Skip to content

Commit 331b931

Browse files
authored
Merge pull request #685 from Qtoss-AI/master
Miscellaneous improvements
2 parents fc2c26d + 54b1a8a commit 331b931

File tree

9 files changed

+115
-20
lines changed

9 files changed

+115
-20
lines changed

src/Infrastructure/BotSharp.Abstraction/Users/IAuthenticationHook.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public interface IAuthenticationHook
88
Task<User> Authenticate(string id, string password);
99
void AddClaims(List<Claim> claims);
1010
void BeforeSending(Token token);
11+
Task UserUpdating(User user);
1112
Task UserCreated(User user);
1213
Task VerificationCodeResetPassword(User user);
1314
Task DelUsers(List<string> userIds);

src/Infrastructure/BotSharp.Abstraction/Users/IUserIdentity.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ public interface IUserIdentity
88
string FirstName { get; }
99
string LastName { get; }
1010
string FullName { get; }
11-
string? UserLanguage { get; }
11+
/// <summary>
12+
/// "en-US", "zh-CN"
13+
/// </summary>
14+
string UserLanguage { get; }
1215
string? Phone { get; }
1316
string? AffiliateId { get; }
1417
}

src/Infrastructure/BotSharp.Abstraction/Users/IUserService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public interface IUserService
1313
Task<User> GetMyProfile();
1414
Task<bool> VerifyUserNameExisting(string userName);
1515
Task<bool> VerifyEmailExisting(string email);
16+
Task<bool> VerifyPhoneExisting(string phone);
1617
Task<bool> SendVerificationCodeResetPasswordNoLogin(User user);
1718
Task<bool> SendVerificationCodeResetPasswordLogin();
1819
Task<bool> ResetUserPassword(User user);

src/Infrastructure/BotSharp.Core/Infrastructures/HookEmitter.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace BotSharp.Core.Infrastructures;
44

55
public static class HookEmitter
66
{
7-
public static async Task<HookEmittedResult> Emit<T>(IServiceProvider services, Action<T> action)
7+
public static HookEmittedResult Emit<T>(IServiceProvider services, Action<T> action)
88
{
99
var logger = services.GetRequiredService<ILogger<T>>();
1010
var result = new HookEmittedResult();
@@ -25,4 +25,26 @@ public static async Task<HookEmittedResult> Emit<T>(IServiceProvider services, A
2525

2626
return result;
2727
}
28+
29+
public static async Task<HookEmittedResult> Emit<T>(IServiceProvider services, Func<T, Task> action)
30+
{
31+
var logger = services.GetRequiredService<ILogger<T>>();
32+
var result = new HookEmittedResult();
33+
var hooks = services.GetServices<T>();
34+
35+
foreach (var hook in hooks)
36+
{
37+
try
38+
{
39+
logger.LogInformation($"Emit hook action on {action.Method.Name}({hook.GetType().Name})");
40+
await action(hook);
41+
}
42+
catch (Exception ex)
43+
{
44+
logger.LogError(ex.ToString());
45+
}
46+
}
47+
48+
return result;
49+
}
2850
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ public string FullName
5858
}
5959

6060
[JsonPropertyName("user_language")]
61-
public string? UserLanguage
61+
public string UserLanguage
6262
{
6363
get
6464
{
6565
_contextAccessor.HttpContext.Request.Headers.TryGetValue("User-Language", out var languages);
66-
return languages.FirstOrDefault();
66+
return languages.FirstOrDefault() ?? "en-US";
6767
}
6868
}
6969

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

Lines changed: 73 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public UserService(IServiceProvider services,
3333
public async Task<User> CreateUser(User user)
3434
{
3535
string hasRegisterId = null;
36-
if (string.IsNullOrEmpty(user.UserName))
36+
if (string.IsNullOrWhiteSpace(user.UserName))
3737
{
3838
// generate unique name
3939
var name = Nanoid.Generate("0123456789botsharp", 10);
@@ -45,16 +45,46 @@ public async Task<User> CreateUser(User user)
4545
}
4646

4747
var db = _services.GetRequiredService<IBotSharpRepository>();
48-
var record = db.GetUserByUserName(user.UserName);
48+
49+
User? record = null;
50+
51+
if (!string.IsNullOrWhiteSpace(user.UserName))
52+
{
53+
record = db.GetUserByUserName(user.UserName);
54+
}
55+
56+
if (record != null && record.Verified)
57+
{
58+
// account is already activated
59+
_logger.LogWarning($"User account already exists: {record.Id} {record.UserName}");
60+
return record;
61+
}
62+
63+
if (!string.IsNullOrWhiteSpace(user.Phone))
64+
{
65+
record = db.GetUserByPhone(user.Phone);
66+
}
67+
68+
if (record == null && !string.IsNullOrWhiteSpace(user.Email))
69+
{
70+
record = db.GetUserByEmail(user.Email);
71+
}
4972

5073
if (record != null)
5174
{
5275
hasRegisterId = record.Id;
5376
}
5477

55-
if (string.IsNullOrEmpty(user.Id))
78+
if (string.IsNullOrWhiteSpace(user.Id))
5679
{
57-
user.Id = Guid.NewGuid().ToString();
80+
if (!string.IsNullOrWhiteSpace(hasRegisterId))
81+
{
82+
user.Id = hasRegisterId;
83+
}
84+
else
85+
{
86+
user.Id = Guid.NewGuid().ToString();
87+
}
5888
}
5989

6090
record = user;
@@ -68,7 +98,7 @@ record = user;
6898

6999
if (_setting.NewUserVerification)
70100
{
71-
record.VerificationCode = Nanoid.Generate(alphabet: "0123456789", size: 6);
101+
// record.VerificationCode = Nanoid.Generate(alphabet: "0123456789", size: 6);
72102
record.Verified = false;
73103
}
74104

@@ -421,6 +451,23 @@ public async Task<bool> VerifyEmailExisting(string email)
421451
return false;
422452
}
423453

454+
public async Task<bool> VerifyPhoneExisting(string phone)
455+
{
456+
if (string.IsNullOrEmpty(phone))
457+
{
458+
return true;
459+
}
460+
461+
var db = _services.GetRequiredService<IBotSharpRepository>();
462+
var UserByphone = db.GetUserByPhone(phone);
463+
if (UserByphone != null && UserByphone.Verified)
464+
{
465+
return true;
466+
}
467+
468+
return false;
469+
}
470+
424471
public async Task<bool> SendVerificationCodeResetPasswordNoLogin(User user)
425472
{
426473
var db = _services.GetRequiredService<IBotSharpRepository>();
@@ -456,7 +503,7 @@ record = db.GetUserByEmail(user.Email);
456503
var hooks = _services.GetServices<IAuthenticationHook>();
457504
foreach (var hook in hooks)
458505
{
459-
hook.VerificationCodeResetPassword(record);
506+
await hook.VerificationCodeResetPassword(record);
460507
}
461508

462509
return true;
@@ -487,7 +534,7 @@ record = db.GetUserById(_user.Id);
487534
var hooks = _services.GetServices<IAuthenticationHook>();
488535
foreach (var hook in hooks)
489536
{
490-
hook.VerificationCodeResetPassword(record);
537+
await hook.VerificationCodeResetPassword(record);
491538
}
492539

493540
return true;
@@ -533,12 +580,20 @@ public async Task<bool> ModifyUserEmail(string email)
533580
var curUser = await GetMyProfile();
534581
var db = _services.GetRequiredService<IBotSharpRepository>();
535582
var record = db.GetUserById(curUser.Id);
536-
if (record == null)
583+
var existEmail = db.GetUserByEmail(email);
584+
if (record == null || existEmail != null)
537585
{
538586
return false;
539587
}
540588

541-
db.UpdateUserEmail(record.Id, email);
589+
record.Email = email;
590+
var hooks = _services.GetServices<IAuthenticationHook>();
591+
foreach (var hook in hooks)
592+
{
593+
await hook.UserUpdating(record);
594+
}
595+
596+
db.UpdateUserEmail(record.Id, record.Email);
542597
return true;
543598
}
544599

@@ -547,18 +602,23 @@ public async Task<bool> ModifyUserPhone(string phone)
547602
var curUser = await GetMyProfile();
548603
var db = _services.GetRequiredService<IBotSharpRepository>();
549604
var record = db.GetUserById(curUser.Id);
605+
var existPhone = db.GetUserByPhone(phone);
550606

551-
if (record == null)
607+
if (record == null || existPhone != null)
552608
{
553609
return false;
554610
}
555611

556-
if ((record.UserName.Substring(0, 3) == "+86" || record.FirstName.Substring(0, 3) == "+86") && phone.Substring(0, 3) != "+86")
612+
record.Phone = phone;
613+
614+
var hooks = _services.GetServices<IAuthenticationHook>();
615+
foreach (var hook in hooks)
557616
{
558-
phone = $"+86{phone}";
617+
await hook.UserUpdating(record);
559618
}
560619

561-
db.UpdateUserPhone(record.Id, phone);
620+
db.UpdateUserPhone(record.Id, record.Phone);
621+
562622
return true;
563623
}
564624

src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ public async Task<bool> VerifyEmailExisting([FromQuery] string email)
109109
return await _userService.VerifyEmailExisting(email);
110110
}
111111

112+
[AllowAnonymous]
113+
[HttpGet("/user/phone/existing")]
114+
public async Task<bool> VerifyPhoneExisting([FromQuery] string phone)
115+
{
116+
return await _userService.VerifyPhoneExisting(phone);
117+
}
118+
112119
[AllowAnonymous]
113120
[HttpPost("/user/verifycode-out")]
114121
public async Task<bool> SendVerificationCodeResetPassword([FromBody] UserCreationModel user)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public partial class MongoRepository
1313

1414
public User? GetUserByPhone(string phone)
1515
{
16-
var user = _dc.Users.AsQueryable().FirstOrDefault(x => x.Phone == phone);
16+
var user = _dc.Users.AsQueryable().FirstOrDefault(x => x.Phone == phone && x.Type != UserType.Affiliate);
1717
return user != null ? user.ToUser() : null;
1818
}
1919

src/Plugins/BotSharp.Plugin.Planner/Functions/SummaryPlanFn.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ public async Task<bool> Execute(RoleDialogModel message)
6767
var summary = await GetAiResponse(plannerAgent);
6868
message.Content = summary.Content;
6969

70-
await HookEmitter.Emit<IPlanningHook>(_services, x =>
71-
x.OnPlanningCompleted(nameof(TwoStageTaskPlanner), message));
70+
await HookEmitter.Emit<IPlanningHook>(_services, async hook =>
71+
await hook.OnPlanningCompleted(nameof(TwoStageTaskPlanner), message)
72+
);
7273

7374
return true;
7475
}

0 commit comments

Comments
 (0)