Skip to content

Commit fe2d6f3

Browse files
authored
Merge pull request #13 from Qtoss-AI/jason_dev
Jason dev
2 parents ecdcb2a + 86289b0 commit fe2d6f3

File tree

8 files changed

+113
-7
lines changed

8 files changed

+113
-7
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,18 @@ public interface IBotSharpRepository
2121
#region User
2222
User? GetUserByEmail(string email) => throw new NotImplementedException();
2323
User? GetUserByPhone(string phone) => throw new NotImplementedException();
24-
User? GetUserById(string id) => throw new NotImplementedException();
24+
User? GetAffiliateUserByPhone(string phone) => throw new NotImplementedException();
25+
User? GetUserById(string id) => throw new NotImplementedException();
26+
List<User> GetUserByIds(List<string> ids) => throw new NotImplementedException();
27+
User? GetUserByAffiliateId(string affiliateId) => throw new NotImplementedException();
2528
User? GetUserByUserName(string userName) => throw new NotImplementedException();
2629
void CreateUser(User user) => throw new NotImplementedException();
2730
void UpdateUserVerified(string userId) => throw new NotImplementedException();
2831
void UpdateUserVerificationCode(string userId, string verficationCode) => throw new NotImplementedException();
2932
void UpdateUserPassword(string userId, string password) => throw new NotImplementedException();
3033
void UpdateUserEmail(string userId, string email)=> throw new NotImplementedException();
3134
void UpdateUserPhone(string userId, string Iphone) => throw new NotImplementedException();
35+
void UpdateUserIsDisable(string userId, bool isDisable) => throw new NotImplementedException();
3236
#endregion
3337

3438
#region Agent
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace BotSharp.Abstraction.Users.Enums
8+
{
9+
public static class UserSource
10+
{
11+
public const string Internal = "internal";
12+
}
13+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public interface IUserService
88
Task<User> GetUser(string id);
99
Task<User> CreateUser(User user);
1010
Task<Token> ActiveUser(UserActivationModel model);
11+
Task<Token?> GetAffiliateToken(string authorization);
1112
Task<Token?> GetToken(string authorization);
1213
Task<User> GetMyProfile();
1314
Task<bool> VerifyUserNameExisting(string userName);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class User
1212
public string? Phone { get; set; }
1313
public string Salt { get; set; } = string.Empty;
1414
public string Password { get; set; } = string.Empty;
15-
public string Source { get; set; } = "internal";
15+
public string Source { get; set; } = UserSource.Internal;
1616
public string? ExternalId { get; set; }
1717
/// <summary>
1818
/// internal, client, affiliate
@@ -21,6 +21,8 @@ public class User
2121
public string Role { get; set; } = UserRole.User;
2222
public string? VerificationCode { get; set; }
2323
public bool Verified { get; set; }
24+
public string? AffiliateId { get; set; }
25+
public bool IsDisabled { get; set; }
2426
public DateTime UpdatedTime { get; set; } = DateTime.UtcNow;
2527
public DateTime CreatedTime { get; set; } = DateTime.UtcNow;
2628
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,26 @@ public partial class FileRepository
1616
return Users.FirstOrDefault(x => x.Phone == phone);
1717
}
1818

19+
public User? GetAffiliateUserByPhone(string phone)
20+
{
21+
return Users.FirstOrDefault(x => x.Phone == phone && x.Type == UserType.Affiliate);
22+
}
23+
1924
public User? GetUserById(string id = null)
2025
{
2126
return Users.FirstOrDefault(x => x.Id == id || (x.ExternalId != null && x.ExternalId == id));
2227
}
2328

