[style] Code style adjustment.
This commit is contained in:
parent
a158ca04b1
commit
d824d16e69
|
|
@ -1,23 +1,23 @@
|
|||
namespace BotSharp.Abstraction.Plugins.Models;
|
||||
|
||||
public class PluginMenuDef
|
||||
public class PluginMenuDef(string label, string? link = null, string? icon = null, int weight = 0)
|
||||
{
|
||||
[JsonIgnore]
|
||||
public string Id { get; set; }
|
||||
public string Id { get; set; } = default!;
|
||||
|
||||
public string Label { get; set; }
|
||||
public string Label { get; set; } = label;
|
||||
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
public string? Icon { get; set; }
|
||||
public string? Icon { get; set; } = icon;
|
||||
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
public string? Link { get; set; }
|
||||
public string? Link { get; set; } = link;
|
||||
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
public bool? IsHeader { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public int Weight { get; set; }
|
||||
public int Weight { get; set; } = weight;
|
||||
|
||||
[JsonIgnore]
|
||||
public List<string>? Roles { get; set; }
|
||||
|
|
@ -25,14 +25,6 @@ public class PluginMenuDef
|
|||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
public List<PluginMenuDef>? SubMenu { get; set; }
|
||||
|
||||
public PluginMenuDef(string label, string? link = null, string? icon = null, int weight = 0)
|
||||
{
|
||||
Label = label;
|
||||
Link = link;
|
||||
Icon = icon;
|
||||
Weight = weight;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{Label} {Link} {Weight}";
|
||||
|
|
|
|||
|
|
@ -1,37 +1,26 @@
|
|||
using BotSharp.Abstraction.Plugins.Models;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Xml;
|
||||
using System.Reflection;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Xml;
|
||||
|
||||
namespace BotSharp.Core.Plugins;
|
||||
|
||||
public class PluginLoader
|
||||
public class PluginLoader(IServiceCollection services,
|
||||
IConfiguration config,
|
||||
PluginSettings settings)
|
||||
{
|
||||
private readonly IServiceCollection _services;
|
||||
private readonly IConfiguration _config;
|
||||
private readonly PluginSettings _settings;
|
||||
private static List<IBotSharpPlugin> _modules = new List<IBotSharpPlugin>();
|
||||
private static List<PluginDef> _plugins = new List<PluginDef>();
|
||||
private static string _executingDir;
|
||||
|
||||
public PluginLoader(IServiceCollection services,
|
||||
IConfiguration config,
|
||||
PluginSettings settings)
|
||||
{
|
||||
_services = services;
|
||||
_config = config;
|
||||
_settings = settings;
|
||||
}
|
||||
private static List<IBotSharpPlugin> _modules = [];
|
||||
private static List<PluginDef> _plugins = [];
|
||||
private static string _executingDir = null!;
|
||||
|
||||
public void Load(Action<Assembly> loaded, string? plugin = null)
|
||||
{
|
||||
_executingDir = Directory.GetParent(Assembly.GetEntryAssembly().Location).FullName;
|
||||
|
||||
_settings.Assemblies.ToList().ForEach(assemblyName =>
|
||||
settings.Assemblies.ToList().ForEach(assemblyName =>
|
||||
{
|
||||
if (plugin != null && plugin != assemblyName)
|
||||
{
|
||||
|
|
@ -87,7 +76,7 @@ public class PluginLoader
|
|||
|
||||
private void InitModule(string assembly, IBotSharpPlugin module)
|
||||
{
|
||||
module.RegisterDI(_services, _config);
|
||||
module.RegisterDI(services, config);
|
||||
// string classSummary = GetSummaryComment(module.GetType());
|
||||
var name = string.IsNullOrEmpty(module.Name) ? module.GetType().Name : module.Name;
|
||||
_modules.Add(module);
|
||||
|
|
|
|||
|
|
@ -6,18 +6,9 @@ namespace BotSharp.OpenAPI.Controllers;
|
|||
|
||||
[Authorize]
|
||||
[ApiController]
|
||||
public class PluginController : ControllerBase
|
||||
public class PluginController(IServiceProvider services, IUserIdentity user, PluginSettings settings) : ControllerBase
|
||||
{
|
||||
private readonly IServiceProvider _services;
|
||||
private readonly IUserIdentity _user;
|
||||
private readonly PluginSettings _settings;
|
||||
|
||||
public PluginController(IServiceProvider services, IUserIdentity user, PluginSettings settings)
|
||||
{
|
||||
_services = services;
|
||||
_user = user;
|
||||
_settings = settings;
|
||||
}
|
||||
private readonly IUserIdentity _user = user;
|
||||
|
||||
[HttpGet("/plugins")]
|
||||
public async Task<PagedItems<PluginDef>> GetPlugins([FromQuery] PluginFilter filter)
|
||||
|
|
@ -28,15 +19,15 @@ public class PluginController : ControllerBase
|
|||
return new PagedItems<PluginDef>();
|
||||
}
|
||||
|
||||
var loader = _services.GetRequiredService<PluginLoader>();
|
||||
return loader.GetPagedPlugins(_services, filter);
|
||||
var loader = services.GetRequiredService<PluginLoader>();
|
||||
return loader.GetPagedPlugins(services, filter);
|
||||
}
|
||||
|
||||
[HttpGet("/plugin/menu")]
|
||||
public async Task<List<PluginMenuDef>> GetPluginMenu()
|
||||
{
|
||||
var menu = new List<PluginMenuDef>
|
||||
{
|
||||
List<PluginMenuDef> menu =
|
||||
[
|
||||
new PluginMenuDef("Apps", weight: 5)
|
||||
{
|
||||
IsHeader = true,
|
||||
|
|
@ -44,28 +35,28 @@ public class PluginController : ControllerBase
|
|||
new PluginMenuDef("System", weight: 30)
|
||||
{
|
||||
IsHeader = true,
|
||||
Roles = new List<string> { UserRole.Root, UserRole.Admin }
|
||||
Roles = [UserRole.Root, UserRole.Admin]
|
||||
},
|
||||
new PluginMenuDef("Plugins", link: "page/plugin", icon: "bx bx-plug", weight: 31)
|
||||
{
|
||||
Roles = new List<string> { UserRole.Root, UserRole.Admin }
|
||||
Roles = [UserRole.Root, UserRole.Admin]
|
||||
},
|
||||
new PluginMenuDef("Settings", link: "page/setting", icon: "bx bx-cog", weight: 32)
|
||||
{
|
||||
Roles = new List<string> { UserRole.Root, UserRole.Admin }
|
||||
Roles = [UserRole.Root, UserRole.Admin]
|
||||
},
|
||||
new PluginMenuDef("Roles", link: "page/roles", icon: "bx bx-group", weight: 33)
|
||||
{
|
||||
Roles = new List<string> { UserRole.Root, UserRole.Admin }
|
||||
Roles = [UserRole.Root, UserRole.Admin]
|
||||
},
|
||||
new PluginMenuDef("Users", link: "page/users", icon: "bx bx-user", weight: 34)
|
||||
{
|
||||
Roles = new List<string> { UserRole.Root, UserRole.Admin }
|
||||
Roles = [UserRole.Root, UserRole.Admin]
|
||||
}
|
||||
};
|
||||
];
|
||||
|
||||
var loader = _services.GetRequiredService<PluginLoader>();
|
||||
foreach (var plugin in loader.GetPlugins(_services))
|
||||
var loader = services.GetRequiredService<PluginLoader>();
|
||||
foreach (var plugin in loader.GetPlugins(services))
|
||||
{
|
||||
if (!plugin.Enabled)
|
||||
{
|
||||
|
|
@ -74,7 +65,7 @@ public class PluginController : ControllerBase
|
|||
plugin.Module.AttachMenu(menu);
|
||||
}
|
||||
|
||||
var userService = _services.GetRequiredService<IUserService>();
|
||||
var userService = services.GetRequiredService<IUserService>();
|
||||
var user = await userService.GetUser(_user.Id);
|
||||
menu = loader.GetPluginMenuByRoles(menu, user?.Role);
|
||||
menu = menu.OrderBy(x => x.Weight).ToList();
|
||||
|
|
@ -84,20 +75,20 @@ public class PluginController : ControllerBase
|
|||
[HttpPost("/plugin/{id}/install")]
|
||||
public PluginDef InstallPlugin([FromRoute] string id)
|
||||
{
|
||||
var loader = _services.GetRequiredService<PluginLoader>();
|
||||
return loader.UpdatePluginStatus(_services, id, true);
|
||||
var loader = services.GetRequiredService<PluginLoader>();
|
||||
return loader.UpdatePluginStatus(services, id, true);
|
||||
}
|
||||
|
||||
[HttpPost("/plugin/{id}/remove")]
|
||||
public PluginDef RemovePluginStats([FromRoute] string id)
|
||||
{
|
||||
var loader = _services.GetRequiredService<PluginLoader>();
|
||||
return loader.UpdatePluginStatus(_services, id, false);
|
||||
var loader = services.GetRequiredService<PluginLoader>();
|
||||
return loader.UpdatePluginStatus(services, id, false);
|
||||
}
|
||||
|
||||
private async Task<bool> IsValidUser()
|
||||
{
|
||||
var userService = _services.GetRequiredService<IUserService>();
|
||||
var userService = services.GetRequiredService<IUserService>();
|
||||
return await userService.IsAdminUser(_user.Id);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue