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

53 lines
1.6 KiB
C#

using System.Security.Claims;
7 years ago
using System.Collections.Generic;
9 years ago
using System.Linq;
using Microsoft.EntityFrameworkCore;
9 years ago
using Yavsc.Models;
7 years ago
using Yavsc.Models.Blog;
9 years ago
namespace Yavsc.Helpers
{
public static class UserHelpers
{
public static string GetUserId(this ClaimsPrincipal user)
{
2 years ago
return user.FindFirstValue("sub");
}
public static string GetUserName(this ClaimsPrincipal user)
{
return user.FindFirstValue(ClaimTypes.Name);
}
public static bool IsSignedIn(this ClaimsPrincipal user)
{
return user.Identity.IsAuthenticated;
}
7 years ago
public static IEnumerable<BlogPost> UserPosts(this ApplicationDbContext dbContext, string posterId, string readerId)
{
if (readerId == null)
{
10 months ago
var userPosts = dbContext.BlogSpot.Include(
7 years ago
b => b.Author
).Where(x => ((x.AuthorId == posterId))).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();
10 months ago
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))));
}
7 years ago
}
9 years ago
}
}