diff --git a/src/Infrastructure/BotSharp.Abstraction/Users/IUserService.cs b/src/Infrastructure/BotSharp.Abstraction/Users/IUserService.cs index 8d0fae18..f4db6628 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Users/IUserService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Users/IUserService.cs @@ -8,7 +8,8 @@ public interface IUserService Task GetUser(string id); Task CreateUser(User user); Task ActiveUser(UserActivationModel model); - Task GetToken(string authorization); + Task GetAffiliateToken(string authorization); + Task GetClientToken(string authorization); Task GetMyProfile(); Task VerifyUserNameExisting(string userName); Task VerifyEmailExisting(string email); diff --git a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs index f9758be6..e8fb5185 100644 --- a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs +++ b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs @@ -106,12 +106,27 @@ public class UserService : IUserService return true; } - public async Task GetToken(string authorization) + public async Task GetAffiliateToken(string authorization) + { + var base64 = Encoding.UTF8.GetString(Convert.FromBase64String(authorization)); + var (id, password) = base64.SplitAsTuple(":"); + var db = _services.GetRequiredService(); + var record = db.GetUserByPhone(id); + + var isCanLoginAffiliateRoleType = record == null && record.Type != UserType.Client; + if (isCanLoginAffiliateRoleType) + { + return await GetToken(record, id, password); + } + + return default; + } + + public async Task GetClientToken(string authorization) { var base64 = Encoding.UTF8.GetString(Convert.FromBase64String(authorization)); var (id, password) = base64.SplitAsTuple(":"); - var hooks = _services.GetServices(); var db = _services.GetRequiredService(); var record = id.Contains("@") ? db.GetUserByEmail(id) : db.GetUserByUserName(id); if (record == null) @@ -119,6 +134,17 @@ public class UserService : IUserService record = db.GetUserByUserName(id); } + if (record != null && record.Type == UserType.Affiliate) + { + return default; + } + + return await GetToken(record, id, password); + } + + private async Task GetToken(User record, string id, string password) + { + var hooks = _services.GetServices(); //verify password is correct or not. if (record != null && !hooks.Any()) { @@ -242,7 +268,7 @@ public class UserService : IUserService }; var tokenHandler = new JwtSecurityTokenHandler(); var token = tokenHandler.CreateToken(tokenDescriptor); - SaveUserTokenExpiresCache(user.Id,expires).GetAwaiter().GetResult(); + SaveUserTokenExpiresCache(user.Id, expires).GetAwaiter().GetResult(); return tokenHandler.WriteToken(token); } diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs index 6a9b0055..a0fb72b0 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs @@ -25,7 +25,7 @@ public class UserController : ControllerBase authcode = authcode.Split(' ')[1]; } - var token = await _userService.GetToken(authcode); + var token = await _userService.GetClientToken(authcode); if (token == null) {