Skip to content

Commit

Permalink
Merge pull request #663 from Qtoss-AI/master
Browse files Browse the repository at this point in the history
return not found if no records.
  • Loading branch information
Oceania2018 authored Oct 2, 2024
2 parents 8e0eb63 + 89205bd commit 63fb864
Show file tree
Hide file tree
Showing 31 changed files with 244 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace BotSharp.Abstraction.Browsing.Models;

public class BrowserActionResult
{
public int ResponseStatusCode { get; set; }
public bool IsSuccess { get; set; }
public string? Message { get; set; }
public string? StackTrace { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ public interface ICacheService
Task<T?> GetAsync<T>(string key);
Task<object> GetAsync(string key, Type type);
Task SetAsync<T>(string key, T value, TimeSpan? expiry);
Task RemoveAsync(string key);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ public override void OnEntry(MethodContext context)
var value = cache.GetAsync(key, context.TaskReturnType).Result;
if (value != null)
{
context.ReplaceReturnValue(this, value);
// check if the cache is out of date
var isOutOfDate = IsOutOfDate(context, value).Result;

if (!isOutOfDate)
{
context.ReplaceReturnValue(this, value);
}
}
}

Expand All @@ -58,6 +64,11 @@ public override void OnSuccess(MethodContext context)
}
}

public virtual Task<bool> IsOutOfDate(MethodContext context, object value)
{
return Task.FromResult(false);
}

