From bed5fae01c3a4d8056892437099e26607ba94d34 Mon Sep 17 00:00:00 2001 From: TekH Date: Thu, 24 Jul 2025 18:09:59 +0200 Subject: [PATCH] =?UTF-8?q?Refactoring:=20Vereinfachung=20der=20TryGetUser?= =?UTF-8?q?Id-Methode=20und=20Umstellung=20des=20Erweiterungsziels=20auf?= =?UTF-8?q?=20ClaimsPrincipal=20-=20TryGetUserId=20wurde=20so=20ge=C3=A4nd?= =?UTF-8?q?ert,=20dass=20es=20direkt=20die=20Methode=20expression-bodied?= =?UTF-8?q?=20mit=20int.TryParse=20verwendet.=20-=20Die=20Erweiterungsmeth?= =?UTF-8?q?oden=20wurden=20aktualisiert,=20sodass=20sie=20nun=20statt=20au?= =?UTF-8?q?f=20ControllerBase=20auf=20ClaimsPrincipal=20abzielen,=20um=20e?= =?UTF-8?q?ine=20bessere=20Abstraktion=20und=20Wiederverwendbarkeit=20zu?= =?UTF-8?q?=20erreichen.=20-=20Andere=20Methoden=20wurden=20aus=20Gr=C3=BC?= =?UTF-8?q?nden=20der=20Konsistenz=20unver=C3=A4ndert=20gelassen.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/ControllerExtensions.cs | 38 +++++-------------- .../Controllers/ProfileController.cs | 1 - 2 files changed, 9 insertions(+), 30 deletions(-) diff --git a/src/WorkFlow.API/Controllers/ControllerExtensions.cs b/src/WorkFlow.API/Controllers/ControllerExtensions.cs index ddabb7e..480e924 100644 --- a/src/WorkFlow.API/Controllers/ControllerExtensions.cs +++ b/src/WorkFlow.API/Controllers/ControllerExtensions.cs @@ -7,31 +7,11 @@ namespace WorkFlow.API.Controllers [APIKeyAuth] public static class ControllerExtensions { - public static bool TryGetUserId(this ControllerBase controller, out int? id) - { - var value = controller.User.FindFirstValue(ClaimTypes.NameIdentifier); - - if (value is null) - { - id = default; - return false; - } - - if(int.TryParse(value, out int id_int)) - { - id = id_int; - return true; - } - else - { - id = null; - return false; - } - } + public static bool TryGetUserId(this ClaimsPrincipal user, out int id) => int.TryParse(user.FindFirstValue(ClaimTypes.NameIdentifier), out id); - public static bool TryGetUsername(this ControllerBase controller, out string username) + public static bool TryGetUsername(this ClaimsPrincipal user, out string username) { - var value = controller.User.FindFirstValue(ClaimTypes.Name); + var value = user.FindFirstValue(ClaimTypes.Name); if (value is null) { @@ -45,9 +25,9 @@ namespace WorkFlow.API.Controllers } } - public static bool TryGetName(this ControllerBase controller, out string name) + public static bool TryGetName(this ClaimsPrincipal user, out string name) { - var value = controller.User.FindFirstValue(ClaimTypes.Surname); + var value = user.FindFirstValue(ClaimTypes.Surname); if (value is null) { @@ -61,9 +41,9 @@ namespace WorkFlow.API.Controllers } } - public static bool TryGetPrename(this ControllerBase controller, out string prename) + public static bool TryGetPrename(this ClaimsPrincipal user, out string prename) { - var value = controller.User.FindFirstValue(ClaimTypes.GivenName); + var value = user.FindFirstValue(ClaimTypes.GivenName); if (value is null) { @@ -77,9 +57,9 @@ namespace WorkFlow.API.Controllers } } - public static bool TryGetEmail(this ControllerBase controller, out string email) + public static bool TryGetEmail(this ClaimsPrincipal user, out string email) { - var value = controller.User.FindFirstValue(ClaimTypes.Email); + var value = user.FindFirstValue(ClaimTypes.Email); if (value is null) { diff --git a/src/WorkFlow.API/Controllers/ProfileController.cs b/src/WorkFlow.API/Controllers/ProfileController.cs index 4f2fdbd..c2dc053 100644 --- a/src/WorkFlow.API/Controllers/ProfileController.cs +++ b/src/WorkFlow.API/Controllers/ProfileController.cs @@ -1,7 +1,6 @@ using MediatR; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; -using System.Threading.Tasks; using WorkFlow.API.Attributes; using WorkFlow.Application.Profiles;