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;
|
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;
|
2020-11-05 16:28:59 +01:00
|
|
|
using zero.Core.Routing;
|
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 />
|
2020-11-05 16:28:59 +01:00
|
|
|
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; }
|
|
|
|
|
|
2020-11-05 16:28:59 +01:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public IZeroOptions Options { get; protected set; }
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2021-05-04 17:23:52 +02:00
|
|
|
public Route Route { get; private set; }
|
2020-11-05 16:28:59 +01:00
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public IResolvedRoute ResolvedRoute { get; private set; }
|
2020-11-04 20:45:53 +01:00
|
|
|
|
2020-11-18 01:26:12 +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; }
|
|
|
|
|
|
|
|
|
|
|
2020-11-05 16:28:59 +01:00
|
|
|
private bool _resolved = false;
|
|
|
|
|
|
|
|
|
|
|
2021-05-20 15:21:37 +02: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;
|
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;
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-05 16:28:59 +01:00
|
|
|
_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);
|
2020-11-05 16:28:59 +01:00
|
|
|
if (authResult?.Principal is not null)
|
2020-11-04 20:45:53 +01:00
|
|
|
{
|
2020-11-05 16:28:59 +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-11-05 16:28:59 +01:00
|
|
|
|
2020-12-15 16:09:58 +01:00
|
|
|
// set current culture
|
2021-05-20 15:21:37 +02:00
|
|
|
await CultureResolver.Resolve(this);
|
2020-12-15 16:09:58 +01:00
|
|
|
|
|
|
|
|
// 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>
|
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>
|
2020-11-05 16:28:59 +01:00
|
|
|
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; }
|
|
|
|
|
|
2020-11-05 16:28:59 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Global zero options
|
|
|
|
|
/// </summary>
|
|
|
|
|
IZeroOptions Options { get; }
|
|
|
|
|
|
2020-11-18 01:26:12 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Document store
|
|
|
|
|
/// </summary>
|
|
|
|
|
IZeroStore Store { get; }
|
|
|
|
|
|
2020-11-05 16:28:59 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Matching (frontend) path route
|
|
|
|
|
/// </summary>
|
2021-05-04 17:23:52 +02:00
|
|
|
Route Route { get; }
|
2020-11-05 16:28:59 +01:00
|
|
|
|
|
|
|
|
/// <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
|
|
|
}
|
|
|
|
|
}
|