66 lines
2.8 KiB
C#

using EnvelopeGenerator.Web.Models;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
namespace EnvelopeGenerator.Web.Controllers
{
public static class ControllerBaseExtensions
{
public static (string EnvelopeUuid, string ReceiverSignature)? GetAuthenticatedEnvelopeDetails(this ControllerBase controller)
{
if(controller?.User?.Identity?.IsAuthenticated ?? false)
{
var envelopeUuid = controller.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
var receiverSignature = controller.User.FindFirst(ClaimTypes.Hash)?.Value;
if (!string.IsNullOrEmpty(envelopeUuid) && !string.IsNullOrEmpty(receiverSignature))
return (EnvelopeUuid: envelopeUuid, ReceiverSignature: receiverSignature);
}
return null;
}
public static string? GetAuthenticatedEnvelopeUuid(this ControllerBase controller)
{
if (controller?.User?.Identity?.IsAuthenticated ?? false)
{
var envelopeUuid = controller.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
if (!string.IsNullOrEmpty(envelopeUuid))
return envelopeUuid;
}
return null;
}
public static string? GetAuthenticatedReceiverSignature(this ControllerBase controller)
{
if (controller?.User?.Identity?.IsAuthenticated ?? false)
{
var receiverSignature = controller.User.FindFirst(ClaimTypes.Hash)?.Value;
if (!string.IsNullOrEmpty(receiverSignature))
return receiverSignature;
}
return null;
}
public static ViewResult ViewError(this Controller controller, ErrorViewModel errorViewModel) => controller.View("_Error", errorViewModel);
public static ViewResult ViewError404(this Controller controller) => controller.ViewError(new ErrorViewModel()
{
Title = "404",
Subtitle = "Die von Ihnen gesuchte Seite ist nicht verfügbar",
Body = "Sie können derzeit nur an Sie gerichtete Briefe einsehen und unterschreiben.",
});
public static ViewResult ViewEnvelopeNotFound(this Controller controller) => controller.ViewError(new ErrorViewModel()
{
Title = "404",
Subtitle = "Umschlag nicht gefunden",
Body = "Wenn Sie diese URL in Ihrer E-Mail erhalten haben, wenden Sie sich bitte an das IT-Team."
});
public static ViewResult ViewInnerServiceError(this Controller controller) => controller.ViewError(new ErrorViewModel()
{
Title = "500",
Subtitle = "Ein unerwarteter Fehler ist aufgetreten",
Body = "Bitte kontaktieren Sie das IT-Team."
});
}
}