Skip to content

Commit

Permalink
Adding an IContentSecurityPolicyProvider to allow tenant admin login
Browse files Browse the repository at this point in the history
  • Loading branch information
Piedone committed Jul 22, 2024
1 parent 77ee4f0 commit bc0f780
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
16 changes: 7 additions & 9 deletions Lombiq.Hosting.Tenants.Admin.Login/Filters/TenantsIndexFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,11 @@ public TenantsIndexFilter(

public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
{
var actionRouteController = context.ActionDescriptor.RouteValues["Controller"];
var actionRouteArea = context.ActionDescriptor.RouteValues["Area"];
var actionRouteValue = context.ActionDescriptor.RouteValues["Action"];

if (actionRouteController == typeof(AdminController).ControllerName() &&
actionRouteArea == $"{nameof(OrchardCore)}.{nameof(OrchardCore.Tenants)}" &&
actionRouteValue is nameof(AdminController.Edit) &&
if (IsTenantsEditAction(context) &&
context.Result is ViewResult &&
await _authorizationService.AuthorizeAsync(
_hca.HttpContext.User,
TenantAdminPermissions.LoginAsAdmin)
)
TenantAdminPermissions.LoginAsAdmin))
{
var shellSettings = _shellHost.GetSettings(context.RouteData.Values["Id"].ToString());
if (shellSettings != null &&
Expand All @@ -70,4 +63,9 @@ await contentZone.AddAsync(

await next();
}

public static bool IsTenantsEditAction(ActionContext context) =>
context.ActionDescriptor.RouteValues["Controller"] == typeof(AdminController).ControllerName() &&
context.ActionDescriptor.RouteValues["Area"] == $"{nameof(OrchardCore)}.{nameof(OrchardCore.Tenants)}" &&
context.ActionDescriptor.RouteValues["Action"] is nameof(AdminController.Edit);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Lombiq.HelpfulLibraries.AspNetCore.Security;
using Lombiq.Hosting.Tenants.Admin.Login.Filters;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using OrchardCore.Environment.Shell;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Lombiq.Hosting.Tenants.Admin.Login.Services;

internal sealed class TenantLoginSecurityPolicyProvider : IContentSecurityPolicyProvider
{
private readonly IActionContextAccessor _actionContextAccessor;
private readonly IShellHost _shellHost;

public TenantLoginSecurityPolicyProvider(IActionContextAccessor actionContextAccessor, IShellHost shellHost)
{
_actionContextAccessor = actionContextAccessor;
_shellHost = shellHost;
}

public ValueTask UpdateAsync(IDictionary<string, string> securityPolicies, HttpContext context)
{
var actionContext = _actionContextAccessor.ActionContext;

if (!TenantsIndexFilter.IsTenantsEditAction(actionContext))
{
return ValueTask.CompletedTask;
}

var shellName = actionContext.RouteData.Values["Id"].ToString();

if (_shellHost.TryGetSettings(shellName, out var shellSettings))
{
CspHelper.MergeValues(securityPolicies, ContentSecurityPolicyDirectives.FormAction, shellSettings.RequestUrlHosts);
}

return ValueTask.CompletedTask;
}
}
1 change: 1 addition & 0 deletions Lombiq.Hosting.Tenants.Admin.Login/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ public override void ConfigureServices(IServiceCollection services)
services.Configure<MvcOptions>(options => options.Filters.Add(typeof(TenantsIndexFilter)));
services.AddScoped<IPermissionProvider, TenantAdminPermissions>();
services.AddSingleton<ITenantLoginPasswordValidator, TenantLoginKeyValidator>();
services.AddContentSecurityPolicyProvider<TenantLoginSecurityPolicyProvider>();
}
}

0 comments on commit bc0f780

Please sign in to comment.