2020-04-20 12:20:47 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Filters;
|
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2020-09-21 09:52:58 +02:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2020-04-20 12:20:47 +02:00
|
|
|
|
using Umbraco.Core;
|
|
|
|
|
|
|
2020-05-14 14:47:59 +10:00
|
|
|
|
namespace Umbraco.Web.Common.Install
|
2020-04-20 12:20:47 +02:00
|
|
|
|
{
|
2020-05-13 14:49:00 +10:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Ensures authorization occurs for the installer if it has already completed.
|
|
|
|
|
|
/// If install has not yet occurred then the authorization is successful.
|
|
|
|
|
|
/// </summary>
|
2020-04-20 12:20:47 +02:00
|
|
|
|
public class InstallAuthorizeAttribute : TypeFilterAttribute
|
|
|
|
|
|
{
|
2020-11-20 15:40:20 +11:00
|
|
|
|
// NOTE: This doesn't need to be an authz policy, it's only used for the installer
|
|
|
|
|
|
|
2020-05-14 21:20:06 +10:00
|
|
|
|
public InstallAuthorizeAttribute() : base(typeof(InstallAuthorizeFilter))
|
2020-04-20 12:20:47 +02:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-05-14 21:20:06 +10:00
|
|
|
|
private class InstallAuthorizeFilter : IAuthorizationFilter
|
2020-04-20 12:20:47 +02:00
|
|
|
|
{
|
2020-05-13 14:49:00 +10:00
|
|
|
|
public void OnAuthorization(AuthorizationFilterContext authorizationFilterContext)
|
2020-04-20 12:20:47 +02:00
|
|
|
|
{
|
2020-05-13 14:49:00 +10:00
|
|
|
|
var serviceProvider = authorizationFilterContext.HttpContext.RequestServices;
|
|
|
|
|
|
var runtimeState = serviceProvider.GetService<IRuntimeState>();
|
|
|
|
|
|
var umbracoContext = serviceProvider.GetService<IUmbracoContext>();
|
2020-09-21 09:52:58 +02:00
|
|
|
|
var logger = serviceProvider.GetService<ILogger<InstallAuthorizeFilter>>();
|
2020-04-20 12:20:47 +02:00
|
|
|
|
|
2020-05-13 14:49:00 +10:00
|
|
|
|
if (!IsAllowed(runtimeState, umbracoContext, logger))
|
2020-04-20 12:20:47 +02:00
|
|
|
|
{
|
2020-05-13 14:49:00 +10:00
|
|
|
|
authorizationFilterContext.Result = new ForbidResult();
|
2020-04-20 12:20:47 +02:00
|
|
|
|
}
|
2020-05-13 14:49:00 +10:00
|
|
|
|
|
2020-04-20 12:20:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-21 09:52:58 +02:00
|
|
|
|
private static bool IsAllowed(IRuntimeState runtimeState, IUmbracoContext umbracoContext, ILogger<InstallAuthorizeFilter> logger)
|
2020-04-20 12:20:47 +02:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
// if not configured (install or upgrade) then we can continue
|
|
|
|
|
|
// otherwise we need to ensure that a user is logged in
|
|
|
|
|
|
return runtimeState.Level == RuntimeLevel.Install
|
|
|
|
|
|
|| runtimeState.Level == RuntimeLevel.Upgrade
|
2020-05-13 14:49:00 +10:00
|
|
|
|
|| (umbracoContext?.Security?.ValidateCurrentUser() ?? false);
|
2020-04-20 12:20:47 +02:00
|
|
|
|
}
|
2020-05-13 14:49:00 +10:00
|
|
|
|
catch (Exception ex)
|
2020-04-20 12:20:47 +02:00
|
|
|
|
{
|
2020-09-16 09:40:49 +02:00
|
|
|
|
logger.LogError(ex, "An error occurred determining authorization");
|
2020-04-20 12:20:47 +02:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-05-13 14:49:00 +10:00
|
|
|
|
|
2020-04-20 12:20:47 +02:00
|
|
|
|
}
|