2023-03-19 17:57:55 +00:00
|
|
|
using System.Security.Claims;
|
2018-11-20 15:50:04 +01:00
|
|
|
using System.Collections.Generic;
|
2017-02-01 19:53:10 +01:00
|
|
|
using System.Linq;
|
2023-03-19 17:57:55 +00:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2017-02-01 19:53:10 +01:00
|
|
|
using Yavsc.Models;
|
2018-11-20 15:50:04 +01:00
|
|
|
using Yavsc.Models.Blog;
|
2017-02-01 19:53:10 +01:00
|
|
|
|
|
|
|
|
namespace Yavsc.Helpers
|
|
|
|
|
{
|
|
|
|
|
public static class UserHelpers
|
|
|
|
|
{
|
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)
|
|
|
|
|
{
|
|
|
|
|
return user.FindFirstValue(ClaimTypes.Name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool IsSignedIn(this ClaimsPrincipal user)
|
|
|
|
|
{
|
|
|
|
|
return user.Identity.IsAuthenticated;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-20 15:50:04 +01:00
|
|
|
public static IEnumerable<BlogPost> UserPosts(this ApplicationDbContext dbContext, string posterId, string readerId)
|
|
|
|
|
{
|
2024-12-03 01:44:58 +00:00
|
|
|
if (readerId == null)
|
|
|
|
|
{
|
2025-06-28 16:14:52 +01:00
|
|
|
var userPosts = dbContext.blogspotPublications.Include(
|
|
|
|
|
b => b.BlogPost
|
|
|
|
|
).Where(x => x.BlogPost.AuthorId == posterId)
|
|
|
|
|
.Select(x=>x.BlogPost).ToArray();
|
2024-12-03 01:44:58 +00:00
|
|
|
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();
|
2025-02-08 20:06:24 +00:00
|
|
|
return dbContext.BlogSpot.Include(
|
2024-12-03 01:44:58 +00:00
|
|
|
b => b.Author
|
|
|
|
|
).Include(p => p.ACL).Where(x => x.Author.Id == posterId &&
|
2025-02-23 20:23:23 +00:00
|
|
|
(x.ACL.Count == 0 || x.ACL.Any(a => readerCirclesMemberships.Contains(a.CircleId))));
|
2024-12-03 01:44:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-20 15:50:04 +01:00
|
|
|
}
|
2017-02-01 19:53:10 +01:00
|
|
|
}
|
|
|
|
|
}
|