Skip to content

Commit

Permalink
FIX - when you run with no user logged in on v10 you get and exception
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinJump committed Jun 18, 2022
1 parent 2392715 commit d974e9b
Showing 1 changed file with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,23 @@ public async Task InvokeAsync(HttpContext context, IBackofficeUserAccessor backo
{
_logger.LogDebug("Maintenance mode middleware triggered {url}", context.Request.Path);

if (_runtimeState.Level == RuntimeLevel.Run &&
_maintenanceModeService.IsInMaintenanceMode)
bool IsAuthenticated = IsBackOfficeAuthenticated(backofficeUserAccessor);

if (backofficeUserAccessor != null)
{
// todo figure out how to do this check here
if (!_maintenanceModeService.Settings.AllowBackOfficeUsersThrough
&& backofficeUserAccessor.BackofficeUser.IsAuthenticated)
{
context = HandleRequest(context);
}
else if (!backofficeUserAccessor.BackofficeUser.IsAuthenticated)
if (_runtimeState.Level == RuntimeLevel.Run &&
_maintenanceModeService.IsInMaintenanceMode)
{
context = HandleRequest(context);
// todo figure out how to do this check here
if (!_maintenanceModeService.Settings.AllowBackOfficeUsersThrough
&& IsAuthenticated)
{
context = HandleRequest(context);
}
else if (!IsAuthenticated)
{
context = HandleRequest(context);
}
}
}

Expand All @@ -53,6 +58,17 @@ public async Task InvokeAsync(HttpContext context, IBackofficeUserAccessor backo

}

private bool IsBackOfficeAuthenticated(IBackofficeUserAccessor backofficeUserAccessor) {
try {
return backofficeUserAccessor.BackofficeUser?.IsAuthenticated ?? false;
}
catch(System.Exception ex) {
// in v10 - this thows an erro internally, if there is no backoffice user 🤷‍♀️
// _logger.LogWarning(ex, "Error getting back office user");
return false;
}
}

private static HttpContext HandleRequest(HttpContext context)
{
var newPath = new PathString($"/{MaintenanceMode.MaintenanceRoot}");
Expand Down

0 comments on commit d974e9b

Please sign in to comment.