From 2ee0dbec8a0a798e3dfb3d80ed61a4b4da55d28d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Mon, 12 Feb 2018 09:58:14 +0300 Subject: [PATCH] Add role related properties/methods to ICurrentUser --- .../Volo/Abp/Session/CurrentUser.cs | 14 ++++++++++++++ .../Volo/Abp/Session/ICurrentUser.cs | 8 ++++++++ 2 files changed, 22 insertions(+) diff --git a/src/Volo.Abp.Session/Volo/Abp/Session/CurrentUser.cs b/src/Volo.Abp.Session/Volo/Abp/Session/CurrentUser.cs index 87b2dd0deb..fc38d1a93b 100644 --- a/src/Volo.Abp.Session/Volo/Abp/Session/CurrentUser.cs +++ b/src/Volo.Abp.Session/Volo/Abp/Session/CurrentUser.cs @@ -8,6 +8,8 @@ namespace Volo.Abp.Session { public class CurrentUser : ICurrentUser, ITransientDependency { + private static readonly Claim[] EmptyClaimsArray = new Claim[0]; + public virtual bool IsAuthenticated => Id.HasValue; public virtual Guid? Id @@ -28,6 +30,8 @@ namespace Volo.Abp.Session public virtual string Email => this.FindClaimValue(AbpClaimTypes.Email); + public virtual string[] Roles => FindClaims(AbpClaimTypes.Role).Select(c => c.Value).ToArray(); + private readonly ICurrentPrincipalAccessor _principalAccessor; public CurrentUser(ICurrentPrincipalAccessor principalAccessor) @@ -39,5 +43,15 @@ namespace Volo.Abp.Session { return _principalAccessor.Principal?.Claims.FirstOrDefault(c => c.Type == claimType); } + + public virtual Claim[] FindClaims(string claimType) + { + return _principalAccessor.Principal?.Claims.Where(c => c.Type == claimType).ToArray() ?? EmptyClaimsArray; + } + + public bool IsInRole(string roleName) + { + return FindClaims(AbpClaimTypes.Role).Any(c => c.Value == roleName); + } } } \ 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 8185a0a5c6..a24833c898 100644 --- a/src/Volo.Abp.Session/Volo/Abp/Session/ICurrentUser.cs +++ b/src/Volo.Abp.Session/Volo/Abp/Session/ICurrentUser.cs @@ -17,7 +17,15 @@ namespace Volo.Abp.Session [CanBeNull] string Email { get; } + [NotNull] + string[] Roles { get; } + [CanBeNull] Claim FindClaim(string claimType); + + [NotNull] + Claim[] FindClaims(string claimType); + + bool IsInRole(string roleName); } }