Files
Umbraco-CMS/src/Umbraco.Web.Common/Install/InstallController.cs

123 lines
4.4 KiB
C#
Raw Normal View History

2020-04-20 12:20:47 +02:00
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using System.Threading.Tasks;
using Umbraco.Core;
using Umbraco.Core.Configuration;
using Umbraco.Core.Hosting;
using Umbraco.Core.Logging;
using Umbraco.Core.WebAssets;
using Umbraco.Extensions;
2020-04-27 10:09:10 +02:00
using Umbraco.Web.Common.Filters;
2020-04-20 12:20:47 +02:00
using Umbraco.Web.Install;
using Umbraco.Web.Security;
2020-04-20 12:20:47 +02:00
namespace Umbraco.Web.Common.Install
{
/// <summary>
/// The Installation controller
/// </summary>
[InstallAuthorize]
[Area(Umbraco.Core.Constants.Web.Mvc.InstallArea)]
public class InstallController : Controller
{
private readonly IWebSecurity _webSecurity;
private readonly InstallHelper _installHelper;
private readonly IRuntimeState _runtime;
private readonly IGlobalSettings _globalSettings;
private readonly IHostingEnvironment _hostingEnvironment;
2020-05-05 14:28:01 +02:00
private readonly IUmbracoVersion _umbracoVersion;
private readonly ILogger _logger;
private readonly LinkGenerator _linkGenerator;
private readonly IRuntimeMinifier _runtimeMinifier;
public InstallController(
IWebSecurity webSecurity,
InstallHelper installHelper,
IRuntimeState runtime,
IGlobalSettings globalSettings,
IRuntimeMinifier runtimeMinifier,
2020-05-05 14:28:01 +02:00
IHostingEnvironment hostingEnvironment,
IUmbracoVersion umbracoVersion,
ILogger logger,
LinkGenerator linkGenerator)
2017-07-20 11:21:28 +02:00
{
_webSecurity = webSecurity;
_installHelper = installHelper;
_runtime = runtime;
_globalSettings = globalSettings;
_runtimeMinifier = runtimeMinifier;
_hostingEnvironment = hostingEnvironment;
2020-05-05 14:28:01 +02:00
_umbracoVersion = umbracoVersion;
_logger = logger;
_linkGenerator = linkGenerator;
2017-07-20 11:21:28 +02:00
}
[HttpGet]
2020-04-27 10:09:10 +02:00
[StatusCodeResult(System.Net.HttpStatusCode.ServiceUnavailable)]
[TypeFilter(typeof(StatusCodeResultAttribute), Arguments = new object []{System.Net.HttpStatusCode.ServiceUnavailable})]
public async Task<ActionResult> Index()
{
var umbracoPath = Url.GetBackOfficeUrl();
if (_runtime.Level == RuntimeLevel.Run)
return Redirect(umbracoPath);
2014-02-27 10:16:30 +01:00
if (_runtime.Level == RuntimeLevel.Upgrade)
{
// Update ClientDependency version and delete its temp directories to make sure we get fresh caches
_runtimeMinifier.Reset();
var result = _webSecurity.ValidateCurrentUser(false);
switch (result)
{
case ValidateRequestAttempt.FailedNoPrivileges:
case ValidateRequestAttempt.FailedNoContextId:
2020-04-20 12:20:47 +02:00
return Redirect(_globalSettings.UmbracoPath + "/AuthorizeUpgrade?redir=" + Request.GetEncodedUrl());
}
}
// gen the install base url
ViewData.SetInstallApiBaseUrl(Url.GetInstallerApiUrl());
// get the base umbraco folder
ViewData.SetUmbracoBaseFolder(_hostingEnvironment.ToAbsolute(_globalSettings.UmbracoPath));
2020-05-05 14:28:01 +02:00
ViewData.SetUmbracoVersion(_umbracoVersion.SemanticVersion);
await _installHelper.SetInstallStatusAsync(false, "");
2020-04-20 12:20:47 +02:00
return View();
}
/// <summary>
/// Used to perform the redirect to the installer when the runtime level is <see cref="RuntimeLevel.Install"/> or <see cref="RuntimeLevel.Upgrade"/>
/// </summary>
/// <returns></returns>
[HttpGet]
public ActionResult Redirect()
{
var uri = HttpContext.Request.GetEncodedUrl();
// redirect to install
ReportRuntime(_logger, _runtime.Level, "Umbraco must install or upgrade.");
var installUrl = $"{_linkGenerator.GetInstallerUrl()}?redir=true&url={uri}";
return Redirect(installUrl);
}
private static bool _reported;
private static RuntimeLevel _reportedLevel;
private static void ReportRuntime(ILogger logger, RuntimeLevel level, string message)
{
if (_reported && _reportedLevel == level) return;
_reported = true;
_reportedLevel = level;
logger.Warn(typeof(UmbracoInstallApplicationBuilderExtensions), message);
}
}
}