From af566b3c47c5bd2522abcba638cab9d271e45510 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Wed, 11 Oct 2017 19:06:05 +0300 Subject: [PATCH] Added ICurrentUser (instead of IAbpSession). --- .../Shared/Components/AbpMenu/Default.cshtml | 12 +++- .../HttpContextCurrentPrincipalAccessor.cs | 18 ++++++ .../Abp/MultiTenancy/MultiTenancyManager.cs | 4 +- .../Abp/MultiTenancy/TenantResolveContext.cs | 2 +- .../Volo/Abp/Security/Claims/AbpClaimTypes.cs | 34 +++++++++++ .../Claims/ICurrentPrincipalAccessor.cs | 9 +++ .../Claims/ThreadCurrentPrincipalAccessor.cs | 11 ++++ src/Volo.Abp/Volo/Abp/Session/CurrentUser.cs | 60 +++++++++++++++++++ src/Volo.Abp/Volo/Abp/Session/ICurrentUser.cs | 23 +++++++ 9 files changed, 168 insertions(+), 5 deletions(-) create mode 100644 src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/Claims/HttpContextCurrentPrincipalAccessor.cs create mode 100644 src/Volo.Abp/Volo/Abp/Security/Claims/AbpClaimTypes.cs create mode 100644 src/Volo.Abp/Volo/Abp/Security/Claims/ICurrentPrincipalAccessor.cs create mode 100644 src/Volo.Abp/Volo/Abp/Security/Claims/ThreadCurrentPrincipalAccessor.cs create mode 100644 src/Volo.Abp/Volo/Abp/Session/CurrentUser.cs create mode 100644 src/Volo.Abp/Volo/Abp/Session/ICurrentUser.cs diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/Views/Shared/Components/AbpMenu/Default.cshtml b/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/Views/Shared/Components/AbpMenu/Default.cshtml index 4a2365b898..1835de738a 100644 --- a/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/Views/Shared/Components/AbpMenu/Default.cshtml +++ b/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/Views/Shared/Components/AbpMenu/Default.cshtml @@ -1,6 +1,8 @@ @using System.Threading.Tasks +@using Volo.Abp.Session @using Volo.Abp.Ui.Navigation -@model Volo.Abp.Ui.Navigation.ApplicationMenu +@model ApplicationMenu +@inject ICurrentUser CurrentUser diff --git a/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/Claims/HttpContextCurrentPrincipalAccessor.cs b/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/Claims/HttpContextCurrentPrincipalAccessor.cs new file mode 100644 index 0000000000..d1318e2c3e --- /dev/null +++ b/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/Claims/HttpContextCurrentPrincipalAccessor.cs @@ -0,0 +1,18 @@ +using System.Security.Claims; +using Microsoft.AspNetCore.Http; +using Volo.Abp.Security.Claims; + +namespace Volo.Abp.AspNetCore.Security.Claims +{ + public class HttpContextCurrentPrincipalAccessor : ThreadCurrentPrincipalAccessor + { + public override ClaimsPrincipal Principal => _httpContextAccessor.HttpContext?.User ?? base.Principal; + + private readonly IHttpContextAccessor _httpContextAccessor; + + public HttpContextCurrentPrincipalAccessor(IHttpContextAccessor httpContextAccessor) + { + _httpContextAccessor = httpContextAccessor; + } + } +} diff --git a/src/Volo.Abp.MultiTenancy/Volo/Abp/MultiTenancy/MultiTenancyManager.cs b/src/Volo.Abp.MultiTenancy/Volo/Abp/MultiTenancy/MultiTenancyManager.cs index 1db22637d7..a53ffd7e60 100644 --- a/src/Volo.Abp.MultiTenancy/Volo/Abp/MultiTenancy/MultiTenancyManager.cs +++ b/src/Volo.Abp.MultiTenancy/Volo/Abp/MultiTenancy/MultiTenancyManager.cs @@ -76,6 +76,8 @@ namespace Volo.Abp.MultiTenancy return _tenantScopeProvider.CurrentScope.Tenant; } + //TODO: Get from ICurrentUser before resolvers and fail if resolvers find a different tenant! + return GetCurrentTenantFromResolvers(); } @@ -94,7 +96,7 @@ namespace Volo.Abp.MultiTenancy { tenantResolver.Resolve(context); - if (context.ResolvedTenantOrHost()) + if (context.HasResolvedTenantOrHost()) { if (context.TenantIdOrName == null) { diff --git a/src/Volo.Abp.MultiTenancy/Volo/Abp/MultiTenancy/TenantResolveContext.cs b/src/Volo.Abp.MultiTenancy/Volo/Abp/MultiTenancy/TenantResolveContext.cs index 6f84d5ed75..6f27d5be17 100644 --- a/src/Volo.Abp.MultiTenancy/Volo/Abp/MultiTenancy/TenantResolveContext.cs +++ b/src/Volo.Abp.MultiTenancy/Volo/Abp/MultiTenancy/TenantResolveContext.cs @@ -10,7 +10,7 @@ namespace Volo.Abp.MultiTenancy public bool Handled { get; set; } - internal bool ResolvedTenantOrHost() + internal bool HasResolvedTenantOrHost() { return Handled || TenantIdOrName != null; } diff --git a/src/Volo.Abp/Volo/Abp/Security/Claims/AbpClaimTypes.cs b/src/Volo.Abp/Volo/Abp/Security/Claims/AbpClaimTypes.cs new file mode 100644 index 0000000000..79eaf25701 --- /dev/null +++ b/src/Volo.Abp/Volo/Abp/Security/Claims/AbpClaimTypes.cs @@ -0,0 +1,34 @@ +using System.Security.Claims; + +namespace Volo.Abp.Security.Claims +{ + /// + /// Used to get ABP-specific claim type names. + /// + public static class AbpClaimTypes + { + /// + /// UserId. + /// Default: + /// + public static string UserName { get; set; } = ClaimTypes.Name; + + /// + /// UserId. + /// Default: + /// + public static string UserId { get; set; } = ClaimTypes.NameIdentifier; + + /// + /// UserId. + /// Default: + /// + public static string Role { get; set; } = ClaimTypes.Role; + + /// + /// UserId. + /// Default: + /// + public static string Email { get; set; } = ClaimTypes.Email; + } +} diff --git a/src/Volo.Abp/Volo/Abp/Security/Claims/ICurrentPrincipalAccessor.cs b/src/Volo.Abp/Volo/Abp/Security/Claims/ICurrentPrincipalAccessor.cs new file mode 100644 index 0000000000..d49e61ddd6 --- /dev/null +++ b/src/Volo.Abp/Volo/Abp/Security/Claims/ICurrentPrincipalAccessor.cs @@ -0,0 +1,9 @@ +using System.Security.Claims; + +namespace Volo.Abp.Security.Claims +{ + public interface ICurrentPrincipalAccessor + { + ClaimsPrincipal Principal { get; } + } +} diff --git a/src/Volo.Abp/Volo/Abp/Security/Claims/ThreadCurrentPrincipalAccessor.cs b/src/Volo.Abp/Volo/Abp/Security/Claims/ThreadCurrentPrincipalAccessor.cs new file mode 100644 index 0000000000..de0032c51d --- /dev/null +++ b/src/Volo.Abp/Volo/Abp/Security/Claims/ThreadCurrentPrincipalAccessor.cs @@ -0,0 +1,11 @@ +using System.Security.Claims; +using System.Threading; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.Security.Claims +{ + public class ThreadCurrentPrincipalAccessor : ICurrentPrincipalAccessor, ISingletonDependency + { + public virtual ClaimsPrincipal Principal => Thread.CurrentPrincipal as ClaimsPrincipal; + } +} \ No newline at end of file diff --git a/src/Volo.Abp/Volo/Abp/Session/CurrentUser.cs b/src/Volo.Abp/Volo/Abp/Session/CurrentUser.cs new file mode 100644 index 0000000000..6652a8436a --- /dev/null +++ b/src/Volo.Abp/Volo/Abp/Session/CurrentUser.cs @@ -0,0 +1,60 @@ +using System; +using System.Linq; +using System.Security.Claims; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Security.Claims; + +namespace Volo.Abp.Session +{ + public class CurrentUser : ICurrentUser, ITransientDependency //TODO: Singleton? + { + public virtual bool IsAuthenticated => Id.HasValue; + + public virtual Guid? Id + { + get + { + var value = FindClaimValue(AbpClaimTypes.UserId); + if (value == null) + { + return null; + } + + return Guid.Parse(value); + } + } + + public virtual string UserName => FindClaimValue(AbpClaimTypes.UserName); + + public virtual string Email => FindClaimValue(AbpClaimTypes.Email); + + private readonly ICurrentPrincipalAccessor _principalAccessor; + + public CurrentUser(ICurrentPrincipalAccessor principalAccessor) + { + _principalAccessor = principalAccessor; + } + + public virtual Claim FindClaim(string claimType) + { + 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(T); + } + + 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/Volo/Abp/Session/ICurrentUser.cs b/src/Volo.Abp/Volo/Abp/Session/ICurrentUser.cs new file mode 100644 index 0000000000..d4eee21cf7 --- /dev/null +++ b/src/Volo.Abp/Volo/Abp/Session/ICurrentUser.cs @@ -0,0 +1,23 @@ +using System; +using System.Security.Claims; + +namespace Volo.Abp.Session +{ + public interface ICurrentUser + { + bool IsAuthenticated { get; } + + Guid? Id { get; } + + string UserName { get; } + + string Email { get; } + + Claim FindClaim(string claimType); + + string FindClaimValue(string claimType); + + T FindClaimValue(string claimType) + where T : struct; + } +}