aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline/Controllers/ControllerAuthExtensions.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-04-28 19:42:24 +0800
committercrupest <crupest@outlook.com>2021-04-28 19:42:24 +0800
commitfb3e62c89daa4ea497d544355a46a599ad29df25 (patch)
tree05f86c20fdcc7f1df248023651386077a8fb2b58 /BackEnd/Timeline/Controllers/ControllerAuthExtensions.cs
parente16c958e5ba47834dc1624e09ed8e5074a60d1c6 (diff)
downloadtimeline-fb3e62c89daa4ea497d544355a46a599ad29df25.tar.gz
timeline-fb3e62c89daa4ea497d544355a46a599ad29df25.tar.bz2
timeline-fb3e62c89daa4ea497d544355a46a599ad29df25.zip
refactor: ...
Diffstat (limited to 'BackEnd/Timeline/Controllers/ControllerAuthExtensions.cs')
-rw-r--r--BackEnd/Timeline/Controllers/ControllerAuthExtensions.cs22
1 files changed, 3 insertions, 19 deletions
diff --git a/BackEnd/Timeline/Controllers/ControllerAuthExtensions.cs b/BackEnd/Timeline/Controllers/ControllerAuthExtensions.cs
index 9fcb727a..cd2bdadf 100644
--- a/BackEnd/Timeline/Controllers/ControllerAuthExtensions.cs
+++ b/BackEnd/Timeline/Controllers/ControllerAuthExtensions.cs
@@ -1,9 +1,7 @@
using Microsoft.AspNetCore.Mvc;
using System;
-using System.Security.Claims;
using Timeline.Auth;
using Timeline.Services.User;
-using static Timeline.Resources.Controllers.ControllerAuthExtensions;
namespace Timeline.Controllers
{
@@ -11,31 +9,17 @@ namespace Timeline.Controllers
{
public static bool UserHasPermission(this ControllerBase controller, UserPermission permission)
{
- return controller.User != null && controller.User.HasPermission(permission);
+ return controller.User.HasPermission(permission);
}
public static long GetUserId(this ControllerBase controller)
{
- var claim = controller.User.FindFirst(ClaimTypes.NameIdentifier);
- if (claim == null)
- throw new InvalidOperationException(ExceptionNoUserIdentifierClaim);
-
- if (long.TryParse(claim.Value, out var value))
- return value;
-
- throw new InvalidOperationException(ExceptionUserIdentifierClaimBadFormat);
+ return controller.GetOptionalUserId() ?? throw new InvalidOperationException(Resource.ExceptionNoUserId);
}
public static long? GetOptionalUserId(this ControllerBase controller)
{
- var claim = controller.User.FindFirst(ClaimTypes.NameIdentifier);
- if (claim == null)
- return null;
-
- if (long.TryParse(claim.Value, out var value))
- return value;
-
- throw new InvalidOperationException(ExceptionUserIdentifierClaimBadFormat);
+ return controller.User.GetUserId();
}
}
}