private string GetCacheKey(SharpCacheSettings settings, MethodContext context)
{
var key = settings.Prefix + "-" + context.Method.Name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,26 @@ public interface IBotSharpRepository
void Add<TTableInterface>(object entity);

#region Plugin
PluginConfig GetPluginConfig();
PluginConfig GetPluginConfig();
void SavePluginConfig(PluginConfig config);
#endregion

#region User
User? GetUserByEmail(string email) => throw new NotImplementedException();
User? GetUserByPhone(string phone) => throw new NotImplementedException();
User? GetAffiliateUserByPhone(string phone) => throw new NotImplementedException();
User? GetUserById(string id) => throw new NotImplementedException();
User? GetUserById(string id) => throw new NotImplementedException();
List<User> GetUserByIds(List<string> ids) => throw new NotImplementedException();
User? GetUserByAffiliateId(string affiliateId) => throw new NotImplementedException();
User? GetUserByUserName(string userName) => throw new NotImplementedException();
void CreateUser(User user) => throw new NotImplementedException();
void UpdateUserVerified(string userId) => throw new NotImplementedException();
void UpdateUserVerificationCode(string userId, string verficationCode) => throw new NotImplementedException();
void UpdateUserPassword(string userId, string password) => throw new NotImplementedException();
void UpdateUserEmail(string userId, string email)=> throw new NotImplementedException();
void UpdateUserEmail(string userId, string email) => throw new NotImplementedException();
void UpdateUserPhone(string userId, string Iphone) => throw new NotImplementedException();
void UpdateUserIsDisable(string userId, bool isDisable) => throw new NotImplementedException();
void UpdateUsersIsDisable(List<string> userIds, bool isDisable) => throw new NotImplementedException();
#endregion

#region Agent
Expand Down Expand Up @@ -76,7 +77,7 @@ public interface IBotSharpRepository
List<string> GetIdleConversations(int batchSize, int messageLimit, int bufferHours, IEnumerable<string> excludeAgentIds);
IEnumerable<string> TruncateConversation(string conversationId, string messageId, bool cleanLog = false);
#endregion

#region Execution Log
void AddExecutionLogs(string conversationId, List<string> logs);
List<string> GetExecutionLogs(string conversationId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ public interface IAuthenticationHook
void BeforeSending(Token token);
Task UserCreated(User user);
Task VerificationCodeResetPassword(User user);
Task DelUsers(List<string> userIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ public interface IUserIdentity
string FullName { get; }
string? UserLanguage { get; }
string? Phone { get; }
string? AffiliateId { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ public interface IUserService
Task<bool> ModifyUserPhone(string phone);
Task<bool> UpdatePassword(string newPassword, string verificationCode);
Task<DateTime> GetUserTokenExpires();
Task<bool> UpdateUsersIsDisable(List<string> userIds, bool isDisable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface IVectorDb
Task<IEnumerable<VectorCollectionData>> GetCollectionData(string collectionName, IEnumerable<Guid> ids, bool withPayload = false, bool withVector = false);
Task<bool> CreateCollection(string collectionName, int dimension);
Task<bool> DeleteCollection(string collectionName);
Task<bool> Upsert(string collectionName, Guid id, float[] vector, string text, Dictionary<string, string>? payload = null);
Task<bool> Upsert(string collectionName, Guid id, float[] vector, string text, Dictionary<string, object>? payload = null);
Task<IEnumerable<VectorCollectionData>> Search(string collectionName, float[] vector, IEnumerable<string>? fields, int limit = 5, float confidence = 0.5f, bool withVector = false);
Task<bool> DeleteCollectionData(string collectionName, List<Guid> ids);
Task<bool> DeleteCollectionAllData(string collectionName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace BotSharp.Abstraction.VectorStorage.Models;
public class VectorCollectionData
{
public string Id { get; set; }
public Dictionary<string, string> Data { get; set; } = new();
public Dictionary<string, object> Data { get; set; } = new();
public double? Score { get; set; }

[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ public class VectorCreateModel
{
public string Text { get; set; }
public string DataSource { get; set; } = VectorDataSource.Api;
public Dictionary<string, string>? Payload { get; set; }
public Dictionary<string, object>? Payload { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ public async Task SetAsync<T>(string key, T value, TimeSpan? expiry)
AbsoluteExpirationRelativeToNow = expiry
});
}

public async Task RemoveAsync(string key)
{
_cache.Remove(key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,20 @@ public async Task SetAsync<T>(string key, T value, TimeSpan? expiry)
var db = redis.GetDatabase();
await db.StringSetAsync(key, JsonConvert.SerializeObject(value), expiry);
}

public async Task RemoveAsync(string key)
{
if (string.IsNullOrEmpty(_settings.Redis))
{
return;
}

if (redis == null)
{
redis = ConnectionMultiplexer.Connect(_settings.Redis);
}

var db = redis.GetDatabase();
await db.KeyDeleteAsync(key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,7 @@ public string? UserLanguage

[JsonPropertyName("phone")]
public string? Phone => _claims?.FirstOrDefault(x => x.Type == "phone")?.Value;

[JsonPropertyName("affiliateId")]
public string? AffiliateId => _claims?.FirstOrDefault(x => x.Type == "affiliateId")?.Value;
}
44 changes: 36 additions & 8 deletions src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using BotSharp.Abstraction.Users.Enums;
using BotSharp.Abstraction.Infrastructures;
using BotSharp.Abstraction.Users.Enums;
using BotSharp.Abstraction.Users.Models;
using BotSharp.Abstraction.Users.Settings;
using BotSharp.OpenAPI.ViewModels.Users;
Expand All @@ -9,7 +9,6 @@
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text.RegularExpressions;
using System.Net;

namespace BotSharp.Core.Users.Services;

Expand Down Expand Up @@ -112,7 +111,11 @@ public async Task<Token> GetAffiliateToken(string authorization)
var base64 = Encoding.UTF8.GetString(Convert.FromBase64String(authorization));
var (id, password) = base64.SplitAsTuple(":");
var db = _services.GetRequiredService<IBotSharpRepository>();
var record = db.GetUserByPhone(id);
var record = db.GetAffiliateUserByPhone(id);
if (record == null)
{
record = db.GetUserByPhone(id);
}

var isCanLoginAffiliateRoleType = record != null && !record.IsDisabled && record.Type != UserType.Client;
if (!isCanLoginAffiliateRoleType)
Expand Down Expand Up @@ -254,7 +257,8 @@ private string GenerateJwtToken(User user)
new Claim("type", user.Type ?? UserType.Client),
new Claim("role", user.Role ?? UserRole.User),
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
new Claim("phone", user.Phone ?? string.Empty)
new Claim("phone", user.Phone ?? string.Empty),
new Claim("affiliateId", user.AffiliateId ?? string.Empty)
};

var validators = _services.GetServices<IAuthenticationHook>();
Expand All @@ -280,14 +284,19 @@ private string GenerateJwtToken(User user)
};
var tokenHandler = new JwtSecurityTokenHandler();
var token = tokenHandler.CreateToken(tokenDescriptor);
SaveUserTokenExpiresCache(user.Id, expires).GetAwaiter().GetResult();
SaveUserTokenExpiresCache(user.Id, expires, expireInMinutes).GetAwaiter().GetResult();
return tokenHandler.WriteToken(token);
}

private async Task SaveUserTokenExpiresCache(string userId, DateTime expires)
private async Task SaveUserTokenExpiresCache(string userId, DateTime expires, int expireInMinutes)
{
var _cacheService = _services.GetRequiredService<ICacheService>();
await _cacheService.SetAsync<DateTime>(GetUserTokenExpiresCacheKey(userId), expires, null);
var config = _services.GetService<IConfiguration>();
var enableSingleLogin = bool.Parse(config["Jwt:EnableSingleLogin"] ?? "false");
if (enableSingleLogin)
{
var _cacheService = _services.GetRequiredService<ICacheService>();
await _cacheService.SetAsync(GetUserTokenExpiresCacheKey(userId), expires, TimeSpan.FromMinutes(expireInMinutes));
}
}

private string GetUserTokenExpiresCacheKey(string userId)
Expand Down Expand Up @@ -514,4 +523,23 @@ public async Task<bool> ModifyUserPhone(string phone)
db.UpdateUserPhone(record.Id, phone);
return true;
}

public async Task<bool> UpdateUsersIsDisable(List<string> userIds, bool isDisable)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
db.UpdateUsersIsDisable(userIds, isDisable);

if (!isDisable)
{
return true;
}

// del membership
var hooks = _services.GetServices<IAuthenticationHook>();
foreach (var hook in hooks)
{
await hook.DelUsers(userIds);
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public async Task<ActionResult<Token>> ActivateUser(UserActivationModel model)
var token = await _userService.ActiveUser(model);
if (token == null)
{
return Unauthorized();
return BadRequest();
}
return Ok(token);
}
Expand Down Expand Up @@ -143,6 +143,12 @@ public async Task<bool> ModifyUserPhone([FromQuery] string phone)
return await _userService.ModifyUserPhone(phone);
}

[HttpPost("/user/update/isdisable")]
public async Task<bool> UpdateUsersIsDisable([FromQuery] List<string> userIds, [FromQuery] bool isDisable)
{
return await _userService.UpdateUsersIsDisable(userIds, isDisable);
}

#region Avatar
[HttpPost("/user/avatar")]
public bool UploadUserAvatar([FromBody] UserAvatarModel input)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ public UserSingleLoginFilter(IUserService userService, IServiceProvider services

public void OnAuthorization(AuthorizationFilterContext context)
{
var isAllowAnonymous = context.ActionDescriptor.EndpointMetadata
.Any(em => em.GetType() == typeof(AllowAnonymousAttribute));

if (isAllowAnonymous)
{
return;
}

var bearerToken = GetBearerToken(context);
if (!string.IsNullOrWhiteSpace(bearerToken))
{
Expand All @@ -37,7 +45,8 @@ public void OnAuthorization(AuthorizationFilterContext context)
if (validTo != currentExpires)
{
Serilog.Log.Warning($"Token expired. Token expires at {validTo}, current expires at {currentExpires}");
context.Result = new UnauthorizedResult();
// login confict
context.Result = new ConflictResult();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ public class VectorKnowledgeCreateRequest
public string DataSource { get; set; } = VectorDataSource.Api;

[JsonPropertyName("payload")]
public Dictionary<string, string>? Payload { get; set; }
public Dictionary<string, object>? Payload { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class VectorKnowledgeViewModel
public string Id { get; set; }

[JsonPropertyName("data")]
public IDictionary<string, string> Data { get; set; }
public IDictionary<string, object> Data { get; set; }

[JsonPropertyName("score")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
Expand Down
Loading

0 comments on commit 63fb864

Please sign in to comment.