29+
public List<User> GetUserByIds(List<string> ids)
30+
{
31+
return Users.Where(x => ids.Contains(x.Id) || (x.ExternalId != null && ids.Contains(x.ExternalId)))?.ToList() ?? new List<User>();
32+
}
33+
34+
public User? GetUserByAffiliateId(string affiliateId)
35+
{
36+
return Users.FirstOrDefault(x => x.AffiliateId == affiliateId);
37+
}
38+
2439
public User? GetUserByUserName(string userName = null)
2540
{
2641
return Users.FirstOrDefault(x => x.UserName == userName.ToLower());

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

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.IdentityModel.Tokens.Jwt;
1010
using System.Security.Claims;
1111
using System.Text.RegularExpressions;
12+
using System.Net;
1213

1314
namespace BotSharp.Core.Users.Services;
1415

@@ -106,19 +107,54 @@ public async Task<bool> UpdatePassword(string password, string verificationCode)
106107
return true;
107108
}
108109

110+
public async Task<Token> GetAffiliateToken(string authorization)
111+
{
112+
var base64 = Encoding.UTF8.GetString(Convert.FromBase64String(authorization));
113+
var (id, password) = base64.SplitAsTuple(":");
114+
var db = _services.GetRequiredService<IBotSharpRepository>();
115+
var record = db.GetUserByPhone(id);
116+
117+
var isCanLoginAffiliateRoleType = record != null && !record.IsDisabled && record.Type != UserType.Client;
118+
if (!isCanLoginAffiliateRoleType)
119+
{
120+
return default;
121+
}
122+
123+
if (Utilities.HashTextMd5($"{password}{record.Salt}") != record.Password)
124+
{
125+
return default;
126+
}
127+
128+
var accessToken = GenerateJwtToken(record);
129+
var jwt = new JwtSecurityTokenHandler().ReadJwtToken(accessToken);
130+
var token = new Token
131+
{
132+
AccessToken = accessToken,
133+
ExpireTime = jwt.Payload.Exp.Value,
134+
TokenType = "Bearer",
135+
Scope = "api"
136+
};
137+
return token;
138+
}
139+
109140
public async Task<Token?> GetToken(string authorization)
110141
{
111142
var base64 = Encoding.UTF8.GetString(Convert.FromBase64String(authorization));
112143
var (id, password) = base64.SplitAsTuple(":");
113144

114-
var hooks = _services.GetServices<IAuthenticationHook>();
115145
var db = _services.GetRequiredService<IBotSharpRepository>();
116146
var record = id.Contains("@") ? db.GetUserByEmail(id) : db.GetUserByUserName(id);
117147
if (record == null)
118148
{
119149
record = db.GetUserByUserName(id);
120150
}
121151

152+
if (record != null && record.Type == UserType.Affiliate)
153+
{
154+
return default;
155+
}
156+
157+
var hooks = _services.GetServices<IAuthenticationHook>();
122158
//verify password is correct or not.
123159
if (record != null && !hooks.Any())
124160
{
@@ -131,7 +167,7 @@ record = db.GetUserByUserName(id);
131167

132168
User? user = record;
133169
var isAuthenticatedByHook = false;
134-
if (record == null || record.Source != "internal")
170+
if (record == null || record.Source != UserSource.Internal)
135171
{
136172
// check 3rd party user
137173
foreach (var hook in hooks)
@@ -142,7 +178,7 @@ record = db.GetUserByUserName(id);
142178
continue;
143179
}
144180

145-
if (string.IsNullOrEmpty(user.Source) || user.Source == "internal")
181+
if (string.IsNullOrEmpty(user.Source) || user.Source == UserSource.Internal)
146182
{
147183
_logger.LogError($"Please set source name in the Authenticate hook.");
148184
return null;
@@ -244,7 +280,7 @@ private string GenerateJwtToken(User user)
244280
};
245281
var tokenHandler = new JwtSecurityTokenHandler();
246282
var token = tokenHandler.CreateToken(tokenDescriptor);
247-
SaveUserTokenExpiresCache(user.Id,expires).GetAwaiter().GetResult();
283+
SaveUserTokenExpiresCache(user.Id, expires).GetAwaiter().GetResult();
248284
return tokenHandler.WriteToken(token);
249285
}
250286

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ public class UserDocument : MongoBase
1212
public string? Phone { get; set; }
1313
public string Salt { get; set; } = null!;
1414
public string Password { get; set; } = null!;
15-
public string Source { get; set; } = "internal";
15+
public string Source { get; set; } = UserSource.Internal;
1616
public string? ExternalId { get; set; }
1717
public string Type { get; set; } = UserType.Client;
1818
public string Role { get; set; } = null!;
1919
public string? VerificationCode { get; set; }
2020
public bool Verified { get; set; }
21+
public string? AffiliateId { get; set; }
22+
public bool IsDisabled { get; set; }
2123
public DateTime CreatedTime { get; set; }
2224
public DateTime UpdatedTime { get; set; }
2325

@@ -37,6 +39,8 @@ public User ToUser()
3739
ExternalId = ExternalId,
3840
Type = Type,
3941
Role = Role,
42+
AffiliateId = AffiliateId,
43+
IsDisabled = IsDisabled,
4044
VerificationCode = VerificationCode,
4145
Verified = Verified,
4246
};

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using BotSharp.Abstraction.Users.Enums;
12
using BotSharp.Abstraction.Users.Models;
23

