Skip to content

Commit e30519c

Browse files
authored
Merge pull request #836 from Qtoss-AI/master
clean code of sql planner
2 parents 5f0f4f7 + 6f4448d commit e30519c

File tree

38 files changed

+381
-222
lines changed

38 files changed

+381
-222
lines changed

src/Infrastructure/BotSharp.Abstraction/Browsing/Models/HttpRequestParams.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
using System.Diagnostics;
12
using System.Net.Http;
23

34
namespace BotSharp.Abstraction.Browsing.Models;
45

6+
[DebuggerStepThrough]
57
public class HttpRequestParams
68
{
79
[JsonPropertyName("url")]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ bool UserAuthenticated(User user, Token token)
4949
/// </summary>
5050
/// <param name="user"></param>
5151
/// <returns></returns>
52-
Task VerificationCodeResetPassword(User user);
52+
Task SendVerificationCode(User user);
5353

5454
/// <summary>
5555
/// Delete users

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@ public interface IUserService
1818
Task<Token?> GetAdminToken(string authorization);
1919
Task<Token?> GetToken(string authorization);
2020
Task<Token> CreateTokenByUser(User user);
21+
Task<Token> RenewToken();
2122
Task<User> GetMyProfile();
2223
Task<bool> VerifyUserNameExisting(string userName);
2324
Task<bool> VerifyEmailExisting(string email);
2425
Task<bool> VerifyPhoneExisting(string phone, string regionCode);
25-
Task<bool> SendVerificationCodeResetPasswordNoLogin(User user);
26-
Task<bool> SendVerificationCodeResetPasswordLogin();
26+
Task<User> ResetVerificationCode(User user);
27+
Task<bool> SendVerificationCodeNoLogin(User user);
28+
Task<bool> SendVerificationCodeLogin();
29+
Task<bool> SetUserPassword(User user);
2730
Task<bool> ResetUserPassword(User user);
2831
Task<bool> ModifyUserEmail(string email);
2932
Task<bool> ModifyUserPhone(string phone, string regionCode);

src/Infrastructure/BotSharp.Abstraction/Users/Models/User.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class User
2020
public string Type { get; set; } = UserType.Client;
2121
public string Role { get; set; } = UserRole.User;
2222
public string? VerificationCode { get; set; }
23+
public DateTime? VerificationCodeExpireAt { get; set; }
2324
public bool Verified { get; set; }
2425
public string RegionCode { get; set; } = "CN";
2526
public string? AffiliateId { get; set; }

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

Lines changed: 84 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,7 @@ public async Task<User> CreateUser(User user)
5353
record = db.GetUserByUserName(user.UserName);
5454
}
5555

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))
56+
if (record == null && !string.IsNullOrWhiteSpace(user.Phone))
6457
{
6558
record = db.GetUserByPhone(user.Phone, regionCode: (string.IsNullOrWhiteSpace(user.RegionCode) ? "CN" : user.RegionCode));
6659
}
@@ -70,6 +63,13 @@ record = db.GetUserByPhone(user.Phone, regionCode: (string.IsNullOrWhiteSpace(us
7063
record = db.GetUserByEmail(user.Email);
7164
}
7265

66+
if (record != null && record.Verified)
67+
{
68+
// account is already activated
69+
_logger.LogWarning($"User account already exists: {record.Id} {record.UserName}");
70+
return record;
71+
}
72+
7373
if (record != null)
7474
{
7575
hasRegisterId = record.Id;
@@ -94,8 +94,13 @@ record = user;
9494
//record.Phone = "+" + Regex.Match(user.Phone, @"\d+").Value;
9595
record.Phone = Regex.Match(user.Phone, @"\d+").Value;
9696
}
97+
9798
record.Salt = Guid.NewGuid().ToString("N");
98-
record.Password = Utilities.HashTextMd5($"{user.Password}{record.Salt}");
99+
100+
if (!string.IsNullOrWhiteSpace(user.Password))
101+
{
102+
record.Password = Utilities.HashTextMd5($"{user.Password}{record.Salt}");
103+
}
99104

100105
if (_setting.NewUserVerification)
101106
{
@@ -482,7 +487,7 @@ record = db.GetUserByPhone(id, regionCode: (string.IsNullOrWhiteSpace(model.Regi
482487
return default;
483488
}
484489

485-
if (record.VerificationCode != model.VerificationCode)
490+
if (record.VerificationCode != model.VerificationCode || (record.VerificationCodeExpireAt != null && DateTime.UtcNow > record.VerificationCodeExpireAt))
486491
{
487492
return default;
488493
}
@@ -520,6 +525,16 @@ public async Task<Token> CreateTokenByUser(User user)
520525
return token;
521526
}
522527

528+
public async Task<Token> RenewToken()
529+
{
530+
var newToken = GenerateJwtToken(await GetMyProfile());
531+
var newJwt = new JwtSecurityTokenHandler().ReadJwtToken(newToken);
532+
Token token = new Token();
533+
token.AccessToken = newToken;
534+
token.ExpireTime = newJwt.Payload.Exp.Value;
535+
return token;
536+
}
537+
523538
public async Task<bool> VerifyUserNameExisting(string userName)
524539
{
525540
if (string.IsNullOrEmpty(userName))
@@ -572,15 +587,32 @@ public async Task<bool> VerifyPhoneExisting(string phone, string regionCode)
572587
return false;
573588
}
574589

575-
public async Task<bool> SendVerificationCodeResetPasswordNoLogin(User user)
590+
public async Task<bool> SendVerificationCodeNoLogin(User user)
576591
{
577-
var db = _services.GetRequiredService<IBotSharpRepository>();
592+
User? record = await ResetVerificationCode(user);
578593

579-
User? record = null;
594+
if (record == null)
595+
{
596+
return false;
597+
}
580598

599+
//send code to user Email.
600+
var hooks = _services.GetServices<IAuthenticationHook>();
601+
foreach (var hook in hooks)
602+
{
603+
await hook.SendVerificationCode(record);
604+
}
605+
606+
return true;
607+
}
608+
609+
public async Task<User> ResetVerificationCode(User user)
610+
{
611+
var db = _services.GetRequiredService<IBotSharpRepository>();
612+
User record = null;
581613
if (!string.IsNullOrEmpty(user.Email) && !string.IsNullOrEmpty(user.Phone))
582614
{
583-
return false;
615+
return null;
584616
}
585617

586618
if (!string.IsNullOrEmpty(user.Phone))
@@ -595,25 +627,18 @@ record = db.GetUserByEmail(user.Email);
595627

596628
if (record == null)
597629
{
598-
return false;
630+
return null;
599631
}
600632

601633
record.VerificationCode = Nanoid.Generate(alphabet: "0123456789", size: 6);
602634

603635
//update current verification code.
604636
db.UpdateUserVerificationCode(record.Id, record.VerificationCode);
605637

606-
//send code to user Email.
607-
var hooks = _services.GetServices<IAuthenticationHook>();
608-
foreach (var hook in hooks)
609-
{
610-
await hook.VerificationCodeResetPassword(record);
611-
}
612-
613-
return true;
638+
return record;
614639
}
615640

616-
public async Task<bool> SendVerificationCodeResetPasswordLogin()
641+
public async Task<bool> SendVerificationCodeLogin()
617642
{
618643
var db = _services.GetRequiredService<IBotSharpRepository>();
619644

@@ -638,7 +663,7 @@ record = db.GetUserById(_user.Id);
638663
var hooks = _services.GetServices<IAuthenticationHook>();
639664
foreach (var hook in hooks)
640665
{
641-
await hook.VerificationCodeResetPassword(record);
666+
await hook.SendVerificationCode(record);
642667
}
643668

644669
return true;
@@ -669,7 +694,40 @@ record = db.GetUserByPhone(user.Phone, regionCode: (string.IsNullOrWhiteSpace(us
669694
return false;
670695
}
671696

672-
if (user.VerificationCode != record.VerificationCode)
697+
if (user.VerificationCode != record.VerificationCode || (record.VerificationCodeExpireAt != null && DateTime.UtcNow > record.VerificationCodeExpireAt))
698+
{
699+
return false;
700+
}
701+
702+
var newPassword = Utilities.HashTextMd5($"{user.Password}{record.Salt}");
703+
db.UpdateUserPassword(record.Id, newPassword);
704+
return true;
705+
}
706+
707+
public async Task<bool> SetUserPassword(User user)
708+
{
709+
if (!string.IsNullOrEmpty(user.Id) && !string.IsNullOrEmpty(user.Email) && !string.IsNullOrEmpty(user.Phone))
710+
{
711+
return false;
712+
}
713+
var db = _services.GetRequiredService<IBotSharpRepository>();
714+
715+
User? record = null;
716+
717+
if (!string.IsNullOrEmpty(user.Id))
718+
{
719+
record = db.GetUserById(user.Id);
720+
}
721+
else if (!string.IsNullOrEmpty(user.Phone))
722+
{
723+
record = db.GetUserByPhone(user.Phone, regionCode: (string.IsNullOrWhiteSpace(user.RegionCode) ? "CN" : user.RegionCode));
724+
}
725+
else if (!string.IsNullOrEmpty(user.Email))
726+
{
727+
record = db.GetUserByEmail(user.Email);
728+
}
729+
730+
if (record == null)
673731
{
674732
return false;
675733
}

src/Infrastructure/BotSharp.OpenAPI/BotSharpOpenApiExtensions.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
using Microsoft.OpenApi.Models;
1212
using Microsoft.IdentityModel.JsonWebTokens;
1313
using BotSharp.OpenAPI.BackgroundServices;
14-
using BotSharp.OpenAPI.Filters;
1514

1615
namespace BotSharp.OpenAPI;
1716

@@ -33,15 +32,6 @@ public static IServiceCollection AddBotSharpOpenAPI(this IServiceCollection serv
3332
services.AddScoped<IUserIdentity, UserIdentity>();
3433
services.AddHostedService<ConversationTimeoutService>();
3534

36-
var enableSingleLogin = bool.Parse(config["Jwt:EnableSingleLogin"] ?? "false");
37-
if (enableSingleLogin)
38-
{
39-
services.AddMvc(options =>
40-
{
41-
options.Filters.Add<UserSingleLoginFilter>();
42-
});
43-
}
44-
4535
// Add bearer authentication
4636
var schema = "MIXED_SCHEME";
4737
var builder = services.AddAuthentication(options =>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,13 @@ public async Task<bool> VerifyPhoneExisting([FromQuery] string phone, [FromQuery
132132
[HttpPost("/user/verifycode-out")]
133133
public async Task<bool> SendVerificationCodeResetPassword([FromBody] UserCreationModel user)
134134
{
135-
return await _userService.SendVerificationCodeResetPasswordNoLogin(user.ToUser());
135+
return await _userService.SendVerificationCodeNoLogin(user.ToUser());
136136
}
137137

138138
[HttpPost("/user/verifycode-in")]
139139
public async Task<bool> SendVerificationCodeResetPasswordLogined()
140140
{
141-
return await _userService.SendVerificationCodeResetPasswordLogin();
141+
return await _userService.SendVerificationCodeLogin();
142142
}
143143

144144
[AllowAnonymous]

src/Infrastructure/BotSharp.OpenAPI/Filters/UserSingleLoginFilter.cs

Lines changed: 0 additions & 78 deletions
This file was deleted.

src/Plugins/BotSharp.Plugin.MongoStorage/Collections/UserDocument.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class UserDocument : MongoBase
1717
public string Type { get; set; } = UserType.Client;
1818
public string Role { get; set; } = null!;
1919
public string? VerificationCode { get; set; }
20+
public DateTime? VerificationCodeExpireAt { get; set; }
2021
public bool Verified { get; set; }
2122
public string? RegionCode { get; set; }
2223
public string? AffiliateId { get; set; }
@@ -48,6 +49,7 @@ public User ToUser()
4849
EmployeeId = EmployeeId,
4950
IsDisabled = IsDisabled,
5051
VerificationCode = VerificationCode,
52+
VerificationCodeExpireAt = VerificationCodeExpireAt,
5153
Verified = Verified,
5254
RegionCode = RegionCode,
5355
Permissions = Permissions,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ public void UpdateUserVerificationCode(string userId, string verficationCode)
132132
{
133133
var filter = Builders<UserDocument>.Filter.Eq(x => x.Id, userId);
134134
var update = Builders<UserDocument>.Update.Set(x => x.VerificationCode, verficationCode)
135+
.Set(x => x.VerificationCodeExpireAt, DateTime.UtcNow.AddMinutes(5))
135136
.Set(x => x.UpdatedTime, DateTime.UtcNow);
136137
_dc.Users.UpdateOne(filter, update);
137138
}

0 commit comments

Comments
 (0)