Adds the consumption activity access policy and repository interfaces

This commit is contained in:
BinaryAlley committed 2026-10-24 15:29:43 +03:00
1 parent 822ccc9adc
commit 2ffacb5360
7 files changed
+251

No files matched your search

@@ -0,0 +1,42 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.MediaConsumption;
using Lumina.Application.Common.DataAccess.Repositories.Common.Base;
using Lumina.Domain.Common.Primitives;
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.Common.DataAccess.Repositories.MediaConsumption;
/// <summary>
/// Interface for the repository for the consumption records used to build consumption statistics.
/// </summary>
public interface IConsumptionStatisticsRepository : IRepository<MediaConsumptionRecordEntity>
{
/// <summary>
/// Gets the consumption records of the user identified by <paramref name="userId"/>, optionally limited to a media library, a media kind, and a date
/// range.
/// </summary>
/// <param name="userId">The Id of the user whose consumption records are retrieved.</param>
/// <param name="libraryId">The Id of the media library to restrict the records to, or <see langword="null"/> for every library.</param>
/// <param name="mediaKind">The kind of media to restrict the records to, or <see langword="null"/> for every kind.</param>
/// <param name="fromUtc">The start of the date range, or <see langword="null"/> when the range has no start.</param>
/// <param name="toUtc">The end of the date range, or <see langword="null"/> when the range has no end.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> containing either the consumption records of the user, or an error.</returns>
Task<Result<IReadOnlyList<MediaConsumptionRecordEntity>>> GetForUserAsync(Guid userId, Guid? libraryId, MediaKind? mediaKind, DateTime? fromUtc, DateTime? toUtc, CancellationToken cancellationToken);
/// <summary>
/// Gets the consumption records of every user, optionally limited to a media library, a media kind, and a date range.
/// </summary>
/// <param name="libraryId">The Id of the media library to restrict the records to, or <see langword="null"/> for every library.</param>
/// <param name="mediaKind">The kind of media to restrict the records to, or <see langword="null"/> for every kind.</param>
/// <param name="fromUtc">The start of the date range, or <see langword="null"/> when the range has no start.</param>
/// <param name="toUtc">The end of the date range, or <see langword="null"/> when the range has no end.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> containing either the consumption records of every user, or an error.</returns>
Task<Result<IReadOnlyList<MediaConsumptionRecordEntity>>> GetForAllUsersAsync(Guid? libraryId, MediaKind? mediaKind, DateTime? fromUtc, DateTime? toUtc, CancellationToken cancellationToken);
}
@@ -0,0 +1,59 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.MediaConsumption;
using Lumina.Application.Common.DataAccess.Repositories.Common.Actions;
using Lumina.Application.Common.DataAccess.Repositories.Common.Base;
using Lumina.Domain.Common.Primitives;
using System;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.Common.DataAccess.Repositories.MediaConsumption;
/// <summary>
/// Interface for the repository for media consumption records.
/// </summary>
public interface IMediaConsumptionRecordRepository : IRepository<MediaConsumptionRecordEntity>,
IInsertRepositoryAction<MediaConsumptionRecordEntity>
{
/// <summary>
/// Gets the consumption record identified by <paramref name="id"/> when it belongs to the user identified by <paramref name="userId"/>, if it exists.
/// </summary>
/// <param name="id">The Id of the consumption record to get.</param>
/// <param name="userId">The Id of the user the consumption record must belong to.</param>
/// <param name="shouldTrackEntities">Whether the retrieved record should be tracked by the persistence medium, so that changes to it can be saved.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>
/// An <see cref="Result{TValue}"/> containing either the consumption record identified by <paramref name="id"/> and owned by
/// <paramref name="userId"/>, <see langword="null"/> when no such record exists, or an error.
/// </returns>
Task<Result<MediaConsumptionRecordEntity?>> GetByIdForUserAsync(Guid id, Guid userId, bool shouldTrackEntities = true, CancellationToken cancellationToken = default);
/// <summary>
/// Updates the consumption record of the user identified by <paramref name="userId"/>, when the record belongs to that user and still holds the
/// version it was read with.
/// </summary>
/// <param name="data">The consumption record whose editable values are applied to the stored record. Its owner must match <paramref name="userId"/>.</param>
/// <param name="userId">The Id of the user the consumption record must belong to. It is authoritative for locating the stored record; the owner carried by <paramref name="data"/> is not used to select it.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> representing either a successful update, or an error.</returns>
Task<Result<Updated>> UpdateForUserAsync(MediaConsumptionRecordEntity data, Guid userId, CancellationToken cancellationToken);
/// <summary>
/// Deletes the consumption record identified by <paramref name="id"/> when it belongs to the user identified by <paramref name="userId"/>. A record
/// that does not exist is not an error.
/// </summary>
/// <param name="id">The Id of the consumption record to delete.</param>
/// <param name="userId">The Id of the user the consumption record must belong to.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> representing either a successful deletion, or an error.</returns>
Task<Result<Deleted>> DeleteForUserAsync(Guid id, Guid userId, CancellationToken cancellationToken);
/// <summary>
/// Deletes every consumption record of the user identified by <paramref name="userId"/>.
/// </summary>
/// <param name="userId">The Id of the user whose consumption records are deleted.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> representing either a successful deletion, or an error.</returns>
Task<Result<Deleted>> DeleteAllForUserAsync(Guid userId, CancellationToken cancellationToken);
}
@@ -0,0 +1,70 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.MediaConsumption;
using Lumina.Application.Common.DataAccess.Repositories.Common.Base;
using Lumina.Domain.Common.Primitives;
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
using System;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.Common.DataAccess.Repositories.MediaConsumption;
/// <summary>
/// Interface for the repository for media resume points.
/// </summary>
public interface IMediaResumePointRepository : IRepository<MediaResumePointEntity>
{
/// <summary>
/// Gets the resume point of the user identified by <paramref name="userId"/> for the media item identified by <paramref name="mediaKind"/> and
/// <paramref name="mediaItemId"/>, if it exists.
/// </summary>
/// <param name="userId">The Id of the user the resume point must belong to.</param>
/// <param name="mediaKind">The kind of the media item to resume.</param>
/// <param name="mediaItemId">The Id of the media item to resume.</param>
/// <param name="shouldTrackEntities">Whether the retrieved resume point should be tracked by the persistence medium, so that changes to it can be saved.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>
/// An <see cref="Result{TValue}"/> containing either the resume point of the user for the media item, <see langword="null"/> when no such resume
/// point exists, or an error.
/// </returns>
Task<Result<MediaResumePointEntity?>> GetForUserAndMediaItemAsync(Guid userId, MediaKind mediaKind, Guid mediaItemId, bool shouldTrackEntities = true, CancellationToken cancellationToken = default);
/// <summary>
/// Saves the resume point of the user identified by <paramref name="userId"/>, inserting it when it does not exist and updating it otherwise. The
/// business key of the resume point, made of the user, the media kind, and the media item, is preserved.
/// </summary>
/// <param name="data">The resume point whose values are saved. Its owner must match <paramref name="userId"/>.</param>
/// <param name="userId">The Id of the user the resume point must belong to. It is authoritative for locating the stored resume point and for the owner of a new one; the owner carried by <paramref name="data"/> is not used to select it.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> representing either a successful save, or an error.</returns>
Task<Result<Updated>> UpsertForUserAsync(MediaResumePointEntity data, Guid userId, CancellationToken cancellationToken);
/// <summary>
/// Deletes the resume point of the user identified by <paramref name="userId"/> for the media item identified by <paramref name="mediaKind"/> and
/// <paramref name="mediaItemId"/>. A resume point that does not exist is not an error.
/// </summary>
/// <param name="userId">The Id of the user the resume point must belong to.</param>
/// <param name="mediaKind">The kind of the media item to resume.</param>
/// <param name="mediaItemId">The Id of the media item to resume.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> representing either a successful deletion, or an error.</returns>
Task<Result<Deleted>> DeleteForUserAndMediaItemAsync(Guid userId, MediaKind mediaKind, Guid mediaItemId, CancellationToken cancellationToken);
/// <summary>
/// Deletes every resume point of the user identified by <paramref name="userId"/>.
/// </summary>
/// <param name="userId">The Id of the user whose resume points are deleted.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> representing either a successful deletion, or an error.</returns>
Task<Result<Deleted>> DeleteAllForUserAsync(Guid userId, CancellationToken cancellationToken);
/// <summary>
/// Deletes every resume point of the user identified by <paramref name="userId"/> in the media library identified by <paramref name="libraryId"/>.
/// </summary>
/// <param name="userId">The Id of the user whose resume points are deleted.</param>
/// <param name="libraryId">The Id of the media library whose resume points are deleted.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> representing either a successful deletion, or an error.</returns>
Task<Result<Deleted>> DeleteAllForUserInLibraryAsync(Guid userId, Guid libraryId, CancellationToken cancellationToken);
}
@@ -0,0 +1,14 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.Infrastructure.Authorization.Policies.Common.Base;
using System;
using System.Diagnostics;
#endregion
namespace Lumina.Application.Common.Infrastructure.Authorization.Policies.ConsumptionActivity;
/// <summary>
/// Represents the context required to evaluate the consumption activity authorization policy.
/// </summary>
/// <param name="TargetUserId">The Id of the user whose consumption activity is being accessed.</param>
[DebuggerDisplay("TargetUserId: {TargetUserId}")]
public sealed record ConsumptionActivityAccessPolicyContext(Guid TargetUserId) : PolicyContext;
@@ -0,0 +1,12 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.Infrastructure.Authorization.Policies.Common.Base;
#endregion
namespace Lumina.Application.Common.Infrastructure.Authorization.Policies.ConsumptionActivity;
/// <summary>
/// Interface for the consumption activity authorization policy.
/// </summary>
public interface IConsumptionActivityAccessPolicy : IAuthorizationPolicy
{
}
@@ -2,6 +2,7 @@
using Lumina.Application.Common.DomainEvents;
using Lumina.Application.Common.Infrastructure.Authentication;
using Lumina.Application.Common.Infrastructure.Authorization;
using Lumina.Application.Common.Infrastructure.Authorization.Policies.ConsumptionActivity;
using Lumina.Application.Common.Infrastructure.Authorization.Policies.LibraryOwnership;
using Lumina.Application.Common.Infrastructure.Authorization.Policies.Over18;
using Lumina.Application.Common.Infrastructure.Models.DTO.Configuration;
@@ -30,6 +31,7 @@ using Lumina.Infrastructure.Common.Networking;
using Lumina.Infrastructure.Core.Authentication;
using Lumina.Infrastructure.Core.Authorization;
using Lumina.Infrastructure.Core.Authorization.Policies.Common.Factory;
using Lumina.Infrastructure.Core.Authorization.Policies.ConsumptionActivity;
using Lumina.Infrastructure.Core.Authorization.Policies.LibraryOwnership;
using Lumina.Infrastructure.Core.Authorization.Policies.Over18;
using Lumina.Infrastructure.Core.MediaLibrary.Management.Scanning.Cancellation;
@@ -103,6 +105,7 @@ public static class InfrastructureLayerServices
// Authorization.
services.AddScoped<IOver18Policy, Over18Policy>();
services.AddScoped<ILibraryOwnershipPolicy, LibraryOwnershipPolicy>();
services.AddScoped<IConsumptionActivityAccessPolicy, ConsumptionActivityAccessPolicy>();
services.AddScoped<IAuthorizationPolicyFactory, AuthorizationPolicyFactory>();
services.AddScoped<IAuthorizationService, AuthorizationService>();
@@ -0,0 +1,51 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.Infrastructure.Authorization;
using Lumina.Application.Common.Infrastructure.Authorization.Policies.Common.Base;
using Lumina.Application.Common.Infrastructure.Authorization.Policies.ConsumptionActivity;
using System;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Infrastructure.Core.Authorization.Policies.ConsumptionActivity;
/// <summary>
/// Authorization policy that checks whether a user can access the consumption activity of a user: admins can access any user's consumption activity,
/// while other users can only access their own.
/// </summary>
public class ConsumptionActivityAccessPolicy : IConsumptionActivityAccessPolicy
{
private readonly IAuthorizationService _authorizationService;
/// <summary>
/// Initializes a new instance of the <see cref="ConsumptionActivityAccessPolicy"/> class.
/// </summary>
/// <param name="authorizationService">Injected service for authorization related functionality.</param>
public ConsumptionActivityAccessPolicy(IAuthorizationService authorizationService)
{
_authorizationService = authorizationService;
}
/// <summary>
/// Evaluates the policy for the user identified by <paramref name="userId"/> against the consumption activity of the user identified in
/// <paramref name="context"/>.
/// </summary>
/// <param name="userId">The unique identifier of the user for which to evaluate the policy.</param>
/// <param name="context">The context carrying the Id of the user whose consumption activity is being accessed.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns><see langword="true"/> if the user is an admin or accesses their own consumption activity, <see langword="false"/> otherwise.</returns>
public async Task<bool> EvaluateAsync(Guid userId, PolicyContext? context, CancellationToken cancellationToken)
{
// An empty user id is a defaulted value, not a real user, so it must never match another empty id.
if (userId == Guid.Empty)
return false;
if (context is not ConsumptionActivityAccessPolicyContext consumptionActivityContext)
return false;
if (consumptionActivityContext.TargetUserId == Guid.Empty)
return false;
if (consumptionActivityContext.TargetUserId == userId) // A user can access their own consumption activity.
return true;
// Admins can access consumption activities all the time.
return await _authorizationService.IsInRoleAsync(userId, AuthorizationRoles.ADMIN, cancellationToken).ConfigureAwait(false);
}
}