improvements to module/theme installation and removal
This commit is contained in:
@ -7,6 +7,8 @@ using Microsoft.AspNetCore.Authorization;
|
||||
using Oqtane.Infrastructure;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
|
||||
namespace Oqtane.Controllers
|
||||
{
|
||||
@ -15,11 +17,13 @@ namespace Oqtane.Controllers
|
||||
{
|
||||
private readonly IModuleDefinitionRepository ModuleDefinitions;
|
||||
private readonly IInstallationManager InstallationManager;
|
||||
private readonly IWebHostEnvironment environment;
|
||||
|
||||
public ModuleDefinitionController(IModuleDefinitionRepository ModuleDefinitions, IInstallationManager InstallationManager)
|
||||
public ModuleDefinitionController(IModuleDefinitionRepository ModuleDefinitions, IInstallationManager InstallationManager, IWebHostEnvironment environment)
|
||||
{
|
||||
this.ModuleDefinitions = ModuleDefinitions;
|
||||
this.InstallationManager = InstallationManager;
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
// GET: api/<controller>?siteid=x
|
||||
@ -55,5 +59,34 @@ namespace Oqtane.Controllers
|
||||
{
|
||||
InstallationManager.InstallPackages("Modules");
|
||||
}
|
||||
|
||||
// DELETE api/<controller>/5?siteid=x
|
||||
[HttpDelete("{id}")]
|
||||
[Authorize(Roles = Constants.HostRole)]
|
||||
public void Delete(int id, int siteid)
|
||||
{
|
||||
List<ModuleDefinition> moduledefinitions = ModuleDefinitions.GetModuleDefinitions(siteid).ToList();
|
||||
ModuleDefinition moduledefinition = moduledefinitions.Where(item => item.ModuleDefinitionId == id).FirstOrDefault();
|
||||
if (moduledefinition != null)
|
||||
{
|
||||
string moduledefinitionname = moduledefinition.ModuleDefinitionName.Substring(0, moduledefinition.ModuleDefinitionName.IndexOf(","));
|
||||
|
||||
string folder = Path.Combine(environment.WebRootPath, "Modules\\" + moduledefinitionname);
|
||||
if (Directory.Exists(folder))
|
||||
{
|
||||
Directory.Delete(folder, true);
|
||||
}
|
||||
|
||||
string binfolder = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
|
||||
foreach (string file in Directory.EnumerateFiles(binfolder, moduledefinitionname + "*.dll"))
|
||||
{
|
||||
System.IO.File.Delete(file);
|
||||
}
|
||||
|
||||
ModuleDefinitions.DeleteModuleDefinition(id, siteid);
|
||||
|
||||
InstallationManager.RestartApplication();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,8 @@ using Oqtane.Shared;
|
||||
using Oqtane.Infrastructure;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
|
||||
namespace Oqtane.Controllers
|
||||
{
|
||||
@ -15,11 +17,13 @@ namespace Oqtane.Controllers
|
||||
{
|
||||
private readonly IThemeRepository Themes;
|
||||
private readonly IInstallationManager InstallationManager;
|
||||
private readonly IWebHostEnvironment environment;
|
||||
|
||||
public ThemeController(IThemeRepository Themes, IInstallationManager InstallationManager)
|
||||
public ThemeController(IThemeRepository Themes, IInstallationManager InstallationManager, IWebHostEnvironment environment)
|
||||
{
|
||||
this.Themes = Themes;
|
||||
this.InstallationManager = InstallationManager;
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
// GET: api/<controller>
|
||||
@ -44,5 +48,32 @@ namespace Oqtane.Controllers
|
||||
{
|
||||
InstallationManager.InstallPackages("Themes");
|
||||
}
|
||||
|
||||
// DELETE api/<controller>/xxx
|
||||
[HttpDelete("{themename}")]
|
||||
[Authorize(Roles = Constants.HostRole)]
|
||||
public void Delete(string themename)
|
||||
{
|
||||
List<Theme> themes = Themes.GetThemes().ToList();
|
||||
Theme theme = themes.Where(item => item.ThemeName == themename).FirstOrDefault();
|
||||
if (theme != null)
|
||||
{
|
||||
themename = theme.ThemeName.Substring(0, theme.ThemeName.IndexOf(","));
|
||||
|
||||
string folder = Path.Combine(environment.WebRootPath, "Themes\\" + themename);
|
||||
if (Directory.Exists(folder))
|
||||
{
|
||||
Directory.Delete(folder, true);
|
||||
}
|
||||
|
||||
string binfolder = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
|
||||
foreach (string file in Directory.EnumerateFiles(binfolder, themename + "*.dll"))
|
||||
{
|
||||
System.IO.File.Delete(file);
|
||||
}
|
||||
|
||||
InstallationManager.RestartApplication();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,5 +3,6 @@
|
||||
public interface IInstallationManager
|
||||
{
|
||||
void InstallPackages(string Folders);
|
||||
void RestartApplication();
|
||||
}
|
||||
}
|
||||
|
@ -77,8 +77,13 @@ namespace Oqtane.Infrastructure
|
||||
if (install)
|
||||
{
|
||||
// restart application
|
||||
HostApplicationLifetime.StopApplication();
|
||||
RestartApplication();
|
||||
}
|
||||
}
|
||||
|
||||
public void RestartApplication()
|
||||
{
|
||||
HostApplicationLifetime.StopApplication();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ namespace Oqtane.Repository
|
||||
{
|
||||
IEnumerable<ModuleDefinition> GetModuleDefinitions(int SideId);
|
||||
void UpdateModuleDefinition(ModuleDefinition ModuleDefinition);
|
||||
void DeleteModuleDefinition(int ModuleDefinitionId, int SiteId);
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -13,6 +13,7 @@ namespace Oqtane.Repository
|
||||
void UpdatePermissions(int SiteId, string EntityName, int EntityId, string Permissions);
|
||||
Permission GetPermission(int PermissionId);
|
||||
void DeletePermission(int PermissionId);
|
||||
void DeletePermissions(int SiteId, string EntityName, int EntityId);
|
||||
string EncodePermissions(int EntityId, IEnumerable<Permission> Permissions);
|
||||
IEnumerable<Permission> DecodePermissions(string Permissions, int SiteId, string EntityName, int EntityId);
|
||||
}
|
||||
|
@ -186,5 +186,13 @@ namespace Oqtane.Repository
|
||||
{
|
||||
Permissions.UpdatePermissions(ModuleDefinition.SiteId, "ModuleDefinition", ModuleDefinition.ModuleDefinitionId, ModuleDefinition.Permissions);
|
||||
}
|
||||
|
||||
public void DeleteModuleDefinition(int ModuleDefinitionId, int SiteId)
|
||||
{
|
||||
ModuleDefinition ModuleDefinition = db.ModuleDefinition.Find(ModuleDefinitionId);
|
||||
Permissions.DeletePermissions(SiteId, "ModuleDefinition", ModuleDefinitionId);
|
||||
db.ModuleDefinition.Remove(ModuleDefinition);
|
||||
db.SaveChanges();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -61,8 +61,9 @@ namespace Oqtane.Repository
|
||||
// get current permissions and delete
|
||||
IEnumerable<Permission> permissions = db.Permission
|
||||
.Where(item => item.EntityName == EntityName)
|
||||
.Where(item => item.EntityId == EntityId);
|
||||
foreach(Permission permission in permissions)
|
||||
.Where(item => item.EntityId == EntityId)
|
||||
.Where(item => item.SiteId == SiteId);
|
||||
foreach (Permission permission in permissions)
|
||||
{
|
||||
db.Permission.Remove(permission);
|
||||
}
|
||||
@ -87,6 +88,19 @@ namespace Oqtane.Repository
|
||||
db.SaveChanges();
|
||||
}
|
||||
|
||||
public void DeletePermissions(int SiteId, string EntityName, int EntityId)
|
||||
{
|
||||
IEnumerable<Permission> permissions = db.Permission
|
||||
.Where(item => item.EntityName == EntityName)
|
||||
.Where(item => item.EntityId == EntityId)
|
||||
.Where(item => item.SiteId == SiteId);
|
||||
foreach (Permission permission in permissions)
|
||||
{
|
||||
db.Permission.Remove(permission);
|
||||
}
|
||||
db.SaveChanges();
|
||||
}
|
||||
|
||||
// permissions are stored in the format "{permissionname:!rolename1;![userid1];rolename2;rolename3;[userid2];[userid3]}" where "!" designates Deny permissions
|
||||
public string EncodePermissions(int EntityId, IEnumerable<Permission> Permissions)
|
||||
{
|
||||
|
@ -6,6 +6,7 @@ Create tables
|
||||
|
||||
CREATE TABLE [dbo].[Site](
|
||||
[SiteId] [int] IDENTITY(1,1) NOT NULL,
|
||||
[TenantId] [int] NOT NULL,
|
||||
[Name] [nvarchar](200) NOT NULL,
|
||||
[Logo] [nvarchar](50) NOT NULL,
|
||||
[DefaultThemeType] [nvarchar](200) NOT NULL,
|
||||
|
@ -187,8 +187,8 @@ namespace Oqtane.Server
|
||||
Assembly assembly = assemblies.Where(item => item.Location == file.FullName).FirstOrDefault();
|
||||
if (assembly == null)
|
||||
{
|
||||
// load assembly ( as long as dependencies are in /bin they will load as well )
|
||||
assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(file.FullName);
|
||||
// load assembly from stream to prevent locking file ( as long as dependencies are in /bin they will load as well )
|
||||
assembly = AssemblyLoadContext.Default.LoadFromStream(new MemoryStream(File.ReadAllBytes(file.FullName)));
|
||||
moduleassemblies.Add(assembly);
|
||||
}
|
||||
}
|
||||
@ -200,8 +200,8 @@ namespace Oqtane.Server
|
||||
Assembly assembly = assemblies.Where(item => item.Location == file.FullName).FirstOrDefault();
|
||||
if (assembly == null)
|
||||
{
|
||||
// load assembly ( as long as dependencies are in /bin they will load as well )
|
||||
assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(file.FullName);
|
||||
// load assembly from stream to prevent locking file ( as long as dependencies are in /bin they will load as well )
|
||||
assembly = AssemblyLoadContext.Default.LoadFromStream(new MemoryStream(File.ReadAllBytes(file.FullName)));
|
||||
}
|
||||
}
|
||||
|
||||
@ -374,9 +374,8 @@ namespace Oqtane.Server
|
||||
Assembly assembly = assemblies.Where(item => item.Location == file.FullName).FirstOrDefault();
|
||||
if (assembly == null)
|
||||
{
|
||||
// load assembly ( as long as dependencies are in /bin they will load as well )
|
||||
assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(file.FullName);
|
||||
moduleassemblies.Add(assembly);
|
||||
// load assembly from stream to prevent locking file ( as long as dependencies are in /bin they will load as well )
|
||||
assembly = AssemblyLoadContext.Default.LoadFromStream(new MemoryStream(File.ReadAllBytes(file.FullName)));
|
||||
}
|
||||
}
|
||||
|
||||
@ -387,8 +386,8 @@ namespace Oqtane.Server
|
||||
Assembly assembly = assemblies.Where(item => item.Location == file.FullName).FirstOrDefault();
|
||||
if (assembly == null)
|
||||
{
|
||||
// load assembly ( as long as dependencies are in /bin they will load as well )
|
||||
assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(file.FullName);
|
||||
// load assembly from stream to prevent locking file ( as long as dependencies are in /bin they will load as well )
|
||||
assembly = AssemblyLoadContext.Default.LoadFromStream(new MemoryStream(File.ReadAllBytes(file.FullName)));
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user