diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs index db4db62a..b435a4d1 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs @@ -48,5 +48,7 @@ public interface IAgentService string GetDataDir(); string GetAgentDataDir(string agentId); + List GetAgentsByUser(string userId); + PluginDef GetPlugin(string agentId); } diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs index 154d7398..af6cb65e 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs @@ -1,5 +1,6 @@ using BotSharp.Abstraction.Repositories.Enums; using BotSharp.Abstraction.Routing.Models; +using BotSharp.Abstraction.Users.Enums; using System.IO; namespace BotSharp.Core.Agents.Services; @@ -8,6 +9,10 @@ public partial class AgentService { public async Task UpdateAgent(Agent agent, AgentField updateField) { + var userService = _services.GetRequiredService(); + var user = await userService.GetUser(_user.Id); + if (user?.Role != UserRole.Admin) return; + if (agent == null || string.IsNullOrEmpty(agent.Id)) return; var record = _db.GetAgent(agent.Id); diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs index bd009db0..da1b8cf1 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs @@ -47,4 +47,10 @@ public partial class AgentService : IAgentService } return dir; } + + public List GetAgentsByUser(string userId) + { + var agents = _db.GetAgentsByUser(userId); + return agents; + } } diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.User.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.User.cs index 2d299f6d..8f1103e6 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.User.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.User.cs @@ -25,7 +25,6 @@ public partial class FileRepository { var userId = Guid.NewGuid().ToString(); user.Id = userId; - user.Role = UserRole.Admin; var dir = Path.Combine(_dbSettings.FileRepository, "users", userId); if (!Directory.Exists(dir)) { diff --git a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs index 6e856b9f..322b300d 100644 --- a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs +++ b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs @@ -1,4 +1,3 @@ -using BotSharp.Abstraction.Repositories; using BotSharp.Abstraction.Users.Models; using Microsoft.Extensions.Configuration; using Microsoft.IdentityModel.Tokens; diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs index b10b60e8..d867e782 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs @@ -50,17 +50,16 @@ public class AgentController : ControllerBase rule.RedirectToAgentName = found.Name; } - var editable = false; + var editable = true; var userService = _services.GetRequiredService(); var user = await userService.GetUser(_user.Id); - if (user != null && user.Role != UserRole.Admin) + if (user?.Role != UserRole.Admin) { - var db = _services.GetRequiredService(); - var userAgents = db.GetAgentsByUser(user.Id); + var userAgents = _agentService.GetAgentsByUser(user?.Id); editable = userAgents?.Select(x => x.Id)?.Contains(targetAgent.Id) ?? false; } - targetAgent.Editable = editable || user?.Role == UserRole.Admin; + targetAgent.Editable = editable; return targetAgent; } diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs index cbcc9028..f62680d2 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs @@ -50,9 +50,7 @@ public class ConversationController : ControllerBase filter.UserId = user.Role != UserRole.Admin ? user.Id : null; var conversations = await convService.GetConversations(filter); var agentService = _services.GetRequiredService(); - var list = conversations.Items - .Select(x => ConversationViewModel.FromSession(x)) - .ToList(); + var list = conversations.Items.Select(x => ConversationViewModel.FromSession(x)).ToList(); foreach (var item in list) { diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/PluginController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/PluginController.cs index 2c231e16..dc2c2740 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/PluginController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/PluginController.cs @@ -1,4 +1,5 @@ using BotSharp.Abstraction.Plugins.Models; +using BotSharp.Abstraction.Users.Enums; using BotSharp.Core.Plugins; namespace BotSharp.OpenAPI.Controllers; @@ -8,38 +9,50 @@ namespace BotSharp.OpenAPI.Controllers; public class PluginController : ControllerBase { private readonly IServiceProvider _services; + private readonly IUserIdentity _user; private readonly PluginSettings _settings; - public PluginController(IServiceProvider services, PluginSettings settings) + public PluginController(IServiceProvider services, IUserIdentity user, PluginSettings settings) { _services = services; + _user = user; _settings = settings; } [HttpGet("/plugins")] - public PagedItems GetPlugins([FromQuery] PluginFilter filter) + public async Task> GetPlugins([FromQuery] PluginFilter filter) { + var userService = _services.GetRequiredService(); + var user = await userService.GetUser(_user.Id); + if (user?.Role != UserRole.Admin) + { + return new PagedItems(); + } + var loader = _services.GetRequiredService(); return loader.GetPagedPlugins(_services, filter); } [HttpGet("/plugin/menu")] - public List GetPluginMenu() + public async Task> GetPluginMenu() { var menu = new List { new PluginMenuDef("Apps", weight: 5) { IsHeader = true, - }, - new PluginMenuDef("System", weight: 30) - { - IsHeader = true - }, - new PluginMenuDef("Plugins", link: "page/plugin", icon: "bx bx-plug", weight: 31), - new PluginMenuDef("Settings", link: "page/setting", icon: "bx bx-cog", weight: 32), + } }; + var userService = _services.GetRequiredService(); + var user = await userService.GetUser(_user.Id); + if (user?.Role == UserRole.Admin) + { + menu.Add(new PluginMenuDef("System", weight: 30) { IsHeader = true }); + menu.Add(new PluginMenuDef("Plugins", link: "page/plugin", icon: "bx bx-plug", weight: 31)); + menu.Add(new PluginMenuDef("Settings", link: "page/setting", icon: "bx bx-cog", weight: 32)); + } + var loader = _services.GetRequiredService(); foreach (var plugin in loader.GetPlugins(_services)) {