Refactoring (Controller): Aktualisierung zur Verwendung der aktuellen Version von TryGetUserId
This commit is contained in:
parent
bed5fae01c
commit
ad1fd3163e
@ -27,13 +27,13 @@ public class ProfileController : ControllerBase
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (!this.TryGetUserId(out var userId))
|
if (!User.TryGetUserId(out var userId))
|
||||||
{
|
{
|
||||||
_logger.LogError("Invalid user ID: Retrieved ID is null or not an integer.");
|
_logger.LogError("Invalid user ID: Retrieved ID is null or not an integer.");
|
||||||
return Unauthorized("Failed to retrieve user identity.");
|
return Unauthorized("Failed to retrieve user identity.");
|
||||||
}
|
}
|
||||||
|
|
||||||
var profile = await _mediator.Send(new ReadProfile(UserId: userId ?? throw new InvalidOperationException("User ID is null")));
|
var profile = await _mediator.Send(new ReadProfile(UserId: userId));
|
||||||
return profile is null ? NotFound() : Ok(profile);
|
return profile is null ? NotFound() : Ok(profile);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|||||||
@ -35,15 +35,10 @@ public class ProfileControlsTFController : CRUDControllerBase<IProfileControlsTF
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (!this.TryGetUserId(out int? id))
|
if (!User.TryGetUserId(out var id))
|
||||||
{
|
{
|
||||||
logger.LogError("Authorization failed: User ID claim not found.");
|
logger.LogError("Authorization failed: User ID claim not found.");
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError, "Failed to retrieve user identity.");
|
return Unauthorized("Failed to retrieve user identity.");
|
||||||
}
|
|
||||||
else if (id is null)
|
|
||||||
{
|
|
||||||
logger.LogError("Invalid user ID: Retrieved ID is null or not an integer.");
|
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError, "Invalid user ID.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return await _service.ReadAsync(
|
return await _service.ReadAsync(
|
||||||
@ -71,16 +66,11 @@ public class ProfileControlsTFController : CRUDControllerBase<IProfileControlsTF
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (!this.TryGetUserId(out int? id))
|
if (!User.TryGetUserId(out var id))
|
||||||
{
|
{
|
||||||
logger.LogError("Authorization failed: User ID claim not found.");
|
logger.LogError("Authorization failed: User ID claim not found.");
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError, "Failed to retrieve user identity.");
|
return StatusCode(StatusCodes.Status500InternalServerError, "Failed to retrieve user identity.");
|
||||||
}
|
}
|
||||||
else if (id is null)
|
|
||||||
{
|
|
||||||
logger.LogError("Invalid user ID: Retrieved ID is null or not an integer.");
|
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError, "Invalid user ID.");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (createDto.UserId != id)
|
if (createDto.UserId != id)
|
||||||
return Unauthorized();
|
return Unauthorized();
|
||||||
@ -100,16 +90,11 @@ public class ProfileControlsTFController : CRUDControllerBase<IProfileControlsTF
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (!this.TryGetUserId(out int? userId))
|
if (!User.TryGetUserId(out var userId))
|
||||||
{
|
{
|
||||||
logger.LogError("Authorization failed: User ID claim not found.");
|
logger.LogError("Authorization failed: User ID claim not found.");
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError, "Failed to retrieve user identity.");
|
return StatusCode(StatusCodes.Status500InternalServerError, "Failed to retrieve user identity.");
|
||||||
}
|
}
|
||||||
else if (userId is null)
|
|
||||||
{
|
|
||||||
logger.LogError("Invalid user ID: Retrieved ID is null or not an integer.");
|
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError, "Invalid user ID.");
|
|
||||||
}
|
|
||||||
|
|
||||||
return await _service.ReadByIdAsync(id).ThenAsync(
|
return await _service.ReadByIdAsync(id).ThenAsync(
|
||||||
SuccessAsync: async pctf => pctf.UserId == userId ? await base.Delete(id) : Unauthorized(),
|
SuccessAsync: async pctf => pctf.UserId == userId ? await base.Delete(id) : Unauthorized(),
|
||||||
|
|||||||
@ -35,15 +35,10 @@ namespace WorkFlow.API.Controllers
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (!this.TryGetUserId(out int? id))
|
if (!User.TryGetUserId(out var id))
|
||||||
{
|
{
|
||||||
logger.LogError("Authorization failed: User ID claim not found.");
|
logger.LogError("Authorization failed: User ID claim not found.");
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError, "Failed to retrieve user identity.");
|
return Unauthorized("Failed to retrieve user identity.");
|
||||||
}
|
|
||||||
else if (id is null)
|
|
||||||
{
|
|
||||||
logger.LogError("Invalid user ID: Retrieved ID is null or not an integer.");
|
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError, "Invalid user ID.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return await _service.ReadAsync(
|
return await _service.ReadAsync(
|
||||||
@ -70,16 +65,11 @@ namespace WorkFlow.API.Controllers
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (!this.TryGetUserId(out int? id))
|
if (!User.TryGetUserId(out var id))
|
||||||
{
|
{
|
||||||
logger.LogError("Authorization failed: User ID claim not found.");
|
logger.LogError("Authorization failed: User ID claim not found.");
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError, "Failed to retrieve user identity.");
|
return StatusCode(StatusCodes.Status500InternalServerError, "Failed to retrieve user identity.");
|
||||||
}
|
}
|
||||||
else if (id is null)
|
|
||||||
{
|
|
||||||
logger.LogError("Invalid user ID: Retrieved ID is null or not an integer.");
|
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError, "Invalid user ID.");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (createDto.UserId != id)
|
if (createDto.UserId != id)
|
||||||
return Unauthorized();
|
return Unauthorized();
|
||||||
@ -98,16 +88,11 @@ namespace WorkFlow.API.Controllers
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (!this.TryGetUserId(out int? userId))
|
if (!User.TryGetUserId(out var userId))
|
||||||
{
|
{
|
||||||
logger.LogError("Authorization failed: User ID claim not found.");
|
logger.LogError("Authorization failed: User ID claim not found.");
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError, "Failed to retrieve user identity.");
|
return StatusCode(StatusCodes.Status500InternalServerError, "Failed to retrieve user identity.");
|
||||||
}
|
}
|
||||||
else if (userId is null)
|
|
||||||
{
|
|
||||||
logger.LogError("Invalid user ID: Retrieved ID is null or not an integer.");
|
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError, "Invalid user ID.");
|
|
||||||
}
|
|
||||||
|
|
||||||
return await _service.ReadByIdAsync(id).ThenAsync(
|
return await _service.ReadByIdAsync(id).ThenAsync(
|
||||||
SuccessAsync: async pctf => pctf.UserId == userId ? await base.Delete(id) : Unauthorized(),
|
SuccessAsync: async pctf => pctf.UserId == userId ? await base.Delete(id) : Unauthorized(),
|
||||||
|
|||||||
@ -26,24 +26,19 @@ public class UserController : ControllerBase
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (!this.TryGetUserId(out int? id))
|
if (!User.TryGetUserId(out var id))
|
||||||
{
|
{
|
||||||
logger.LogError("Authorization failed: User ID claim not found.");
|
logger.LogError("Authorization failed: User ID claim not found.");
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError, "Failed to retrieve user identity.");
|
return Unauthorized("Failed to retrieve user identity.");
|
||||||
}
|
}
|
||||||
else if(id is int id_int)
|
|
||||||
return await userService.ReadByIdAsync(id_int).ThenAsync(
|
return await userService.ReadByIdAsync(id).ThenAsync(
|
||||||
Success: Ok,
|
Success: Ok,
|
||||||
Fail: IActionResult (msg, ntc) =>
|
Fail: IActionResult (msg, ntc) =>
|
||||||
{
|
{
|
||||||
logger.LogNotice(ntc);
|
logger.LogNotice(ntc);
|
||||||
return NotFound();
|
return NotFound();
|
||||||
});
|
});
|
||||||
else
|
|
||||||
{
|
|
||||||
logger.LogError("Invalid user ID: Retrieved ID is null or not an integer.");
|
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError, "Invalid user ID.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user