diff options
author | crupest <crupest@outlook.com> | 2021-04-28 19:42:24 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-04-28 19:42:24 +0800 |
commit | 2075e4098025001073b5eca025632cbb3199c316 (patch) | |
tree | 4259278a7523a52e368c16828450342110c7a25b /BackEnd/Timeline/Controllers/ControllerAuthExtensions.cs | |
parent | d922b3241245c9bc1ca6cff8ac69dd7659a958f1 (diff) | |
download | timeline-2075e4098025001073b5eca025632cbb3199c316.tar.gz timeline-2075e4098025001073b5eca025632cbb3199c316.tar.bz2 timeline-2075e4098025001073b5eca025632cbb3199c316.zip |
refactor: ...
Diffstat (limited to 'BackEnd/Timeline/Controllers/ControllerAuthExtensions.cs')
-rw-r--r-- | BackEnd/Timeline/Controllers/ControllerAuthExtensions.cs | 22 |
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();
}
}
}
|