refactor(api): Aktualisierung von ControllerExtensions zur Nutzung von ClaimsPrincipal anstelle von ControllerBase

- Refactoring der Erweiterungsmethoden, sodass sie auf `ClaimsPrincipal` anstatt auf `ControllerBase` basieren.
- Alle Verweise und Verwendungen dieser Erweiterungsmethoden im gesamten Code aktualisiert.
- Dies verbessert die Flexibilität und entkoppelt das Abrufen von Benutzeransprüchen von den Controllern.
This commit is contained in:
Developer 02
2024-09-05 10:24:00 +02:00
parent e3fbf4fc77
commit 5b8d8b9e55
3 changed files with 13 additions and 13 deletions

View File

@@ -5,20 +5,20 @@ namespace EnvelopeGenerator.GeneratorAPI.Controllers
{
public static class ControllerExtensions
{
public static int? GetId(this ControllerBase controller)
=> int.TryParse(controller.User.FindFirst(ClaimTypes.NameIdentifier)?.Value, out int result)
public static int? GetId(this ClaimsPrincipal user)
=> int.TryParse(user.FindFirst(ClaimTypes.NameIdentifier)?.Value, out int result)
? result : null;
public static string? GetUsername(this ControllerBase controller)
=> controller.User.FindFirst(ClaimTypes.Name)?.Value;
public static string? GetUsername(this ClaimsPrincipal user)
=> user.FindFirst(ClaimTypes.Name)?.Value;
public static string? GetName(this ControllerBase controller)
=> controller.User.FindFirst(ClaimTypes.Surname)?.Value;
public static string? GetName(this ClaimsPrincipal user)
=> user.FindFirst(ClaimTypes.Surname)?.Value;
public static string? GetPrename(this ControllerBase controller)
=> controller.User.FindFirst(ClaimTypes.GivenName)?.Value;
public static string? GetPrename(this ClaimsPrincipal user)
=> user.FindFirst(ClaimTypes.GivenName)?.Value;
public static string? GetEmail(this ControllerBase controller)
=> controller.User.FindFirst(ClaimTypes.Email)?.Value;
public static string? GetEmail(this ClaimsPrincipal user)
=> user.FindFirst(ClaimTypes.Email)?.Value;
}
}