Files
mixtape/zero.Core/ZeroContext.cs
T

164 lines
4.2 KiB
C#
Raw Normal View History

2020-11-04 20:45:53 +01:00
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
2020-12-15 16:09:58 +01:00
using Raven.Client.Documents.Session;
2020-11-04 20:45:53 +01:00
using System.Security.Claims;
using System.Threading.Tasks;
2020-12-15 16:09:58 +01:00
using zero.Core.Cultures;
2020-11-16 01:07:07 +01:00
using zero.Core.Database;
2020-11-04 20:45:53 +01:00
using zero.Core.Entities;
2020-11-16 01:07:07 +01:00
using zero.Core.Extensions;
2020-11-04 20:45:53 +01:00
using zero.Core.Options;
using zero.Core.Routing;
2020-11-04 20:45:53 +01:00
namespace zero.Core
{
public class ZeroContext : IZeroContext
{
/// <inheritdoc />
2020-11-16 01:07:07 +01:00
public IApplication Application { get; protected set; }
2020-11-04 20:45:53 +01:00
/// <inheritdoc />
public string AppId { get; protected set; }
/// <inheritdoc />
public ClaimsPrincipal BackofficeUser { get; protected set; }
2020-11-04 20:45:53 +01:00
2020-11-05 00:46:18 +01:00
/// <inheritdoc />
public bool IsBackofficeRequest { get; protected set; }
/// <inheritdoc />
public IZeroOptions Options { get; protected set; }
/// <inheritdoc />
public IRoute Route { get; private set; }
/// <inheritdoc />
public IResolvedRoute ResolvedRoute { get; private set; }
2020-11-04 20:45:53 +01:00
/// <inheritdoc />
public IZeroStore Store { get; private set; }
2020-11-04 20:45:53 +01:00
2020-11-16 01:07:07 +01:00
protected IApplicationResolver AppResolver { get; private set; }
2020-11-04 20:45:53 +01:00
2020-12-15 16:09:58 +01:00
protected ICultureResolver CultureResolver { get; private set; }
2020-11-04 20:45:53 +01:00
protected ILogger<ZeroContext> Logger { get; private set; }
private bool _resolved = false;
2020-12-15 16:09:58 +01:00
public ZeroContext(IZeroOptions options, IApplicationResolver appResolver, ICultureResolver cultureResolver, ILogger<ZeroContext> logger, IZeroStore store)
2020-11-04 20:45:53 +01:00
{
Options = options;
2020-11-16 01:07:07 +01:00
AppResolver = appResolver;
2020-12-15 16:09:58 +01:00
CultureResolver = cultureResolver;
2020-11-04 20:45:53 +01:00
Logger = logger;
2020-11-16 01:07:07 +01:00
Store = store;
2020-11-04 20:45:53 +01:00
}
/// <inheritdoc />
2020-11-17 11:27:31 +01:00
public async virtual Task Resolve(HttpContext context)
2020-11-04 20:45:53 +01:00
{
2020-11-16 01:07:07 +01:00
if (_resolved)
2020-11-04 20:45:53 +01:00
{
return;
}
2020-11-16 01:07:07 +01:00
if (context?.Request is null)
{
Store.ResolvedDatabase = null;
return;
}
2020-12-02 15:45:44 +01:00
if (!Options.SetupCompleted)
{
return;
}
_resolved = true;
2020-11-16 01:07:07 +01:00
// check if the current request is a backoffice request
IsBackofficeRequest = context.IsBackofficeRequest(Options.BackofficePath);
// get the currently logged-in backoffice user
BackofficeUser = new ClaimsPrincipal();
2020-11-04 20:45:53 +01:00
AuthenticateResult authResult = await context.AuthenticateAsync(Constants.Auth.BackofficeScheme);
if (authResult?.Principal is not null)
2020-11-04 20:45:53 +01:00
{
BackofficeUser = authResult.Principal;
2020-11-04 20:45:53 +01:00
}
2020-11-04 23:38:06 +01:00
2020-11-16 01:07:07 +01:00
// resolve current application
2020-11-17 11:27:31 +01:00
Application = await AppResolver.Resolve(context, BackofficeUser);
2020-11-16 01:07:07 +01:00
AppId = Application.Id;
2020-11-05 00:46:18 +01:00
2020-11-16 01:07:07 +01:00
// set default database for document store
Store.ResolvedDatabase = Application.Database;
2020-12-15 16:09:58 +01:00
// set current culture
await CultureResolver.Resolve(this);
// resolve request route
2020-11-17 11:27:29 +01:00
if (IsBackofficeRequest is false && context.Request.RouteValues.TryGetValue("zero.route", out object route))
{
ResolvedRoute = (IResolvedRoute)route;
Route = ResolvedRoute.Route;
}
2020-11-04 20:45:53 +01:00
}
}
public interface IZeroContext
{
/// <summary>
/// Currently loaded application
/// </summary>
2020-11-16 01:07:07 +01:00
IApplication Application { get; }
2020-11-04 20:45:53 +01:00
/// <summary>
/// Current loaded application Id
/// </summary>
string AppId { get; }
/// <summary>
/// Resolved backoffice user principal
/// </summary>
ClaimsPrincipal BackofficeUser { get; }
2020-11-04 20:45:53 +01:00
2020-11-05 00:46:18 +01:00
/// <summary>
/// Whether the current request is a backoffice request or not
/// </summary>
bool IsBackofficeRequest { get; }
/// <summary>
/// Global zero options
/// </summary>
IZeroOptions Options { get; }
/// <summary>
/// Document store
/// </summary>
IZeroStore Store { get; }
/// <summary>
/// Matching (frontend) path route
/// </summary>
IRoute Route { get; }
/// <summary>
/// Matching (frontend) resolved route
/// </summary>
IResolvedRoute ResolvedRoute { get; }
2020-11-04 20:45:53 +01:00
/// <summary>
/// Resolves the current application (for backoffice + frontend requests) and
/// the currently active backoffice user, as users are not signed in with the default scheme and do therefore not populate HttpContext.User
/// </summary>
2020-11-17 11:27:31 +01:00
Task Resolve(HttpContext context);
2020-11-04 20:45:53 +01:00
}
}