public static async Task <IActionResult> IsAuthenticated <T1, T2>( Func <HttpRequest, ClaimsPrincipal, ILogger, T1, T2, Task <IActionResult> > func, HttpRequest req, ClaimsPrincipal principal, ILogger log, T1 param1, T2 param2, bool authOverride = false) { try { string email = principal?.FindFirst(ClaimTypes.Email)?.Value; if (email == null) { return(Response.Error <object>($"Not logged in.", statusCode: HttpStatusCode.Unauthorized)); } FancyConn.EnsureShared(); var role = await FancyConn.Shared.GetRole(email); // generate error if not an exec if (!authOverride && role < Role.Executive) { log.LogWarning($"Unauthorized access by {email} to {req.Path}"); return(Response.Error <object>($"Unauthorized.", statusCode: HttpStatusCode.Unauthorized)); } return(await func(req, principal, log, param1, param2)); } catch (Exception e) { log.LogError("Unexpected error.", e); return(Response.Error("Failed to process action.", e)); } finally { FancyConn.Shared?.Dispose(); } }
public static async Task <IActionResult> IsUnauthenticated <T1, T2>( Func <HttpRequest, ILogger, T1, T2, Task <IActionResult> > func, HttpRequest req, ILogger log, T1 param1, T2 param2) { try { FancyConn.EnsureShared(); return(await func(req, log, param1, param2)); } catch (Exception e) { log.LogError("Unexpected error.", e); return(Response.Error("Failed to process action.", e)); } finally { FancyConn.Shared.Dispose(); } }