34
namespace BotSharp.Plugin.MongoStorage.Repository;
@@ -16,13 +17,33 @@ public partial class MongoRepository
1617
return user != null ? user.ToUser() : null;
1718
}
1819

20+
public User? GetAffiliateUserByPhone(string phone)
21+
{
22+
var user = _dc.Users.AsQueryable().FirstOrDefault(x => x.Phone == phone && x.Type == UserType.Affiliate);
23+
return user != null ? user.ToUser() : null;
24+
}
25+
1926
public User? GetUserById(string id)
2027
{
2128
var user = _dc.Users.AsQueryable()
2229
.FirstOrDefault(x => x.Id == id || (x.ExternalId != null && x.ExternalId == id));
2330
return user != null ? user.ToUser() : null;
2431
}
2532

33+
public List<User> GetUserByIds(List<string> ids)
34+
{
35+
var users = _dc.Users.AsQueryable()
36+
.Where(x => ids.Contains(x.Id) || (x.ExternalId != null && ids.Contains(x.ExternalId))).ToList();
37+
return users?.Any() == true ? users.Select(x => x.ToUser()).ToList() : new List<User>();
38+
}
39+
40+
public User? GetUserByAffiliateId(string affiliateId)
41+
{
42+
var user = _dc.Users.AsQueryable()
43+
.FirstOrDefault(x => x.AffiliateId == affiliateId);
44+
return user != null ? user.ToUser() : null;
45+
}
46+
2647
public User? GetUserByUserName(string userName)
2748
{
2849
var user = _dc.Users.AsQueryable().FirstOrDefault(x => x.UserName == userName.ToLower());
@@ -49,6 +70,8 @@ public void CreateUser(User user)
4970
Type = user.Type,
5071
VerificationCode = user.VerificationCode,
5172
Verified = user.Verified,
73+
AffiliateId = user.AffiliateId,
74+
IsDisabled = user.IsDisabled,
5275
CreatedTime = DateTime.UtcNow,
5376
UpdatedTime = DateTime.UtcNow
5477
};
@@ -95,4 +118,12 @@ public void UpdateUserPhone(string userId, string phone)
95118
.Set(x => x.UpdatedTime, DateTime.UtcNow);
96119
_dc.Users.UpdateOne(filter, update);
97120
}
121+
122+
public void UpdateUserIsDisable(string userId, bool isDisable)
123+
{
124+
var filter = Builders<UserDocument>.Filter.Eq(x => x.Id, userId);
125+
var update = Builders<UserDocument>.Update.Set(x => x.IsDisabled, isDisable)
126+
.Set(x => x.UpdatedTime, DateTime.UtcNow);
127+
_dc.Users.UpdateOne(filter, update);
128+
}
98129
}

0 commit comments

Comments
 (0)