Extracted CurrentUserExtensions.

pull/206/head
Halil İbrahim Kalkan 7 years ago
parent b17a190264
commit c6d59f84db

@ -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<T>(string claimType)
where T : struct
{
var value = FindClaimValue(claimType);
if (value == null)
{
return default;
}
return value.To<T>();
}
public virtual string FindClaimValue(string claimType)
{
return FindClaim(claimType)?.Value;
}
}
}

@ -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<T>(this ICurrentUser currentUser, string claimType)
where T : struct
{
var value = currentUser.FindClaimValue(claimType);
if (value == null)
{
return default;
}
return value.To<T>();
}
}
}

@ -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<T>(string claimType)
where T : struct;
}
}

Loading…
Cancel
Save