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); } }