add user role in menu
This commit is contained in:
parent
394d607163
commit
ca5407f97c
|
|
@ -19,6 +19,9 @@ public class PluginMenuDef
|
|||
[JsonIgnore]
|
||||
public int Weight { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public List<string>? Roles { get; set; }
|
||||
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
public List<PluginMenuDef>? SubMenu { get; set; }
|
||||
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ public class AgentPlugin : IBotSharpPlugin
|
|||
{
|
||||
SubMenu = new List<PluginMenuDef>
|
||||
{
|
||||
new PluginMenuDef("Routing", link: "page/agent/router"), // icon: "bx bx-map-pin"
|
||||
new PluginMenuDef("Routing", link: "page/agent/router") { Roles = new List<string> { "admin" } }, // icon: "bx bx-map-pin"
|
||||
new PluginMenuDef("Evaluating", link: "page/agent/evaluator"), // icon: "bx bx-task"
|
||||
new PluginMenuDef("Agents", link: "page/agent"), // icon: "bx bx-bot"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -269,4 +269,20 @@ public class PluginLoader
|
|||
}
|
||||
});
|
||||
}
|
||||
|
||||
public List<PluginMenuDef> FilterPluginsByRoles(List<PluginMenuDef> plugins, string userRole)
|
||||
{
|
||||
if (plugins.IsNullOrEmpty()) return plugins;
|
||||
|
||||
var filtered = new List<PluginMenuDef>();
|
||||
foreach (var plugin in plugins)
|
||||
{
|
||||
if (plugin.Roles.IsNullOrEmpty() || plugin.Roles.Contains(userRole))
|
||||
{
|
||||
plugin.SubMenu = FilterPluginsByRoles(plugin.SubMenu, userRole);
|
||||
filtered.Add(plugin);
|
||||
}
|
||||
}
|
||||
return filtered;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
using BotSharp.Abstraction.Plugins.Models;
|
||||
using BotSharp.Abstraction.Tasks;
|
||||
using BotSharp.Abstraction.Users.Enums;
|
||||
using BotSharp.Core.Tasks.Services;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
|
|
@ -19,7 +20,10 @@ public class TaskPlugin : IBotSharpPlugin
|
|||
public bool AttachMenu(List<PluginMenuDef> menu)
|
||||
{
|
||||
var section = menu.First(x => x.Label == "Apps");
|
||||
menu.Add(new PluginMenuDef("Task", link: "page/task", icon: "bx bx-task", weight: section.Weight + 8));
|
||||
menu.Add(new PluginMenuDef("Task", link: "page/task", icon: "bx bx-task", weight: section.Weight + 8)
|
||||
{
|
||||
Roles = new List<string> { UserRole.Admin }
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,18 +41,22 @@ public class PluginController : ControllerBase
|
|||
new PluginMenuDef("Apps", weight: 5)
|
||||
{
|
||||
IsHeader = true,
|
||||
},
|
||||
new PluginMenuDef("System", weight: 30)
|
||||
{
|
||||
IsHeader = true,
|
||||
Roles = new List<string> { UserRole.Admin }
|
||||
},
|
||||
new PluginMenuDef("Plugins", link: "page/plugin", icon: "bx bx-plug", weight: 31)
|
||||
{
|
||||
Roles = new List<string> { UserRole.Admin }
|
||||
},
|
||||
new PluginMenuDef("Settings", link: "page/setting", icon: "bx bx-cog", weight: 32)
|
||||
{
|
||||
Roles = new List<string> { UserRole.Admin }
|
||||
}
|
||||
};
|
||||
|
||||
var userService = _services.GetRequiredService<IUserService>();
|
||||
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<PluginLoader>();
|
||||
foreach (var plugin in loader.GetPlugins(_services))
|
||||
{
|
||||
|
|
@ -62,6 +66,10 @@ public class PluginController : ControllerBase
|
|||
}
|
||||
plugin.Module.AttachMenu(menu);
|
||||
}
|
||||
|
||||
var userService = _services.GetRequiredService<IUserService>();
|
||||
var user = await userService.GetUser(_user.Id);
|
||||
menu = loader.FilterPluginsByRoles(menu, user?.Role);
|
||||
menu = menu.OrderBy(x => x.Weight).ToList();
|
||||
return menu;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue