Skip to content
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

Features/add user management #720

Merged
merged 5 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -55,7 +55,7 @@ public interface IAgentService
string GetDataDir();
string GetAgentDataDir(string agentId);

List<Agent> GetAgentsByUser(string userId);
Task<List<UserAgent>> GetUserAgents(string userId);

PluginDef GetPlugin(string agentId);

Expand Down
20 changes: 18 additions & 2 deletions src/Infrastructure/BotSharp.Abstraction/Agents/Models/UserAgent.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
using BotSharp.Abstraction.Users.Models;

namespace BotSharp.Abstraction.Agents.Models;

public class UserAgent
{
[JsonPropertyName("id")]
public string Id { get; set; } = string.Empty;

[JsonPropertyName("user_id")]
public string UserId { get; set; } = string.Empty;
public string AgentId { get; set; } = string.Empty;
public bool Editable { get; set; }

[JsonPropertyName("agent_id")]
public string AgentId { get; set; }

[JsonPropertyName("actions")]
public IEnumerable<string> Actions { get; set; } = [];

[JsonIgnore]
public Agent? Agent { get; set; }

[JsonPropertyName("updated_time")]
public DateTime UpdatedTime { get; set; } = DateTime.UtcNow;

[JsonPropertyName("created_time")]
public DateTime CreatedTime { get; set; } = DateTime.UtcNow;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ namespace BotSharp.Abstraction.Repositories;

public interface IBotSharpRepository
{
int Transaction<TTableInterface>(Action action);
void Add<TTableInterface>(object entity);

#region Plugin
PluginConfig GetPluginConfig();
void SavePluginConfig(PluginConfig config);
Expand All @@ -35,13 +32,15 @@ public interface IBotSharpRepository
void UpdateUserPhone(string userId, string Iphone, string regionCode) => throw new NotImplementedException();
void UpdateUserIsDisable(string userId, bool isDisable) => throw new NotImplementedException();
void UpdateUsersIsDisable(List<string> userIds, bool isDisable) => throw new NotImplementedException();
PagedItems<User> GetUsers(UserFilter filter) => throw new NotImplementedException();
bool UpdateUser(User user, bool isUpdateUserAgents = false) => throw new NotImplementedException();
#endregion

#region Agent
void UpdateAgent(Agent agent, AgentField field);
Agent? GetAgent(string agentId);
List<Agent> GetAgents(AgentFilter filter);
List<Agent> GetAgentsByUser(string userId);
List<UserAgent> GetUserAgents(string userId);
void BulkInsertAgents(List<Agent> agents);
void BulkInsertUserAgents(List<UserAgent> userAgents);
bool DeleteAgents();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace BotSharp.Abstraction.Users.Enums;

public static class UserAction
{
public const string Edit = "edit";
public const string Chat = "chat";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace BotSharp.Abstraction.Users.Enums;

public static class UserConstant
{
public static IEnumerable<string> AdminRoles = new List<string>
{
UserRole.Admin,
UserRole.Root
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace BotSharp.Abstraction.Users.Enums;

public static class UserPermission
{
public const string CreateAgent = "create-agent";
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ public class UserRole
public const string Assistant = "assistant";

public const string Root = "root";
}
}
2 changes: 2 additions & 0 deletions src/Infrastructure/BotSharp.Abstraction/Users/IUserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace BotSharp.Abstraction.Users;
public interface IUserService
{
Task<User> GetUser(string id);
Task<PagedItems<User>> GetUsers(UserFilter filter);
Task<bool> UpdateUser(User model, bool isUpdateUserAgents = false);
Task<User> CreateUser(User user);
Task<Token> ActiveUser(UserActivationModel model);
Task<Token?> GetAffiliateToken(string authorization);
Expand Down
4 changes: 4 additions & 0 deletions src/Infrastructure/BotSharp.Abstraction/Users/Models/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public class User
public string? AffiliateId { get; set; }
public string? EmployeeId { get; set; }
public bool IsDisabled { get; set; }
public IEnumerable<string> Permissions { get; set; } = [];

[JsonIgnore]
public IEnumerable<UserAgentAction> AgentActions { get; set; } = [];
public DateTime UpdatedTime { get; set; } = DateTime.UtcNow;
public DateTime CreatedTime { get; set; } = DateTime.UtcNow;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace BotSharp.Abstraction.Users.Models;

public class UserAgentAction
{
[JsonPropertyName("id")]
public string Id { get; set; }

[JsonPropertyName("agent_id")]
public string AgentId { get; set; }

[JsonIgnore]
public Agent? Agent { get; set; }

[JsonPropertyName("actions")]
public IEnumerable<string> Actions { get; set; } = [];
}
19 changes: 19 additions & 0 deletions src/Infrastructure/BotSharp.Abstraction/Users/Models/UserFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace BotSharp.Abstraction.Users.Models;

public class UserFilter : Pagination
{
[JsonPropertyName("user_ids")]
public IEnumerable<string>? UserIds { get; set; }

[JsonPropertyName("user_names")]
public IEnumerable<string>? UserNames { get; set; }

[JsonPropertyName("external_ids")]
public IEnumerable<string>? ExternalIds { get; set; }

[JsonPropertyName("roles")]
public IEnumerable<string>? Roles { get; set; }

[JsonPropertyName("sources")]
public IEnumerable<string>? Sources { get; set; }
}
2 changes: 1 addition & 1 deletion src/Infrastructure/BotSharp.Core/Agents/AgentPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public bool AttachMenu(List<PluginMenuDef> menu)
SubMenu = new List<PluginMenuDef>
{
new PluginMenuDef("Routing", link: "page/agent/router"), // icon: "bx bx-map-pin"
new PluginMenuDef("Evaluating", link: "page/agent/evaluator") { Roles = new List<string> { UserRole.Admin } }, // icon: "bx bx-task"
new PluginMenuDef("Evaluating", link: "page/agent/evaluator") { Roles = new List<string> { UserRole.Root, UserRole.Admin } }, // icon: "bx bx-task"
new PluginMenuDef("Agents", link: "page/agent"), // icon: "bx bx-bot"
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ public partial class AgentService
{
public async Task<Agent> CreateAgent(Agent agent)
{
var agentRecord = _db.GetAgentsByUser(_user.Id).FirstOrDefault(x => x.Name.IsEqualTo(agent.Name));

if (agentRecord != null)
var userAgents = _db.GetUserAgents(_user.Id);
var found = userAgents?.FirstOrDefault(x => x.Agent != null && x.Agent.Name.IsEqualTo(agent.Name));
if (found != null)
{
return agentRecord;
return found.Agent;
}

agentRecord = Agent.Clone(agent);
var agentRecord = Agent.Clone(agent);
agentRecord.Id = Guid.NewGuid().ToString();
agentRecord.CreatedDateTime = DateTime.UtcNow;
agentRecord.UpdatedDateTime = DateTime.UtcNow;
Expand All @@ -24,21 +24,7 @@ public async Task<Agent> CreateAgent(Agent agent)
var agentSettings = _services.GetRequiredService<AgentSettings>();

var user = _db.GetUserById(_user.Id);
var userAgentRecord = new UserAgent
{
Id = Guid.NewGuid().ToString(),
UserId = user.Id,
AgentId = agentRecord.Id,
Editable = false,
CreatedTime = DateTime.UtcNow,
UpdatedTime = DateTime.UtcNow
};

_db.Transaction<IBotSharpTable>(delegate
{
_db.Add<IBotSharpTable>(agentRecord);
_db.Add<IBotSharpTable>(userAgentRecord);
});
_db.BulkInsertAgents(new List<Agent> { agentRecord });

Utilities.ClearCache();
return await Task.FromResult(agentRecord);
Expand Down Expand Up @@ -213,17 +199,4 @@ private List<AgentTask> GetTasksFromFile(string fileDir)
task.Content = content.Substring(suffix.Length).Trim();
return task;
}

private UserAgent BuildUserAgent(string agentId, string userId, bool editable = false)
{
return new UserAgent
{
Id = Guid.NewGuid().ToString(),
UserId = userId,
AgentId = agentId,
Editable = editable,
CreatedTime = DateTime.UtcNow,
UpdatedTime = DateTime.UtcNow
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ public partial class AgentService
public async Task<bool> DeleteAgent(string id)
{
var user = _db.GetUserById(_user.Id);
var agent = _db.GetAgentsByUser(_user.Id).FirstOrDefault(x => x.Id.IsEqualTo(id));
var userAgents = await GetUserAgents(user?.Id);
var found = userAgents?.FirstOrDefault(x => x.AgentId == id);

if (user?.Role != UserRole.Admin && agent == null)
if (!UserConstant.AdminRoles.Contains(user?.Role) && (found?.Actions == null || !found.Actions.Contains(UserAction.Edit)))
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,13 @@ public async Task<string> RefreshAgents()
.SetResponses(responses)
.SetSamples(samples);

var userAgent = BuildUserAgent(agent.Id, user.Id);
var tasks = GetTasksFromFile(dir);

var isAgentDeleted = _db.DeleteAgent(agent.Id);
if (isAgentDeleted)
{
await Task.Delay(100);
_db.BulkInsertAgents(new List<Agent> { agent });
_db.BulkInsertUserAgents(new List<UserAgent> { userAgent });
_db.BulkInsertAgentTasks(tasks);
refreshedAgents.Add(agent.Name);
_logger.LogInformation($"Agent {agent.Name} has been migrated.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ public partial class AgentService
{
public async Task UpdateAgent(Agent agent, AgentField updateField)
{
if (agent == null || string.IsNullOrEmpty(agent.Id)) return;

var userService = _services.GetRequiredService<IUserService>();
var user = await userService.GetUser(_user.Id);
var userAgents = GetAgentsByUser(user?.Id);
var editable = userAgents?.Select(x => x.Id)?.Contains(agent.Id) ?? false;
if (user?.Role != UserRole.Admin && !editable) return;

if (agent == null || string.IsNullOrEmpty(agent.Id)) return;
var userAgents = await GetUserAgents(user.Id);
var found = userAgents?.FirstOrDefault(x => x.AgentId == agent.Id);

if (!UserConstant.AdminRoles.Contains(user?.Role) && (found?.Actions == null || found.Actions.Contains(UserAction.Edit)))
{
return;
}

var record = _db.GetAgent(agent.Id);
if (record == null) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ public string GetAgentDataDir(string agentId)
return dir;
}

public List<Agent> GetAgentsByUser(string userId)
public async Task<List<UserAgent>> GetUserAgents(string userId)
{
var agents = _db.GetAgentsByUser(userId);
return agents;
if (string.IsNullOrEmpty(userId)) return [];

var userAgents = _db.GetUserAgents(userId);
return userAgents;
}

public IEnumerable<string> GetAgentUtilities()
Expand Down
61 changes: 1 addition & 60 deletions src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,12 @@
using BotSharp.Abstraction.Plugins.Models;
using BotSharp.Abstraction.Tasks.Models;
using BotSharp.Abstraction.Translation.Models;
using BotSharp.Abstraction.Users.Models;
using BotSharp.Abstraction.VectorStorage.Models;
using Microsoft.EntityFrameworkCore.Infrastructure;

namespace BotSharp.Core.Repository;

public class BotSharpDbContext : Database, IBotSharpRepository
{
public IQueryable<User> Users => throw new NotImplementedException();

public IQueryable<Agent> Agents => throw new NotImplementedException();

public IQueryable<UserAgent> UserAgents => throw new NotImplementedException();

public IQueryable<Conversation> Conversations => throw new NotImplementedException();

public int Transaction<TTableInterface>(Action action)
{
DatabaseFacade database = base.GetMaster(typeof(TTableInterface)).Database;
int num = 0;
if (database.CurrentTransaction == null)
{
using (Microsoft.EntityFrameworkCore.Storage.IDbContextTransaction dbContextTransaction = database.BeginTransaction())
{
try
{
action();
num = base.SaveChanges();
dbContextTransaction.Commit();
return num;
}
catch (Exception ex)
{
dbContextTransaction.Rollback();
if (ex.Message.Contains("See the inner exception for details"))
{
throw ex.InnerException;
}

throw ex;
}
}
}

try
{
action();
return base.SaveChanges();
}
catch (Exception ex2)
{
if (database.CurrentTransaction != null)
{
database.CurrentTransaction.Rollback();
}

if (ex2.Message.Contains("See the inner exception for details"))
{
throw ex2.InnerException;
}

throw ex2;
}
}

#region Plugin
public PluginConfig GetPluginConfig() => throw new NotImplementedException();
public void SavePluginConfig(PluginConfig config) => throw new NotImplementedException();
Expand All @@ -79,7 +20,7 @@ public Agent GetAgent(string agentId)
public List<Agent> GetAgents(AgentFilter filter)
=> throw new NotImplementedException();

public List<Agent> GetAgentsByUser(string userId)
public List<UserAgent> GetUserAgents(string userId)
=> throw new NotImplementedException();

public void UpdateAgent(Agent agent, AgentField field)
Expand Down
Loading