diff --git a/src/Infrastructure/BotSharp.Abstraction/Users/Models/UserAuthorization.cs b/src/Infrastructure/BotSharp.Abstraction/Users/Models/UserAuthorization.cs index 56df6a7d..f3c4c7f0 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Users/Models/UserAuthorization.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Users/Models/UserAuthorization.cs @@ -22,4 +22,26 @@ public static class UserAuthorizationExtension var actions = found.Actions ?? []; return actions.Any(x => x == targetAction); } + + /// + /// Get allowed user actions on the agent. If user is admin, returns null; + /// + /// + /// + /// + public static IEnumerable? GetAllowedAgentActions(this UserAuthorization auth, string agentId) + { + if (auth == null || string.IsNullOrEmpty(agentId)) + { + return []; + } + + if (auth.IsAdmin) + { + return null; + } + + var found = auth.AgentActions.FirstOrDefault(x => x.AgentId == agentId); + return found?.Actions ?? []; + } } \ No newline at end of file diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs index 40c4c03f..d8b701c9 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs @@ -1,5 +1,4 @@ using BotSharp.Abstraction.Agents.Models; -using BotSharp.Abstraction.Users.Enums; namespace BotSharp.OpenAPI.Controllers; @@ -60,11 +59,7 @@ public class AgentController : ControllerBase var userService = _services.GetRequiredService(); var auth = await userService.GetUserAuthorizations(new List { targetAgent.Id }); - - targetAgent.Editable = auth.IsAgentActionAllowed(targetAgent.Id, UserAction.Edit); - targetAgent.Chatable = auth.IsAgentActionAllowed(targetAgent.Id, UserAction.Chat); - targetAgent.Trainable = auth.IsAgentActionAllowed(targetAgent.Id, UserAction.Train); - targetAgent.Evaluable = auth.IsAgentActionAllowed(targetAgent.Id, UserAction.Evaluate); + targetAgent.Actions = auth.GetAllowedAgentActions(targetAgent.Id); return targetAgent; } @@ -91,10 +86,7 @@ public class AgentController : ControllerBase agents = pagedAgents?.Items?.Select(x => { var model = AgentViewModel.FromAgent(x); - model.Editable = auth.IsAgentActionAllowed(x.Id, UserAction.Edit); - model.Chatable = auth.IsAgentActionAllowed(x.Id, UserAction.Chat); - model.Trainable = auth.IsAgentActionAllowed(x.Id, UserAction.Train); - model.Evaluable = auth.IsAgentActionAllowed(x.Id, UserAction.Evaluate); + model.Actions = auth.GetAllowedAgentActions(x.Id); return model; })?.ToList() ?? []; diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs index 46dea0af..34b1bcc1 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs @@ -48,10 +48,7 @@ public class AgentViewModel public PluginDef Plugin { get; set; } - public bool Editable { get; set; } - public bool Chatable { get; set; } - public bool Trainable { get; set; } - public bool Evaluable { get; set; } + public IEnumerable? Actions { get; set; } [JsonPropertyName("created_datetime")] public DateTime CreatedDateTime { get; set; }