Refactoring PackagingServicesController, adding UninstallModule action.

This commit is contained in:
Lombiq
2015-01-18 19:15:10 +01:00
committed by Zoltán Lehóczky
parent 857b73052c
commit 7e355a65c2

View File

@@ -2,10 +2,12 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web.Hosting;
using System.Web.Mvc;
using System.Xml.Linq;
using NuGet;
using Orchard.Environment;
using Orchard.Environment.Configuration;
using Orchard.Environment.Extensions.Models;
using Orchard.FileSystems.AppData;
@@ -33,6 +35,7 @@ namespace Orchard.Packaging.Controllers {
private readonly IPackagingSourceManager _packagingSourceManager;
private readonly IAppDataFolderRoot _appDataFolderRoot;
private readonly IModuleService _moduleService;
private readonly IHostEnvironment _hostEnvironment;
private readonly IRecipeHarvester _recipeHarvester;
private readonly IRecipeManager _recipeManager;
@@ -42,8 +45,9 @@ namespace Orchard.Packaging.Controllers {
IPackagingSourceManager packagingSourceManager,
IAppDataFolderRoot appDataFolderRoot,
IOrchardServices services,
IModuleService moduleService)
: this(shellSettings, packageManager, packagingSourceManager, appDataFolderRoot, services, moduleService, null, null) {
IModuleService moduleService,
IHostEnvironment hostEnvironment)
: this(shellSettings, packageManager, packagingSourceManager, appDataFolderRoot, services, moduleService, hostEnvironment, null, null) {
}
public PackagingServicesController(
@@ -53,6 +57,7 @@ namespace Orchard.Packaging.Controllers {
IAppDataFolderRoot appDataFolderRoot,
IOrchardServices services,
IModuleService moduleService,
IHostEnvironment hostEnvironment,
IRecipeHarvester recipeHarvester,
IRecipeManager recipeManager) {
@@ -60,6 +65,7 @@ namespace Orchard.Packaging.Controllers {
_packageManager = packageManager;
_appDataFolderRoot = appDataFolderRoot;
_moduleService = moduleService;
_hostEnvironment = hostEnvironment;
_recipeHarvester = recipeHarvester;
_recipeManager = recipeManager;
_packagingSourceManager = packagingSourceManager;
@@ -80,14 +86,30 @@ namespace Orchard.Packaging.Controllers {
return View();
}
[HttpPost, ActionName("RemoveTheme")]
public ActionResult RemoveThemePOST(string themeId, string returnUrl, string retryUrl) {
[HttpPost, ActionName("UninstallTheme")]
public ActionResult UninstallThemePost(string themeId, string returnUrl, string retryUrl) {
if (String.IsNullOrEmpty(themeId)) {
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
if (_shellSettings.Name != ShellSettings.DefaultName || !Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not authorized to remove themes")))
return new HttpUnauthorizedResult();
return UninstallPackage(PackageBuilder.BuildPackageId(themeId, DefaultExtensionTypes.Theme), returnUrl, retryUrl);
}
[HttpPost, ActionName("UninstallModule")]
public ActionResult UninstallModulePost(string moduleId, string returnUrl, string retryUrl) {
if (String.IsNullOrEmpty(moduleId)) {
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
if (_shellSettings.Name != ShellSettings.DefaultName || !Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not authorized to remove modules")))
return new HttpUnauthorizedResult();
return UninstallPackage(PackageBuilder.BuildPackageId(moduleId, DefaultExtensionTypes.Module), returnUrl, retryUrl);
}
public ActionResult AddModule(string returnUrl) {
if (_shellSettings.Name != ShellSettings.DefaultName || !Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not authorized to add modules")))
return new HttpUnauthorizedResult();
@@ -105,7 +127,7 @@ namespace Orchard.Packaging.Controllers {
}
try {
PackageInfo packageInfo = _packageManager.Install(packageId, version, source.FeedUrl, HostingEnvironment.MapPath("~/"));
PackageInfo packageInfo = _packageManager.Install(packageId, version, source.FeedUrl, MapAppRoot());
if (DefaultExtensionTypes.IsTheme(packageInfo.ExtensionType)) {
Services.Notifier.Information(T("The theme has been successfully installed. It can be enabled in the \"Themes\" page accessible from the menu."));
@@ -147,7 +169,7 @@ namespace Orchard.Packaging.Controllers {
string fullFileName = Path.Combine(_appDataFolderRoot.RootFolder, Path.GetFileName(httpPostedFileBase.FileName)).Replace(Path.DirectorySeparatorChar, '/');
httpPostedFileBase.SaveAs(fullFileName);
var package = new ZipPackage(fullFileName);
PackageInfo packageInfo = _packageManager.Install(package, _appDataFolderRoot.RootFolder, HostingEnvironment.MapPath("~/"));
PackageInfo packageInfo = _packageManager.Install(package, _appDataFolderRoot.RootFolder, MapAppRoot());
ExtensionDescriptor extensionDescriptor = package.GetExtensionDescriptor(packageInfo.ExtensionType);
System.IO.File.Delete(fullFileName);
@@ -247,20 +269,21 @@ namespace Orchard.Packaging.Controllers {
return Redirect(redirectUrl);
}
public ActionResult UninstallPackage(string id, string returnUrl, string retryUrl) {
if (_shellSettings.Name != ShellSettings.DefaultName || !Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not authorized to uninstall packages")))
return new HttpUnauthorizedResult();
private ActionResult UninstallPackage(string id, string returnUrl, string retryUrl) {
try {
_packageManager.Uninstall(id, HostingEnvironment.MapPath("~/"));
_packageManager.Uninstall(id, MapAppRoot());
}
catch (Exception exception) {
Services.Notifier.Error(T("Uninstall failed: {0}", exception.Message));
return Redirect(retryUrl);
return Redirect(!String.IsNullOrEmpty(retryUrl) ? retryUrl : returnUrl);
}
Services.Notifier.Information(T("Uninstalled package \"{0}\"", id));
return this.RedirectLocal(returnUrl, "~/");
}
private string MapAppRoot() {
return _hostEnvironment.MapPath("~/");
}
}
}