Files
mixtape/zero.Core/ZeroContext.cs
T

266 lines
6.5 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;
2021-05-20 15:21:37 +02:00
using Raven.Client.Documents.Session;
2021-10-27 16:03:15 +02:00
using System;
using System.Collections.Concurrent;
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;
2021-09-29 15:46:23 +02:00
using zero.Core.Handlers;
2020-11-04 20:45:53 +01:00
using zero.Core.Options;
using zero.Core.Routing;
2021-09-30 10:53:01 +02:00
using zero.Core.Utils;
2020-11-04 20:45:53 +01:00
namespace zero.Core
{
public class ZeroContext : IZeroContext
{
/// <inheritdoc />
2021-05-04 17:23:52 +02:00
public Application 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 bool IsLoggedIntoBackoffice { get; protected set; }
/// <inheritdoc />
public IZeroOptions Options { get; protected set; }
/// <inheritdoc />
2021-09-30 10:53:01 +02:00
public Route Route => ResolvedRoute?.Route;
/// <inheritdoc />
2021-11-06 16:27:04 +01:00
public IRouteModel ResolvedRoute => HttpContextAccessor?.HttpContext?.Features.Get<IRouteModel>();
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
2021-05-20 15:21:37 +02:00
protected ICultureResolver CultureResolver { get; private set; }
2020-11-04 20:45:53 +01:00
protected ILogger<ZeroContext> Logger { get; private set; }
2021-09-29 15:46:23 +02:00
protected IHandlerHolder Handler { get; private set; }
2021-09-30 10:53:01 +02:00
protected IHttpContextAccessor HttpContextAccessor { get; private set; }
protected IPrimitiveTypeCollection ValueCollection { get; private set; }
2021-09-29 15:46:23 +02:00
bool _resolved = false;
2021-09-30 10:53:01 +02:00
public ZeroContext(IZeroOptions options, IHttpContextAccessor httpContextAccessor, IApplicationResolver appResolver, ICultureResolver cultureResolver, ILogger<ZeroContext> logger, IZeroStore store, IHandlerHolder handler)
2020-11-04 20:45:53 +01:00
{
Options = options;
2020-11-16 01:07:07 +01:00
AppResolver = appResolver;
2021-05-20 15:21:37 +02:00
CultureResolver = cultureResolver;
2020-11-04 20:45:53 +01:00
Logger = logger;
2020-11-16 01:07:07 +01:00
Store = store;
2021-09-29 15:46:23 +02:00
Handler = handler;
2021-09-30 10:53:01 +02:00
ValueCollection = new PrimitiveTypeCollection();
HttpContextAccessor = httpContextAccessor;
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();
IsLoggedIntoBackoffice = false;
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;
IsLoggedIntoBackoffice = true;
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
2021-09-30 10:53:01 +02:00
await CultureResolver.Resolve(this);
2021-09-29 15:46:23 +02:00
}
/// <inheritdoc />
public void Override(Application app)
{
Application = app;
2021-10-27 16:03:15 +02:00
AppId = app?.Id;
Store.ResolvedDatabase = app?.Database;
}
/// <inheritdoc />
public ZeroContextScope CreateScope(Application app)
{
2021-10-28 14:03:45 +02:00
return new(this, app, Application);
}
2021-09-29 15:46:23 +02:00
/// <inheritdoc />
2021-09-30 10:53:01 +02:00
public T Get<T>() => ValueCollection.Get<T>();
2021-09-29 15:46:23 +02:00
/// <inheritdoc />
2021-09-30 10:53:01 +02:00
public void Set<T>(T value) => ValueCollection.Set(value);
2021-09-29 15:46:23 +02:00
/// <inheritdoc />
2021-09-30 10:53:01 +02:00
public void Remove<T>() => ValueCollection.Remove<T>();
2020-11-04 20:45:53 +01:00
}
2021-10-27 16:03:15 +02:00
public class ZeroContextScope : IDisposable
{
public IZeroContext Context { get; }
2021-10-28 12:11:07 +02:00
public Application App { get; set; }
public string Database { get; set; }
public IZeroStore Store { get; set; }
2021-10-27 16:03:15 +02:00
Application _originalApp = null;
2021-10-28 12:11:07 +02:00
2021-10-28 14:03:45 +02:00
internal ZeroContextScope(IZeroContext context, Application app, Application originalApp)
2021-10-27 16:03:15 +02:00
{
Context = context;
2021-10-28 12:11:07 +02:00
App = app;
Database = app.Database;
Store = context.Store;
2021-10-28 14:03:45 +02:00
_originalApp = originalApp;
2021-10-27 16:03:15 +02:00
Context.Override(app);
}
/// <inheritdoc />
public void Dispose()
{
Context.Override(_originalApp);
}
}
2020-11-04 20:45:53 +01:00
public interface IZeroContext
{
/// <summary>
/// Currently loaded application
/// </summary>
2021-05-04 17:23:52 +02:00
Application 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>
/// Whether the user is logged into the backoffice
/// </summary>
bool IsLoggedIntoBackoffice { get; }
/// <summary>
/// Global zero options
/// </summary>
IZeroOptions Options { get; }
/// <summary>
/// Document store
/// </summary>
IZeroStore Store { get; }
/// <summary>
/// Matching (frontend) path route
/// </summary>
2021-05-04 17:23:52 +02:00
Route Route { get; }
/// <summary>
/// Matching (frontend) resolved route
/// </summary>
2021-11-06 16:27:04 +01:00
IRouteModel 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);
/// <summary>
/// Overrides the resolved application for this context instance
/// </summary>
void Override(Application app);
2021-10-27 16:03:15 +02:00
/// <summary>
/// SCOPE
/// </summary>
ZeroContextScope CreateScope(Application app);
2021-09-29 15:46:23 +02:00
/// <summary>
/// Get a custom property from this scoped context
/// </summary>
2021-09-30 10:53:01 +02:00
T Get<T>();
2021-09-29 15:46:23 +02:00
/// <summary>
/// Add a custom property to this scoped context
/// </summary>
2021-09-30 10:53:01 +02:00
void Set<T>(T value);
2021-09-29 15:46:23 +02:00
/// <summary>
/// Remove a custom property from this scoped context
/// </summary>
2021-09-30 10:53:01 +02:00
void Remove<T>();
2020-11-04 20:45:53 +01:00
}
}