yavsc/src/Yavsc.Server/Helpers/UserHelpers.cs

54 lines
1.8 KiB
C#
Raw Normal View History

2026-06-19 21:13:17 +01:00
using Microsoft.EntityFrameworkCore;
2023-03-19 17:57:55 +00:00
using System.Security.Claims;
2026-06-19 21:13:17 +01:00
using Yavsc.Models;
using Yavsc.Models.Blog;
2017-02-01 19:53:10 +01:00
namespace Yavsc.Server.Helpers
2017-02-01 19:53:10 +01:00
{
public static class UserHelpers
{
2026-06-19 21:13:17 +01:00
public static IEnumerable<BlogPost> UserPosts(this ApplicationDbContext dbContext, string posterId, string? readerId)
{
if (readerId == null)
{
var userPosts = dbContext.blogSpotPublications.Include(
b => b.BlogPost
).Where(x => x.BlogPost.AuthorId == posterId)
.Select(x => x.BlogPost).ToArray();
return userPosts;
}
else
{
long[] readerCirclesMemberships =
dbContext.Circle.Include(c => c.Members)
.Where(c => c.Members.Any(m => m.MemberId == readerId))
.Select(c => c.Id).ToArray();
return dbContext.BlogSpot.Include(
b => b.Author
).Include(p => p.ACL).Where(x => x.Author.Id == posterId &&
(x.ACL.Count == 0 || x.ACL.Any(a => readerCirclesMemberships.Contains(a.CircleId))));
}
}
2026-07-12 01:17:52 +01:00
2023-03-19 17:57:55 +00:00
public static string GetUserId(this ClaimsPrincipal user)
{
2024-03-04 01:02:19 +00:00
return user.FindFirstValue("sub");
2023-03-19 17:57:55 +00:00
}
2024-12-03 01:44:58 +00:00
2023-03-19 17:57:55 +00:00
public static string GetUserName(this ClaimsPrincipal user)
{
2025-07-10 15:19:28 +01:00
return user.FindFirstValue("name");
2023-03-19 17:57:55 +00:00
}
public static bool IsSignedIn(this ClaimsPrincipal user)
{
return user.Identity.IsAuthenticated;
}
2025-07-31 11:44:02 +01:00
public static bool IsInMsRole(this ClaimsPrincipal user, string roleName)
{
return user.HasClaim("http://schemas.microsoft.com/ws/2008/06/identity/claims/role", roleName);
}
2017-02-01 19:53:10 +01:00
}
}