Skip to content

return not found if no records. #663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 26 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
8c460d5
return not found if no records.
Oceania2018 Sep 24, 2024
3f24bdd
add affiliateId claim & add redis expire
Sep 24, 2024
515783e
Merge remote-tracking branch 'origin/master' into jason_dev
Sep 24, 2024
eca30ce
Merge branch 'SciSharp:master' into master
Oceania2018 Sep 25, 2024
6e611b7
Merge pull request #14 from Qtoss-AI/jason_dev
Oceania2018 Sep 25, 2024
feb48a1
login confict
Oceania2018 Sep 25, 2024
d5b74bf
Merge branch 'master' of https://github.com/Qtoss-AI/BotSharp
Oceania2018 Sep 25, 2024
a38c277
fix get affiliate token user issue
Sep 25, 2024
60275e0
Merge remote-tracking branch 'origin/master' into jason_dev
Sep 25, 2024
8f6fbed
UserSingleLoginFilter add token expired don't valid single login
Sep 25, 2024
c77b575
AllowAnonymous api no valid single login
Sep 25, 2024
0d78a39
Merge pull request #15 from Qtoss-AI/jason_dev
Oceania2018 Sep 25, 2024
ebeccad
Add RemoveAsync cache
Oceania2018 Sep 26, 2024
08d1a40
AI搜索都基于商品来展开。
Oceania2018 Sep 28, 2024
af48457
Support more data type in Qdrant payload.
Oceania2018 Sep 29, 2024
9125db9
Handle content filter error for AzureOpenAI.
Oceania2018 Sep 29, 2024
e1aeb79
Merge branch 'SciSharp:master' into master
Oceania2018 Sep 29, 2024
63659d5
fix: modify the return result status code
AnonymousDotNet Sep 29, 2024
50a94f3
Merge branch 'master' into lida_dev
AnonymousDotNet Sep 29, 2024
2c77ad0
remove Unauthorized
AnonymousDotNet Sep 29, 2024
8e060c9
feat: add del user api
AnonymousDotNet Sep 29, 2024
35fef33
Merge pull request #16 from AnonymousDotNet/lida_dev
Oceania2018 Sep 29, 2024
15fbf98
Merge branch 'SciSharp:master' into master
Oceania2018 Sep 30, 2024
dcd3fc5
Issues-321
Oct 1, 2024
cc44845
Merge pull request #17 from Qtoss-AI/Issues-321
Oceania2018 Oct 1, 2024
89205bd
Sync with upstream
Oceania2018 Oct 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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