SocialPub/PrivaPub/Extensions/StringExtensions.cs
2023-02-19 00:43:43 +01:00

37 lines
1.1 KiB
C#

using PrivaPub.ClientModels;
using System.Security.Claims;
namespace PrivaPub.Extensions
{
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;
}
}
}