47 lines
1.9 KiB
C#
47 lines
1.9 KiB
C#
using System.Security.Claims;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Socialize.Api.Data;
|
|
using Socialize.Api.Modules.Identity.Contracts;
|
|
using Socialize.Api.Modules.Organizations.Data;
|
|
using Socialize.Api.Modules.Organizations.Services;
|
|
using Socialize.Api.Modules.ReleaseCommunications.Data;
|
|
|
|
namespace Socialize.Api.Modules.ReleaseCommunications.Services;
|
|
|
|
internal static class ReleaseUpdateVisibility
|
|
{
|
|
public static async Task<ReleaseUpdateAudienceContext> GetAudienceContextAsync(
|
|
AppDbContext dbContext,
|
|
ClaimsPrincipal user,
|
|
Guid userId,
|
|
CancellationToken ct)
|
|
{
|
|
bool isDeveloper = user.IsInRole(KnownRoles.Developer);
|
|
bool isOrganizationOwner = await dbContext.Organizations.AnyAsync(
|
|
organization => organization.OwnerUserId == userId,
|
|
ct)
|
|
|| await dbContext.OrganizationMemberships.AnyAsync(
|
|
membership =>
|
|
membership.UserId == userId &&
|
|
membership.Role == OrganizationRoles.Owner,
|
|
ct);
|
|
|
|
return new ReleaseUpdateAudienceContext(isDeveloper, isOrganizationOwner);
|
|
}
|
|
|
|
public static IQueryable<ReleaseUpdate> VisibleTo(
|
|
this IQueryable<ReleaseUpdate> query,
|
|
ReleaseUpdateAudienceContext context)
|
|
{
|
|
return query.Where(update =>
|
|
update.Status == ReleaseUpdateStatus.Published &&
|
|
(update.Audience == ReleaseUpdateAudience.Everyone ||
|
|
(update.Audience == ReleaseUpdateAudience.OrganizationOwners && context.IsOrganizationOwner) ||
|
|
(update.Audience == ReleaseUpdateAudience.Developers && context.IsDeveloper)));
|
|
}
|
|
}
|
|
|
|
internal record ReleaseUpdateAudienceContext(
|
|
bool IsDeveloper,
|
|
bool IsOrganizationOwner);
|