From c6d59f84dbbd47ddc490f3b04cf574873a76c8dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Fri, 9 Feb 2018 16:19:37 +0300 Subject: [PATCH] Extracted CurrentUserExtensions. --- .../Volo/Abp/Session/CurrentUser.cs | 25 +++--------------- .../Volo/Abp/Session/CurrentUserExtensions.cs | 26 +++++++++++++++++++ .../Volo/Abp/Session/ICurrentUser.cs | 10 +++---- 3 files changed, 35 insertions(+), 26 deletions(-) create mode 100644 src/Volo.Abp.Session/Volo/Abp/Session/CurrentUserExtensions.cs diff --git a/src/Volo.Abp.Session/Volo/Abp/Session/CurrentUser.cs b/src/Volo.Abp.Session/Volo/Abp/Session/CurrentUser.cs index e6f62ce870..87b2dd0deb 100644 --- a/src/Volo.Abp.Session/Volo/Abp/Session/CurrentUser.cs +++ b/src/Volo.Abp.Session/Volo/Abp/Session/CurrentUser.cs @@ -6,7 +6,7 @@ using Volo.Abp.Security.Claims; namespace Volo.Abp.Session { - public class CurrentUser : ICurrentUser, ITransientDependency //TODO: Singleton? + public class CurrentUser : ICurrentUser, ITransientDependency { public virtual bool IsAuthenticated => Id.HasValue; @@ -14,7 +14,7 @@ namespace Volo.Abp.Session { get { - var value = FindClaimValue(AbpClaimTypes.UserId); + var value = this.FindClaimValue(AbpClaimTypes.UserId); if (value == null) { return null; @@ -24,9 +24,9 @@ namespace Volo.Abp.Session } } - public virtual string UserName => FindClaimValue(AbpClaimTypes.UserName); + public virtual string UserName => this.FindClaimValue(AbpClaimTypes.UserName); - public virtual string Email => FindClaimValue(AbpClaimTypes.Email); + public virtual string Email => this.FindClaimValue(AbpClaimTypes.Email); private readonly ICurrentPrincipalAccessor _principalAccessor; @@ -39,22 +39,5 @@ namespace Volo.Abp.Session { return _principalAccessor.Principal?.Claims.FirstOrDefault(c => c.Type == claimType); } - - public virtual T FindClaimValue(string claimType) - where T : struct - { - var value = FindClaimValue(claimType); - if (value == null) - { - return default; - } - - return value.To(); - } - - public virtual string FindClaimValue(string claimType) - { - return FindClaim(claimType)?.Value; - } } } \ No newline at end of file diff --git a/src/Volo.Abp.Session/Volo/Abp/Session/CurrentUserExtensions.cs b/src/Volo.Abp.Session/Volo/Abp/Session/CurrentUserExtensions.cs new file mode 100644 index 0000000000..1ddaa2af7b --- /dev/null +++ b/src/Volo.Abp.Session/Volo/Abp/Session/CurrentUserExtensions.cs @@ -0,0 +1,26 @@ +using System; +using JetBrains.Annotations; + +namespace Volo.Abp.Session +{ + public static class CurrentUserExtensions + { + [CanBeNull] + public static string FindClaimValue(this ICurrentUser currentUser, string claimType) + { + return currentUser.FindClaim(claimType)?.Value; + } + + public static T FindClaimValue(this ICurrentUser currentUser, string claimType) + where T : struct + { + var value = currentUser.FindClaimValue(claimType); + if (value == null) + { + return default; + } + + return value.To(); + } + } +} \ No newline at end of file diff --git a/src/Volo.Abp.Session/Volo/Abp/Session/ICurrentUser.cs b/src/Volo.Abp.Session/Volo/Abp/Session/ICurrentUser.cs index d4eee21cf7..8185a0a5c6 100644 --- a/src/Volo.Abp.Session/Volo/Abp/Session/ICurrentUser.cs +++ b/src/Volo.Abp.Session/Volo/Abp/Session/ICurrentUser.cs @@ -1,5 +1,6 @@ using System; using System.Security.Claims; +using JetBrains.Annotations; namespace Volo.Abp.Session { @@ -7,17 +8,16 @@ namespace Volo.Abp.Session { bool IsAuthenticated { get; } + [CanBeNull] Guid? Id { get; } + [CanBeNull] string UserName { get; } + [CanBeNull] string Email { get; } + [CanBeNull] Claim FindClaim(string claimType); - - string FindClaimValue(string claimType); - - T FindClaimValue(string claimType) - where T : struct; } }