feat: add release communications
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
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);
|
||||
Reference in New Issue
Block a user