Skip to content

Commit d85e16d

Browse files
author
jason.wang
committed
add affiliate token api
1 parent 4752073 commit d85e16d

File tree

10 files changed

+98
-17
lines changed

10 files changed

+98
-17
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

src/Infrastructure/BotSharp.Abstraction/Users/Enums/UserRole.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ public class UserRole
3333
/// AI Assistant
3434
/// </summary>
3535
public const string Assistant = "assistant";
36+
public const string Affiliate = "affiliate";
3637
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
public const string Affiliate = "affiliate";
13+
}
14+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public interface IUserService
99
Task<User> CreateUser(User user);
1010
Task<Token> ActiveUser(UserActivationModel model);
1111
Task<Token?> GetAffiliateToken(string authorization);
12-
Task<Token?> GetClientToken(string authorization);
12+
Task<Token?> GetToken(string authorization);
1313
Task<User> GetMyProfile();
1414
Task<bool> VerifyUserNameExisting(string userName);
1515
Task<bool> VerifyEmailExisting(string email);

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 IsDisable { 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: 22 additions & 12 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

@@ -113,16 +114,30 @@ public async Task<Token> GetAffiliateToken(string authorization)
113114
var db = _services.GetRequiredService<IBotSharpRepository>();
114115
var record = db.GetUserByPhone(id);
115116

116-
var isCanLoginAffiliateRoleType = record == null && record.Type != UserType.Client;
117-
if (isCanLoginAffiliateRoleType)
117+
var isCanLoginAffiliateRoleType = record != null && !record.IsDisable && record.Type != UserType.Client;
118+
if (!isCanLoginAffiliateRoleType)
118119
{
119-
return await GetToken(record, id, password);
120+
return default;
121+
}
122+
123+
if (Utilities.HashTextMd5($"{password}{record.Salt}") != record.Password)
124+
{
125+
return default;
120126
}
121127

122-
return default;
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;
123138
}
124139

125-
public async Task<Token> GetClientToken(string authorization)
140+
public async Task<Token?> GetToken(string authorization)
126141
{
127142
var base64 = Encoding.UTF8.GetString(Convert.FromBase64String(authorization));
128143
var (id, password) = base64.SplitAsTuple(":");
@@ -139,11 +154,6 @@ record = db.GetUserByUserName(id);
139154
return default;
140155
}
141156

142-
return await GetToken(record, id, password);
143-
}
144-
145-
private async Task<Token?> GetToken(User record, string id, string password)
146-
{
147157
var hooks = _services.GetServices<IAuthenticationHook>();
148158
//verify password is correct or not.
149159
if (record != null && !hooks.Any())
@@ -157,7 +167,7 @@ record = db.GetUserByUserName(id);
157167

158168
User? user = record;
159169
var isAuthenticatedByHook = false;
160-
if (record == null || record.Source != "internal")
170+
if (record == null || record.Source != UserSource.Internal)
161171
{
162172
// check 3rd party user
163173
foreach (var hook in hooks)
@@ -168,7 +178,7 @@ record = db.GetUserByUserName(id);
168178
continue;
169179
}
170180

171-
if (string.IsNullOrEmpty(user.Source) || user.Source == "internal")
181+
if (string.IsNullOrEmpty(user.Source) || user.Source == UserSource.Internal)
172182
{
173183
_logger.LogError($"Please set source name in the Authenticate hook.");
174184
return null;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public async Task<ActionResult<Token>> GetToken([FromHeader(Name = "Authorizatio
2525
authcode = authcode.Split(' ')[1];
2626
}
2727

28-
var token = await _userService.GetClientToken(authcode);
28+
var token = await _userService.GetToken(authcode);
2929

3030
if (token == null)
3131
{

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 IsDisable { 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+
IsDisable = IsDisable,
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+
IsDisable = user.IsDisable,
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.IsDisable, isDisable)
126+
.Set(x => x.UpdatedTime, DateTime.UtcNow);
127+
_dc.Users.UpdateOne(filter, update);
128+
}
98129
}

0 commit comments

Comments
 (0)