From fb3e62c89daa4ea497d544355a46a599ad29df25 Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 28 Apr 2021 19:42:24 +0800 Subject: refactor: ... --- .../Controllers/ControllerAuthExtensions.cs | 22 +--- BackEnd/Timeline/Controllers/Resource.Designer.cs | 72 ++++++++++++ BackEnd/Timeline/Controllers/Resource.resx | 123 +++++++++++++++++++++ 3 files changed, 198 insertions(+), 19 deletions(-) create mode 100644 BackEnd/Timeline/Controllers/Resource.Designer.cs create mode 100644 BackEnd/Timeline/Controllers/Resource.resx (limited to 'BackEnd/Timeline/Controllers') 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(); } } } diff --git a/BackEnd/Timeline/Controllers/Resource.Designer.cs b/BackEnd/Timeline/Controllers/Resource.Designer.cs new file mode 100644 index 00000000..6279a055 --- /dev/null +++ b/BackEnd/Timeline/Controllers/Resource.Designer.cs @@ -0,0 +1,72 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Timeline.Controllers { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resource { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resource() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Timeline.Controllers.Resource", typeof(Resource).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to Can't get user id.. + /// + internal static string ExceptionNoUserId { + get { + return ResourceManager.GetString("ExceptionNoUserId", resourceCulture); + } + } + } +} diff --git a/BackEnd/Timeline/Controllers/Resource.resx b/BackEnd/Timeline/Controllers/Resource.resx new file mode 100644 index 00000000..ec45a5c9 --- /dev/null +++ b/BackEnd/Timeline/Controllers/Resource.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Can't get user id. + + \ No newline at end of file -- cgit v1.2.3