SocialPub/PrivaPub/Extensions/StringExtensions.cs

37 lines
1.1 KiB
C#
Raw Normal View History

2023-02-19 00:43:43 +01:00
using PrivaPub.ClientModels;
2023-02-18 08:52:17 +01:00
using System.Security.Claims;
2023-02-19 00:43:43 +01:00
namespace PrivaPub.Extensions
2023-02-18 08:52:17 +01:00
{
public static class StringExtensions
{
public static string GetUserId(this ClaimsPrincipal claimsPrincipal)
{
return claimsPrincipal?.Claims?.FirstOrDefault(c => c.Type == ClaimTypes.UserData)?.Value;
}
public static string GetUserName(this ClaimsPrincipal claimsPrincipal)
{
var userId = claimsPrincipal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value;
return string.IsNullOrEmpty(userId) ? default : userId;
}
public static List<string> GetUserPolicies(this ClaimsPrincipal claimsPrincipal)
{
var policies = new List<string>();
if (claimsPrincipal.Claims.Any(c => c.Type == Policies.IsAdmin && bool.Parse(c.Value)))
policies.Add(Policies.IsAdmin);
if (claimsPrincipal.Claims.Any(c => c.Type == Policies.IsUser && bool.Parse(c.Value)))
policies.Add(Policies.IsUser);
if (claimsPrincipal.Claims.Any(c => c.Type == Policies.IsModerator && bool.Parse(c.Value)))
policies.Add(Policies.IsModerator);
return policies;
}
}
}