From 8a5cb9116ea91da263ffe497402c598a00bdaeab Mon Sep 17 00:00:00 2001 From: Binary Alley Date: Fri, 23 Oct 2026 20:58:52 +0300 Subject: [PATCH] Adds media consumption rules and centralizes authorization role names --- .../Authorization/AuthorizationRoles.cs | 12 +++ .../GetPermissionsQueryHandler.cs | 2 +- .../Commands/AddRole/AddRoleCommandHandler.cs | 5 +- .../DeleteRole/DeleteRoleCommandHandler.cs | 6 +- .../UpdateRole/UpdateRoleCommandHandler.cs | 2 +- .../GetRolePermissionsQueryHandler.cs | 2 +- .../Queries/GetRoles/GetRolesQueryHandler.cs | 2 +- .../AddLibrary/AddLibraryCommandHandler.cs | 2 +- .../CancelLibrariesScanCommandHandler.cs | 2 +- .../ScanLibrariesCommandHandler.cs | 2 +- .../GetEnabledLibrariesQueryHandler.cs | 10 +-- .../GetLibraries/GetLibrariesQueryHandler.cs | 2 +- .../GetRunningLibraryScansQueryHandler.cs | 16 ++-- .../InstallPluginCommandHandler.cs | 4 +- .../AddScheduledJobCommandHandler.cs | 2 +- .../FireScheduledJobCommandHandler.cs | 2 +- .../RemoveScheduledJobCommandHandler.cs | 2 +- .../StartScheduledJobCommandHandler.cs | 3 +- .../StopScheduledJobCommandHandler.cs | 2 +- ...hedulerDisplayPreferencesCommandHandler.cs | 2 +- .../GetScheduledJobHistoryQueryHandler.cs | 2 +- .../GetScheduledJobsQueryHandler.cs | 2 +- ...SchedulerDisplayPreferencesQueryHandler.cs | 2 +- .../DeleteTheme/DeleteThemeCommandHandler.cs | 2 +- .../InstallThemeCommandHandler.cs | 10 +-- .../RestoreThemeCommandHandler.cs | 2 +- .../SetCurrentThemeCommandHandler.cs | 2 +- .../Queries/GetUsers/GetUsersQueryHandler.cs | 5 +- ...ateUserRoleAndPermissionsCommandHandler.cs | 10 +-- .../GetAuthorizationQueryHandler.cs | 13 ++- .../GetUserPermissionsQueryHandler.cs | 5 +- .../GetUserRole/GetUserRoleQueryHandler.cs | 5 +- .../Core/Seed/DataSeedService.cs | 8 +- .../MediaConsumption/ConsumptionStatus.cs | 22 +++++ .../MediaConsumption/MediaPositionKind.cs | 17 ++++ .../Common/Enums/MediaLibrary/MediaKind.cs | 52 ++++++++++++ .../DurationThresholdConsumptionRule.cs | 29 +++++++ .../EverySessionConsumptionRule.cs | 24 ++++++ .../ValueObjects/MediaConsumptionRule.cs | 83 +++++++++++++++++++ .../PageThresholdConsumptionRule.cs | 29 +++++++ .../LibraryAggregate/LibraryId.cs | 51 ++++++++++++ .../MediaItemAggregate/MediaItemId.cs | 51 ++++++++++++ .../UserAggregate/UserId.cs | 51 ++++++++++++ .../ValueObjects/AudiobookConsumptionRule.cs | 35 ++++++++ .../ValueObjects/BookConsumptionRule.cs | 34 ++++++++ .../ValueObjects/MovieConsumptionRule.cs | 35 ++++++++ .../ValueObjects/MusicTrackConsumptionRule.cs | 35 ++++++++ .../ValueObjects/PhotoConsumptionRule.cs | 29 +++++++ .../PodcastEpisodeConsumptionRule.cs | 35 ++++++++ .../ValueObjects/TvEpisodeConsumptionRule.cs | 35 ++++++++ .../ValueObjects/VideoConsumptionRule.cs | 35 ++++++++ .../LibraryOwnershipPolicy.cs | 3 +- .../Notifications/ScheduledJobsHub.cs | 3 +- .../Roles/GetRoles/GetRolesEndpointSummary.cs | 3 +- .../GetAuthorizationEndpointSummary.cs | 3 +- .../GetUserRole/GetUserRoleEndpointSummary.cs | 3 +- 56 files changed, 767 insertions(+), 80 deletions(-) create mode 100644 src/Lumina.Application/Common/Infrastructure/Authorization/AuthorizationRoles.cs create mode 100644 src/Lumina.Domain.SharedKernel/Common/Enums/MediaConsumption/ConsumptionStatus.cs create mode 100644 src/Lumina.Domain.SharedKernel/Common/Enums/MediaConsumption/MediaPositionKind.cs create mode 100644 src/Lumina.Domain.SharedKernel/Common/Enums/MediaLibrary/MediaKind.cs create mode 100644 src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/Common/ValueObjects/DurationThresholdConsumptionRule.cs create mode 100644 src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/Common/ValueObjects/EverySessionConsumptionRule.cs create mode 100644 src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/Common/ValueObjects/MediaConsumptionRule.cs create mode 100644 src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/Common/ValueObjects/PageThresholdConsumptionRule.cs create mode 100644 src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/ExternalIdentifiers/LibraryManagementBoundedContext/LibraryAggregate/LibraryId.cs create mode 100644 src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/ExternalIdentifiers/MediaLibraryBoundedContext/MediaItemAggregate/MediaItemId.cs create mode 100644 src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/ExternalIdentifiers/UserManagementBoundedContext/UserAggregate/UserId.cs create mode 100644 src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaConsumptionRecordAggregate/ValueObjects/AudiobookConsumptionRule.cs create mode 100644 src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaConsumptionRecordAggregate/ValueObjects/BookConsumptionRule.cs create mode 100644 src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaConsumptionRecordAggregate/ValueObjects/MovieConsumptionRule.cs create mode 100644 src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaConsumptionRecordAggregate/ValueObjects/MusicTrackConsumptionRule.cs create mode 100644 src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaConsumptionRecordAggregate/ValueObjects/PhotoConsumptionRule.cs create mode 100644 src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaConsumptionRecordAggregate/ValueObjects/PodcastEpisodeConsumptionRule.cs create mode 100644 src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaConsumptionRecordAggregate/ValueObjects/TvEpisodeConsumptionRule.cs create mode 100644 src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaConsumptionRecordAggregate/ValueObjects/VideoConsumptionRule.cs diff --git a/src/Lumina.Application/Common/Infrastructure/Authorization/AuthorizationRoles.cs b/src/Lumina.Application/Common/Infrastructure/Authorization/AuthorizationRoles.cs new file mode 100644 index 00000000..497db7d6 --- /dev/null +++ b/src/Lumina.Application/Common/Infrastructure/Authorization/AuthorizationRoles.cs @@ -0,0 +1,12 @@ +namespace Lumina.Application.Common.Infrastructure.Authorization; + +/// +/// Class for the names of the authorization roles. +/// +public static class AuthorizationRoles +{ + /// + /// The role that grants administrative access. + /// + public const string ADMIN = "Admin"; +} diff --git a/src/Lumina.Application/Core/Admin/Authorization/Permissions/Queries/GetPermissions/GetPermissionsQueryHandler.cs b/src/Lumina.Application/Core/Admin/Authorization/Permissions/Queries/GetPermissions/GetPermissionsQueryHandler.cs index 3ea703dc..bbc0974b 100644 --- a/src/Lumina.Application/Core/Admin/Authorization/Permissions/Queries/GetPermissions/GetPermissionsQueryHandler.cs +++ b/src/Lumina.Application/Core/Admin/Authorization/Permissions/Queries/GetPermissions/GetPermissionsQueryHandler.cs @@ -57,7 +57,7 @@ public class GetPermissionsQueryHandler : IQueryHandler> getPermissionsResult = await _unitOfWork.PermissionRepository.GetAllAsync(cancellationToken: cancellationToken).ConfigureAwait(false); diff --git a/src/Lumina.Application/Core/Admin/Authorization/Roles/Commands/AddRole/AddRoleCommandHandler.cs b/src/Lumina.Application/Core/Admin/Authorization/Roles/Commands/AddRole/AddRoleCommandHandler.cs index aeeaf41e..69183552 100644 --- a/src/Lumina.Application/Core/Admin/Authorization/Roles/Commands/AddRole/AddRoleCommandHandler.cs +++ b/src/Lumina.Application/Core/Admin/Authorization/Roles/Commands/AddRole/AddRoleCommandHandler.cs @@ -1,8 +1,6 @@ #region ========================================================================= USING ===================================================================================== -using Lumina.Domain.Common.Primitives; using Lumina.Application.Common.CQRS; using Lumina.Application.Common.DataAccess.Entities.Authorization; -using Lumina.Application.Common.DataAccess.Repositories.Authorization; using Lumina.Application.Common.DataAccess.UoW; using Lumina.Application.Common.Errors; using Lumina.Application.Common.Infrastructure.Authentication; @@ -10,6 +8,7 @@ using Lumina.Application.Common.Infrastructure.Authorization; using Lumina.Application.Common.Infrastructure.Validation; using Lumina.Application.Common.Mapping.Authorization; using Lumina.Contracts.Responses.Authorization; +using Lumina.Domain.Common.Primitives; using System; using System.Collections.Generic; using System.Linq; @@ -65,7 +64,7 @@ public class AddRoleCommandHandler : ICommandHandler authorizedLibraryScans = []; // Admins can see all library scans. - if (await _authorizationService.IsInRoleAsync(userId, "Admin", cancellationToken).ConfigureAwait(false)) + if (await _authorizationService.IsInRoleAsync(userId, AuthorizationRoles.ADMIN, cancellationToken).ConfigureAwait(false)) authorizedLibraryScans.AddRange(getRunningLibraryScansResult.Value); else // For regular users, only take the library scans that belong to them. authorizedLibraryScans.AddRange(getRunningLibraryScansResult.Value.Where(libraryScan => libraryScan.UserId == userId)); diff --git a/src/Lumina.Application/Core/MediaLibrary/Management/Commands/ScanLibraries/ScanLibrariesCommandHandler.cs b/src/Lumina.Application/Core/MediaLibrary/Management/Commands/ScanLibraries/ScanLibrariesCommandHandler.cs index 139ab583..7239ca9d 100644 --- a/src/Lumina.Application/Core/MediaLibrary/Management/Commands/ScanLibraries/ScanLibrariesCommandHandler.cs +++ b/src/Lumina.Application/Core/MediaLibrary/Management/Commands/ScanLibraries/ScanLibrariesCommandHandler.cs @@ -76,7 +76,7 @@ public class ScanLibrariesCommandHandler : ICommandHandler> domainEntitiesResult = getLibrariesResult.Value.ToDomainEntities(); // If the current user is not an admin, they can only scan the libraries that belong to them. - if (!await _authorizationService.IsInRoleAsync(userId, "Admin", cancellationToken).ConfigureAwait(false)) + if (!await _authorizationService.IsInRoleAsync(userId, AuthorizationRoles.ADMIN, cancellationToken).ConfigureAwait(false)) domainEntitiesResult = domainEntitiesResult.Where(libraryResult => libraryResult.Value.UserId.Value == userId); List responses = []; diff --git a/src/Lumina.Application/Core/MediaLibrary/Management/Queries/GetEnabledLibraries/GetEnabledLibrariesQueryHandler.cs b/src/Lumina.Application/Core/MediaLibrary/Management/Queries/GetEnabledLibraries/GetEnabledLibrariesQueryHandler.cs index 1bf2803b..8c71b154 100644 --- a/src/Lumina.Application/Core/MediaLibrary/Management/Queries/GetEnabledLibraries/GetEnabledLibrariesQueryHandler.cs +++ b/src/Lumina.Application/Core/MediaLibrary/Management/Queries/GetEnabledLibraries/GetEnabledLibrariesQueryHandler.cs @@ -53,23 +53,23 @@ public class GetEnabledLibrariesQueryHandler : IQueryHandler public async Task> HandleAsync(GetEnabledLibrariesQuery query, CancellationToken cancellationToken) { - // an authenticated request must always carry a user identity + // An authenticated request must always carry a user identity. Guid? currentUserId = _currentUserService.UserId; if (currentUserId is null) return Errors.Authorization.NotAuthorized; Guid userId = currentUserId.Value; - // get the enabled libraries from the repository + // Get the enabled libraries from the repository. Result> getEnabledLibrariesResult = await _unitOfWork.LibraryRepository.GetAllEnabledAsync(cancellationToken).ConfigureAwait(false); if (getEnabledLibrariesResult.IsFailure) return getEnabledLibrariesResult.Errors; - // admins can see all libraries - if (await _authorizationService.IsInRoleAsync(userId, "Admin", cancellationToken).ConfigureAwait(false)) + // Admins can see all libraries. + if (await _authorizationService.IsInRoleAsync(userId, AuthorizationRoles.ADMIN, cancellationToken).ConfigureAwait(false)) return Result.From(getEnabledLibrariesResult.Value.Select(library => library.ToResponse()).ToArray()); else { - // for regular users, only take the libraries that belong to them + // For regular users, only take the libraries that belong to them. LibraryResponse[] userEnabledLibraries = [.. getEnabledLibrariesResult.Value .Where(library => library.UserId == userId) .Select(library => library.ToResponse())]; diff --git a/src/Lumina.Application/Core/MediaLibrary/Management/Queries/GetLibraries/GetLibrariesQueryHandler.cs b/src/Lumina.Application/Core/MediaLibrary/Management/Queries/GetLibraries/GetLibrariesQueryHandler.cs index 56b7ac87..bea35665 100644 --- a/src/Lumina.Application/Core/MediaLibrary/Management/Queries/GetLibraries/GetLibrariesQueryHandler.cs +++ b/src/Lumina.Application/Core/MediaLibrary/Management/Queries/GetLibraries/GetLibrariesQueryHandler.cs @@ -67,7 +67,7 @@ public class GetLibrariesQueryHandler : IQueryHandler library.ToResponse()).ToArray()); else { diff --git a/src/Lumina.Application/Core/MediaLibrary/Management/Queries/GetRunningLibraryScans/GetRunningLibraryScansQueryHandler.cs b/src/Lumina.Application/Core/MediaLibrary/Management/Queries/GetRunningLibraryScans/GetRunningLibraryScansQueryHandler.cs index 2a5da4c6..b87a20da 100644 --- a/src/Lumina.Application/Core/MediaLibrary/Management/Queries/GetRunningLibraryScans/GetRunningLibraryScansQueryHandler.cs +++ b/src/Lumina.Application/Core/MediaLibrary/Management/Queries/GetRunningLibraryScans/GetRunningLibraryScansQueryHandler.cs @@ -1,14 +1,13 @@ #region ========================================================================= USING ===================================================================================== -using Lumina.Domain.Common.Primitives; using Lumina.Application.Common.CQRS; using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management; -using Lumina.Application.Common.DataAccess.Repositories.MediaLibrary; using Lumina.Application.Common.DataAccess.UoW; using Lumina.Application.Common.Errors; using Lumina.Application.Common.Infrastructure.Authentication; using Lumina.Application.Common.Infrastructure.Authorization; using Lumina.Application.Common.Mapping.MediaLibrary.Management; using Lumina.Contracts.Responses.MediaLibrary.Management; +using Lumina.Domain.Common.Primitives; using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate; using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Services.Progress; using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.ValueObjects; @@ -60,26 +59,25 @@ public class GetRunningLibraryScansQueryHandler : IQueryHandler public async Task>> HandleAsync(GetRunningLibraryScansQuery query, CancellationToken cancellationToken) { - // an authenticated request must always carry a user identity + // An authenticated request must always carry a user identity. Guid? currentUserId = _currentUserService.UserId; if (currentUserId is null) return Errors.Authorization.NotAuthorized; Guid userId = currentUserId.Value; - // get the ongoing library scans from the repository + // Get the ongoing library scans from the repository. Result> getRunningScansResult = await _unitOfWork.LibraryScanRepository.GetRunningScansAsync(cancellationToken).ConfigureAwait(false); if (getRunningScansResult.IsFailure) return getRunningScansResult.Errors; - // filter the library scans by what the user is allowed to see: admins see all libraries, regular users only their own + // Filter the library scans by what the user is allowed to see: admins see all libraries, regular users only their own. LibraryScanEntity[] userRunningRepositoryLibraryScans = []; - if (await _authorizationService.IsInRoleAsync(userId, "Admin", cancellationToken).ConfigureAwait(false)) + if (await _authorizationService.IsInRoleAsync(userId, AuthorizationRoles.ADMIN, cancellationToken).ConfigureAwait(false)) userRunningRepositoryLibraryScans = [.. getRunningScansResult.Value]; else - userRunningRepositoryLibraryScans = getRunningScansResult.Value - .Where(libraryScan => libraryScan.UserId == userId).ToArray(); + userRunningRepositoryLibraryScans = [.. getRunningScansResult.Value.Where(libraryScan => libraryScan.UserId == userId)]; - // for each of the filtered library scans, get their progress + // For each of the filtered library scans, get their progress. List libraryScanProgresses = []; IEnumerable> userRunningDomainLibraryScans = userRunningRepositoryLibraryScans.ToDomainEntities(); foreach (Result userRunningDomainLibraryScan in userRunningDomainLibraryScans) diff --git a/src/Lumina.Application/Core/Plugins/Commands/InstallPlugin/InstallPluginCommandHandler.cs b/src/Lumina.Application/Core/Plugins/Commands/InstallPlugin/InstallPluginCommandHandler.cs index 9cbf250f..b6bcef73 100644 --- a/src/Lumina.Application/Core/Plugins/Commands/InstallPlugin/InstallPluginCommandHandler.cs +++ b/src/Lumina.Application/Core/Plugins/Commands/InstallPlugin/InstallPluginCommandHandler.cs @@ -62,8 +62,8 @@ public class InstallPluginCommandHandler : ICommandHandler installResult = await _pluginInstaller.InstallAsync(command.Archive!, command.FileName!, cancellationToken).ConfigureAwait(false); diff --git a/src/Lumina.Application/Core/Scheduling/Commands/AddScheduledJob/AddScheduledJobCommandHandler.cs b/src/Lumina.Application/Core/Scheduling/Commands/AddScheduledJob/AddScheduledJobCommandHandler.cs index 64cb0016..9db35861 100644 --- a/src/Lumina.Application/Core/Scheduling/Commands/AddScheduledJob/AddScheduledJobCommandHandler.cs +++ b/src/Lumina.Application/Core/Scheduling/Commands/AddScheduledJob/AddScheduledJobCommandHandler.cs @@ -78,7 +78,7 @@ public class AddScheduledJobCommandHandler : ICommandHandler getThemeResult = await _unitOfWork.ThemeRepository.GetByThemeIdAsync(command.ThemeId!, cancellationToken).ConfigureAwait(false); diff --git a/src/Lumina.Application/Core/Themes/Management/Commands/InstallTheme/InstallThemeCommandHandler.cs b/src/Lumina.Application/Core/Themes/Management/Commands/InstallTheme/InstallThemeCommandHandler.cs index 1f9e1268..ca44c670 100644 --- a/src/Lumina.Application/Core/Themes/Management/Commands/InstallTheme/InstallThemeCommandHandler.cs +++ b/src/Lumina.Application/Core/Themes/Management/Commands/InstallTheme/InstallThemeCommandHandler.cs @@ -1,8 +1,8 @@ #region ========================================================================= USING ===================================================================================== using Lumina.Application.Common.CQRS; using Lumina.Application.Common.DataAccess.Entities.Themes; -using Lumina.Application.Common.DataAccess.Repositories.Themes; using Lumina.Application.Common.DataAccess.UoW; +using Lumina.Application.Common.Errors; using Lumina.Application.Common.Infrastructure.Authentication; using Lumina.Application.Common.Infrastructure.Authorization; using Lumina.Application.Common.Infrastructure.Models.DTO.Themes; @@ -16,8 +16,6 @@ using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; -using ApplicationErrors = Lumina.Application.Common.Errors.Errors; -using DomainErrors = Lumina.Domain.Common.Errors.Errors; #endregion namespace Lumina.Application.Core.Themes.Management.Commands.InstallTheme; @@ -71,12 +69,12 @@ public class InstallThemeCommandHandler : ICommandHandler installResult = await _themeService.InstallAsync(command.Archive!, cancellationToken).ConfigureAwait(false); diff --git a/src/Lumina.Application/Core/Themes/Management/Commands/RestoreTheme/RestoreThemeCommandHandler.cs b/src/Lumina.Application/Core/Themes/Management/Commands/RestoreTheme/RestoreThemeCommandHandler.cs index ec4b1a68..e818fdc2 100644 --- a/src/Lumina.Application/Core/Themes/Management/Commands/RestoreTheme/RestoreThemeCommandHandler.cs +++ b/src/Lumina.Application/Core/Themes/Management/Commands/RestoreTheme/RestoreThemeCommandHandler.cs @@ -71,7 +71,7 @@ public class RestoreThemeCommandHandler : ICommandHandler getThemeResult = await _unitOfWork.ThemeRepository.GetByThemeIdAsync(command.ThemeId!, cancellationToken).ConfigureAwait(false); diff --git a/src/Lumina.Application/Core/Themes/Management/Commands/SetCurrentTheme/SetCurrentThemeCommandHandler.cs b/src/Lumina.Application/Core/Themes/Management/Commands/SetCurrentTheme/SetCurrentThemeCommandHandler.cs index 0fa7bb67..bda9df19 100644 --- a/src/Lumina.Application/Core/Themes/Management/Commands/SetCurrentTheme/SetCurrentThemeCommandHandler.cs +++ b/src/Lumina.Application/Core/Themes/Management/Commands/SetCurrentTheme/SetCurrentThemeCommandHandler.cs @@ -67,7 +67,7 @@ public class SetCurrentThemeCommandHandler : ICommandHandler getThemeResult = await _unitOfWork.ThemeRepository.GetByThemeIdAsync(command.ThemeId!, cancellationToken).ConfigureAwait(false); diff --git a/src/Lumina.Application/Core/UsersManagement/Authentication/Queries/GetUsers/GetUsersQueryHandler.cs b/src/Lumina.Application/Core/UsersManagement/Authentication/Queries/GetUsers/GetUsersQueryHandler.cs index 1797831b..8bc17d5f 100644 --- a/src/Lumina.Application/Core/UsersManagement/Authentication/Queries/GetUsers/GetUsersQueryHandler.cs +++ b/src/Lumina.Application/Core/UsersManagement/Authentication/Queries/GetUsers/GetUsersQueryHandler.cs @@ -1,8 +1,6 @@ #region ========================================================================= USING ===================================================================================== -using Lumina.Domain.Common.Primitives; using Lumina.Application.Common.CQRS; using Lumina.Application.Common.DataAccess.Entities.UsersManagement; -using Lumina.Application.Common.DataAccess.Repositories.Users; using Lumina.Application.Common.DataAccess.UoW; using Lumina.Application.Common.DTO.Filtering; using Lumina.Application.Common.DTO.Pagination; @@ -12,6 +10,7 @@ using Lumina.Application.Common.Infrastructure.Authorization; using Lumina.Application.Common.Mapping.Authorization; using Lumina.Application.Common.Mapping.UsersManagement.Users; using Lumina.Contracts.Responses.UsersManagement.Users; +using Lumina.Domain.Common.Primitives; using System; using System.Collections.Generic; using System.Threading; @@ -59,7 +58,7 @@ public class GetUsersQueryHandler : IQueryHandler> getRolesResult = await _unitOfWork.UserRepository.GetAllAsync(cancellationToken: cancellationToken).ConfigureAwait(false); diff --git a/src/Lumina.Application/Core/UsersManagement/Authorization/Commands/UpdateUserRoleAndPermissions/UpdateUserRoleAndPermissionsCommandHandler.cs b/src/Lumina.Application/Core/UsersManagement/Authorization/Commands/UpdateUserRoleAndPermissions/UpdateUserRoleAndPermissionsCommandHandler.cs index f53c150f..21ca9776 100644 --- a/src/Lumina.Application/Core/UsersManagement/Authorization/Commands/UpdateUserRoleAndPermissions/UpdateUserRoleAndPermissionsCommandHandler.cs +++ b/src/Lumina.Application/Core/UsersManagement/Authorization/Commands/UpdateUserRoleAndPermissions/UpdateUserRoleAndPermissionsCommandHandler.cs @@ -1,10 +1,7 @@ #region ========================================================================= USING ===================================================================================== -using Lumina.Domain.Common.Primitives; using Lumina.Application.Common.CQRS; using Lumina.Application.Common.DataAccess.Entities.Authorization; using Lumina.Application.Common.DataAccess.Entities.UsersManagement; -using Lumina.Application.Common.DataAccess.Repositories.Authorization; -using Lumina.Application.Common.DataAccess.Repositories.Users; using Lumina.Application.Common.DataAccess.UoW; using Lumina.Application.Common.DTO.Filtering; using Lumina.Application.Common.DTO.Pagination; @@ -12,6 +9,7 @@ using Lumina.Application.Common.Infrastructure.Authentication; using Lumina.Application.Common.Infrastructure.Authorization; using Lumina.Application.Common.Infrastructure.Validation; using Lumina.Contracts.Responses.Authorization; +using Lumina.Domain.Common.Primitives; using System; using System.Collections.Generic; using System.Linq; @@ -69,7 +67,7 @@ public class UpdateUserRoleAndPermissionsCommandHandler : ICommandHandler> getAllUsersResult = await _unitOfWork.UserRepository.GetAllAsync(cancellationToken: cancellationToken).ConfigureAwait(false); if (getAllUsersResult.IsFailure) return getAllUsersResult.Errors; - int adminCount = getAllUsersResult.Value.Data.Count(user => user.UserRole?.Role.RoleName == "Admin"); + int adminCount = getAllUsersResult.Value.Data.Count(user => user.UserRole?.Role.RoleName == AuthorizationRoles.ADMIN); if (adminCount <= 1) return ApplicationErrors.Authorization.CannotRemoveLastAdmin; } diff --git a/src/Lumina.Application/Core/UsersManagement/Authorization/Queries/GetAuthorization/GetAuthorizationQueryHandler.cs b/src/Lumina.Application/Core/UsersManagement/Authorization/Queries/GetAuthorization/GetAuthorizationQueryHandler.cs index b97a35ee..d448b54b 100644 --- a/src/Lumina.Application/Core/UsersManagement/Authorization/Queries/GetAuthorization/GetAuthorizationQueryHandler.cs +++ b/src/Lumina.Application/Core/UsersManagement/Authorization/Queries/GetAuthorization/GetAuthorizationQueryHandler.cs @@ -1,5 +1,4 @@ #region ========================================================================= USING ===================================================================================== -using Lumina.Domain.Common.Primitives; using Lumina.Application.Common.CQRS; using Lumina.Application.Common.DataAccess.Entities.Authorization; using Lumina.Application.Common.Errors; @@ -8,9 +7,9 @@ using Lumina.Application.Common.Infrastructure.Authorization; using Lumina.Application.Common.Infrastructure.Validation; using Lumina.Application.Common.Mapping.Authorization; using Lumina.Contracts.Responses.Authorization; +using Lumina.Domain.Common.Primitives; using System; using System.Collections.Generic; -using System.Linq; using System.Threading; using System.Threading.Tasks; #endregion @@ -53,21 +52,21 @@ public class GetAuthorizationQueryHandler : IQueryHandler 0) return validationResult; - // an authenticated request must always carry a user identity + // An authenticated request must always carry a user identity. Guid? currentUserId = _currentUserService.UserId; if (currentUserId is null) return Errors.Authorization.NotAuthorized; Guid userId = currentUserId.Value; - // first, check if the Id of the user for whom to get the permission list is different from the Id currently making the request + // First, check if the Id of the user for whom to get the permission list is different from the Id currently making the request. if (userId != query.UserId) { - // if it is, get the role of the current user, and see if they are Admin + // If it is, get the role of the current user, and see if they are Admin. Result getCurrentUserPermissionResult = await _authorizationService.GetUserAuthorizationAsync(userId, cancellationToken).ConfigureAwait(false); if (getCurrentUserPermissionResult.IsFailure) return getCurrentUserPermissionResult.Errors; - // if the current user is not an Admin, and the account for whom they request the permissions list is not theirs, deny the request - if (getCurrentUserPermissionResult.Value.Role != "Admin") + // If the current user is not an Admin, and the account for whom they request the permissions list is not theirs, deny the request. + if (getCurrentUserPermissionResult.Value.Role != AuthorizationRoles.ADMIN) return Errors.Authorization.NotAuthorized; } Result getUserPermissionResult = await _authorizationService.GetUserAuthorizationAsync(query.UserId!.Value, cancellationToken).ConfigureAwait(false); diff --git a/src/Lumina.Application/Core/UsersManagement/Authorization/Queries/GetUserPermissions/GetUserPermissionsQueryHandler.cs b/src/Lumina.Application/Core/UsersManagement/Authorization/Queries/GetUserPermissions/GetUserPermissionsQueryHandler.cs index ae8e36e1..b031c2e8 100644 --- a/src/Lumina.Application/Core/UsersManagement/Authorization/Queries/GetUserPermissions/GetUserPermissionsQueryHandler.cs +++ b/src/Lumina.Application/Core/UsersManagement/Authorization/Queries/GetUserPermissions/GetUserPermissionsQueryHandler.cs @@ -1,8 +1,6 @@ #region ========================================================================= USING ===================================================================================== -using Lumina.Domain.Common.Primitives; using Lumina.Application.Common.CQRS; using Lumina.Application.Common.DataAccess.Entities.UsersManagement; -using Lumina.Application.Common.DataAccess.Repositories.Users; using Lumina.Application.Common.DataAccess.UoW; using Lumina.Application.Common.Errors; using Lumina.Application.Common.Infrastructure.Authentication; @@ -10,6 +8,7 @@ using Lumina.Application.Common.Infrastructure.Authorization; using Lumina.Application.Common.Infrastructure.Validation; using Lumina.Application.Common.Mapping.Authorization; using Lumina.Contracts.Responses.Authorization; +using Lumina.Domain.Common.Primitives; using System; using System.Collections.Generic; using System.Linq; @@ -65,7 +64,7 @@ public class GetUserPermissionsQueryHandler : IQueryHandler> SetAdminRolePermissionsAsync(Guid userId, CancellationToken cancellationToken) { // Get the admin role. - Result getAdminRoleResult = await _unitOfWork.RoleRepository.GetByNameAsync("Admin", cancellationToken).ConfigureAwait(false); + Result getAdminRoleResult = await _unitOfWork.RoleRepository.GetByNameAsync(AuthorizationRoles.ADMIN, cancellationToken).ConfigureAwait(false); if (getAdminRoleResult.IsFailure) return getAdminRoleResult.Errors; @@ -143,7 +143,7 @@ public class DataSeedService : IDataSeedService public async Task> SetAdminRoleToAdministratorAccount(Guid userId, CancellationToken cancellationToken) { // Get the admin role. - Result getAdminRoleResult = await _unitOfWork.RoleRepository.GetByNameAsync("Admin", cancellationToken).ConfigureAwait(false); + Result getAdminRoleResult = await _unitOfWork.RoleRepository.GetByNameAsync(AuthorizationRoles.ADMIN, cancellationToken).ConfigureAwait(false); if (getAdminRoleResult.IsFailure) return getAdminRoleResult.Errors; diff --git a/src/Lumina.Domain.SharedKernel/Common/Enums/MediaConsumption/ConsumptionStatus.cs b/src/Lumina.Domain.SharedKernel/Common/Enums/MediaConsumption/ConsumptionStatus.cs new file mode 100644 index 00000000..fbb932ed --- /dev/null +++ b/src/Lumina.Domain.SharedKernel/Common/Enums/MediaConsumption/ConsumptionStatus.cs @@ -0,0 +1,22 @@ +namespace Lumina.Domain.SharedKernel.Common.Enums.MediaConsumption; + +/// +/// Enumeration for the states of a media consumption session. +/// +public enum ConsumptionStatus +{ + /// + /// The consumption session started and has not ended yet. + /// + InProgress, + + /// + /// The consumption session reached its explicit completion or the completion threshold of its library. + /// + Completed, + + /// + /// The consumption session ended before it completed. + /// + Stopped +} diff --git a/src/Lumina.Domain.SharedKernel/Common/Enums/MediaConsumption/MediaPositionKind.cs b/src/Lumina.Domain.SharedKernel/Common/Enums/MediaConsumption/MediaPositionKind.cs new file mode 100644 index 00000000..0c4dc575 --- /dev/null +++ b/src/Lumina.Domain.SharedKernel/Common/Enums/MediaConsumption/MediaPositionKind.cs @@ -0,0 +1,17 @@ +namespace Lumina.Domain.SharedKernel.Common.Enums.MediaConsumption; + +/// +/// Enumeration for the kinds of position that a media consumption session can report. +/// +public enum MediaPositionKind +{ + /// + /// The position is a time offset within the media item. + /// + TimeOffset, + + /// + /// The position is an opaque locator within the media item, such as a reading location of a book. + /// + Location +} diff --git a/src/Lumina.Domain.SharedKernel/Common/Enums/MediaLibrary/MediaKind.cs b/src/Lumina.Domain.SharedKernel/Common/Enums/MediaLibrary/MediaKind.cs new file mode 100644 index 00000000..cfee4790 --- /dev/null +++ b/src/Lumina.Domain.SharedKernel/Common/Enums/MediaLibrary/MediaKind.cs @@ -0,0 +1,52 @@ +namespace Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary; + +/// +/// Enumeration for the kinds of media items. +/// +public enum MediaKind +{ + /// + /// No media kind. Used as the default value; it is not a valid kind for a media item. + /// + None = 0, + + /// + /// A track of a music library. + /// + MusicTrack, + + /// + /// An episode of a podcast. + /// + PodcastEpisode, + + /// + /// An audiobook. + /// + Audiobook, + + /// + /// A movie. + /// + Movie, + + /// + /// An episode of a TV show. + /// + TvEpisode, + + /// + /// A book of a written content library. + /// + Book, + + /// + /// A photo. + /// + Photo, + + /// + /// A video file. + /// + Video +} diff --git a/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/Common/ValueObjects/DurationThresholdConsumptionRule.cs b/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/Common/ValueObjects/DurationThresholdConsumptionRule.cs new file mode 100644 index 00000000..c1cad270 --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/Common/ValueObjects/DurationThresholdConsumptionRule.cs @@ -0,0 +1,29 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Domain.SharedKernel.Common.Enums.MediaConsumption; +using System; +#endregion + +namespace Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.Common.ValueObjects; + +/// +/// Value Object for the consumption rules of a kind of media whose sessions count when they are completed or reach a minimum consumed duration. I.e.: movies. +/// +public abstract class DurationThresholdConsumptionRule : MediaConsumptionRule +{ + /// + /// Gets the minimum consumed duration that a session of this kind of media must reach to be counted, when it is not completed. + /// + public abstract TimeSpan MinimumCountedDuration { get; } + + /// + /// Determines whether a consumption session of this kind of media counts towards statistics. + /// + /// The status of the consumption session. + /// The active duration consumed by the consumption session. + /// The number of consecutive pages read in the consumption session. It is ignored, because this kind of media is counted by duration. + /// if the consumption session counts towards statistics, otherwise. + public override bool Counts(ConsumptionStatus status, TimeSpan consumedDuration, int consecutivePagesRead) + { + return status == ConsumptionStatus.Completed || consumedDuration >= MinimumCountedDuration; + } +} diff --git a/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/Common/ValueObjects/EverySessionConsumptionRule.cs b/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/Common/ValueObjects/EverySessionConsumptionRule.cs new file mode 100644 index 00000000..75aa857a --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/Common/ValueObjects/EverySessionConsumptionRule.cs @@ -0,0 +1,24 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Domain.SharedKernel.Common.Enums.MediaConsumption; +using System; +#endregion + +namespace Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.Common.ValueObjects; + +/// +/// Value Object for the consumption rules of a kind of media whose sessions count every time, regardless of their duration or progress. I.e.: photos. +/// +public abstract class EverySessionConsumptionRule : MediaConsumptionRule +{ + /// + /// Determines whether a consumption session of this kind of media counts towards statistics. + /// + /// The status of the consumption session. It is ignored, because every session of this kind of media counts. + /// The active duration consumed by the consumption session. It is ignored, because every session of this kind of media counts. + /// The number of consecutive pages read in the consumption session. It is ignored, because every session of this kind of media counts. + /// , because every session of this kind of media counts towards statistics. + public override bool Counts(ConsumptionStatus status, TimeSpan consumedDuration, int consecutivePagesRead) + { + return true; + } +} diff --git a/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/Common/ValueObjects/MediaConsumptionRule.cs b/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/Common/ValueObjects/MediaConsumptionRule.cs new file mode 100644 index 00000000..3e7c950a --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/Common/ValueObjects/MediaConsumptionRule.cs @@ -0,0 +1,83 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Domain.Common.Models.Core; +using Lumina.Domain.Common.Primitives; +using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.MediaConsumptionRecordAggregate.ValueObjects; +using Lumina.Domain.SharedKernel.Common.Enums.MediaConsumption; +using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary; +using System; +using System.Collections.Generic; +#endregion + +namespace Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.Common.ValueObjects; + +/// +/// Value Object for the consumption rules of a kind of media. +/// +/// +/// The rules are shared by the consumption record aggregate, the resume point aggregate and the statistics read model, so they live in the common +/// folder of the bounded context. Each concrete rule describes the counting behavior and the resumability of one media kind, and the factory returns +/// the rule for a kind so that callers never switch on the media kind themselves. +/// +public abstract class MediaConsumptionRule : ValueObject +{ + /// + /// Gets the kind of media that this rule applies to. + /// + public abstract MediaKind MediaKind { get; } + + /// + /// Gets a value indicating whether the current catalogues support this kind of media. + /// + public abstract bool IsSupportedByCurrentCatalogues { get; } + + /// + /// Gets the kind of position used to resume this kind of media, if the kind can be resumed. + /// + public abstract Optional PositionKind { get; } + + /// + /// Gets a value indicating whether a consumption session of this kind of media can be resumed. + /// + public bool CanResume => PositionKind.HasValue; + + /// + /// Determines whether a consumption session of this kind of media counts towards statistics. + /// + /// The status of the consumption session. + /// The active duration consumed by the consumption session. + /// The number of consecutive pages read in the consumption session. + /// if the consumption session counts towards statistics, otherwise. + public abstract bool Counts(ConsumptionStatus status, TimeSpan consumedDuration, int consecutivePagesRead); + + /// + /// Gets the consumption rule for . + /// + /// The kind of media for which to get the consumption rule. + /// + /// An containing the consumption rule for , or no value when the kind has no rule. + /// + public static Optional For(MediaKind mediaKind) + { + return mediaKind switch + { + MediaKind.MusicTrack => Optional.Some(new MusicTrackConsumptionRule()), + MediaKind.PodcastEpisode => Optional.Some(new PodcastEpisodeConsumptionRule()), + MediaKind.Audiobook => Optional.Some(new AudiobookConsumptionRule()), + MediaKind.Movie => Optional.Some(new MovieConsumptionRule()), + MediaKind.TvEpisode => Optional.Some(new TvEpisodeConsumptionRule()), + MediaKind.Book => Optional.Some(new BookConsumptionRule()), + MediaKind.Photo => Optional.Some(new PhotoConsumptionRule()), + MediaKind.Video => Optional.Some(new VideoConsumptionRule()), + _ => Optional.None() + }; + } + + /// + /// Gets the list of items that define equality of the object. + /// + /// A list of items defining the equality. + public override IEnumerable GetEqualityComponents() + { + yield return MediaKind; + } +} diff --git a/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/Common/ValueObjects/PageThresholdConsumptionRule.cs b/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/Common/ValueObjects/PageThresholdConsumptionRule.cs new file mode 100644 index 00000000..2c79edf6 --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/Common/ValueObjects/PageThresholdConsumptionRule.cs @@ -0,0 +1,29 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Domain.SharedKernel.Common.Enums.MediaConsumption; +using System; +#endregion + +namespace Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.Common.ValueObjects; + +/// +/// Value Object for the consumption rules of a kind of media whose sessions count when they are completed or reach a minimum number of consecutive pages read. I.e.: books. +/// +public abstract class PageThresholdConsumptionRule : MediaConsumptionRule +{ + /// + /// Gets the minimum number of consecutive pages that a session of this kind of media must read to be counted, when it is not completed. + /// + public abstract int MinimumConsecutivePages { get; } + + /// + /// Determines whether a consumption session of this kind of media counts towards statistics. + /// + /// The status of the consumption session. + /// The active duration consumed by the consumption session. It is ignored, because this kind of media is counted by pages. + /// The number of consecutive pages read in the consumption session. + /// if the consumption session counts towards statistics, otherwise. + public override bool Counts(ConsumptionStatus status, TimeSpan consumedDuration, int consecutivePagesRead) + { + return status == ConsumptionStatus.Completed || consecutivePagesRead > MinimumConsecutivePages; + } +} diff --git a/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/ExternalIdentifiers/LibraryManagementBoundedContext/LibraryAggregate/LibraryId.cs b/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/ExternalIdentifiers/LibraryManagementBoundedContext/LibraryAggregate/LibraryId.cs new file mode 100644 index 00000000..0cc2d6b7 --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/ExternalIdentifiers/LibraryManagementBoundedContext/LibraryAggregate/LibraryId.cs @@ -0,0 +1,51 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Domain.Common.Models.Core; +using System; +using System.Collections.Generic; +using System.Diagnostics; +#endregion + +namespace Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.ExternalIdentifiers.LibraryManagementBoundedContext.LibraryAggregate; + +/// +/// Value Object for the Id of a media library, duplicated from the LibraryManagement bounded context to keep the cross bounded context references decoupled. +/// +[DebuggerDisplay("{Value}")] +public sealed class LibraryId : EntityId +{ + /// + /// Initializes a new instance of the class. + /// + /// The value representing this object. + private LibraryId(Guid value) : base(value) + { + } + + /// + /// Creates a new instance of the class. + /// + /// The created instance. + public static LibraryId CreateUnique() + { + return new LibraryId(Guid.NewGuid()); + } + + /// + /// Creates a new instance of the class, from a pre-existing . + /// + /// The value used to create the instance. + /// The created instance. + public static LibraryId Create(Guid value) + { + return new LibraryId(value); + } + + /// + /// Gets the list of items that define equality of the object. + /// + /// A list of items defining the equality. + public override IEnumerable GetEqualityComponents() + { + yield return Value; + } +} diff --git a/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/ExternalIdentifiers/MediaLibraryBoundedContext/MediaItemAggregate/MediaItemId.cs b/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/ExternalIdentifiers/MediaLibraryBoundedContext/MediaItemAggregate/MediaItemId.cs new file mode 100644 index 00000000..6232478f --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/ExternalIdentifiers/MediaLibraryBoundedContext/MediaItemAggregate/MediaItemId.cs @@ -0,0 +1,51 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Domain.Common.Models.Core; +using System; +using System.Collections.Generic; +using System.Diagnostics; +#endregion + +namespace Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.ExternalIdentifiers.MediaLibraryBoundedContext.MediaItemAggregate; + +/// +/// Value Object for the Id of a media item, duplicated from the media library bounded contexts to keep the cross bounded context references decoupled. +/// +[DebuggerDisplay("{Value}")] +public sealed class MediaItemId : EntityId +{ + /// + /// Initializes a new instance of the class. + /// + /// The value representing this object. + private MediaItemId(Guid value) : base(value) + { + } + + /// + /// Creates a new instance of the class. + /// + /// The created instance. + public static MediaItemId CreateUnique() + { + return new MediaItemId(Guid.NewGuid()); + } + + /// + /// Creates a new instance of the class, from a pre-existing . + /// + /// The value used to create the instance. + /// The created instance. + public static MediaItemId Create(Guid value) + { + return new MediaItemId(value); + } + + /// + /// Gets the list of items that define equality of the object. + /// + /// A list of items defining the equality. + public override IEnumerable GetEqualityComponents() + { + yield return Value; + } +} diff --git a/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/ExternalIdentifiers/UserManagementBoundedContext/UserAggregate/UserId.cs b/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/ExternalIdentifiers/UserManagementBoundedContext/UserAggregate/UserId.cs new file mode 100644 index 00000000..1237c088 --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/ExternalIdentifiers/UserManagementBoundedContext/UserAggregate/UserId.cs @@ -0,0 +1,51 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Domain.Common.Models.Core; +using System; +using System.Collections.Generic; +using System.Diagnostics; +#endregion + +namespace Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.ExternalIdentifiers.UserManagementBoundedContext.UserAggregate; + +/// +/// Value Object for the Id of a user, duplicated from the UserManagement bounded context to keep the cross bounded context references decoupled. +/// +[DebuggerDisplay("{Value}")] +public sealed class UserId : EntityId +{ + /// + /// Initializes a new instance of the class. + /// + /// The value representing this object. + private UserId(Guid value) : base(value) + { + } + + /// + /// Creates a new instance of the class. + /// + /// The created instance. + public static UserId CreateUnique() + { + return new UserId(Guid.NewGuid()); + } + + /// + /// Creates a new instance of the class, from a pre-existing . + /// + /// The value used to create the instance. + /// The created instance. + public static UserId Create(Guid value) + { + return new UserId(value); + } + + /// + /// Gets the list of items that define equality of the object. + /// + /// A list of items defining the equality. + public override IEnumerable GetEqualityComponents() + { + yield return Value; + } +} diff --git a/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaConsumptionRecordAggregate/ValueObjects/AudiobookConsumptionRule.cs b/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaConsumptionRecordAggregate/ValueObjects/AudiobookConsumptionRule.cs new file mode 100644 index 00000000..c43bd5ff --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaConsumptionRecordAggregate/ValueObjects/AudiobookConsumptionRule.cs @@ -0,0 +1,35 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Domain.Common.Primitives; +using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.Common.ValueObjects; +using Lumina.Domain.SharedKernel.Common.Enums.MediaConsumption; +using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary; +using System; +#endregion + +namespace Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.MediaConsumptionRecordAggregate.ValueObjects; + +/// +/// Value Object for the consumption rules of an audiobook. +/// +public sealed class AudiobookConsumptionRule : DurationThresholdConsumptionRule +{ + /// + /// Gets the kind of media that this rule applies to. + /// + public override MediaKind MediaKind { get; } = MediaKind.Audiobook; + + /// + /// Gets a value indicating whether the current catalogues support this kind of media. + /// + public override bool IsSupportedByCurrentCatalogues { get; } = false; + + /// + /// Gets the kind of position used to resume this kind of media, if the kind can be resumed. + /// + public override Optional PositionKind { get; } = MediaPositionKind.TimeOffset; + + /// + /// Gets the minimum consumed duration that a session of this kind of media must reach to be counted, when it is not completed. + /// + public override TimeSpan MinimumCountedDuration { get; } = TimeSpan.FromSeconds(60); +} diff --git a/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaConsumptionRecordAggregate/ValueObjects/BookConsumptionRule.cs b/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaConsumptionRecordAggregate/ValueObjects/BookConsumptionRule.cs new file mode 100644 index 00000000..648831f9 --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaConsumptionRecordAggregate/ValueObjects/BookConsumptionRule.cs @@ -0,0 +1,34 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Domain.Common.Primitives; +using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.Common.ValueObjects; +using Lumina.Domain.SharedKernel.Common.Enums.MediaConsumption; +using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary; +#endregion + +namespace Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.MediaConsumptionRecordAggregate.ValueObjects; + +/// +/// Value Object for the consumption rules of a book. +/// +public sealed class BookConsumptionRule : PageThresholdConsumptionRule +{ + /// + /// Gets the kind of media that this rule applies to. + /// + public override MediaKind MediaKind { get; } = MediaKind.Book; + + /// + /// Gets a value indicating whether the current catalogues support this kind of media. + /// + public override bool IsSupportedByCurrentCatalogues { get; } = true; + + /// + /// Gets the kind of position used to resume this kind of media, if the kind can be resumed. + /// + public override Optional PositionKind { get; } = MediaPositionKind.Location; + + /// + /// Gets the minimum number of consecutive pages that a session of this kind of media must read to be counted, when it is not completed. + /// + public override int MinimumConsecutivePages { get; } = 5; +} diff --git a/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaConsumptionRecordAggregate/ValueObjects/MovieConsumptionRule.cs b/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaConsumptionRecordAggregate/ValueObjects/MovieConsumptionRule.cs new file mode 100644 index 00000000..eb6a1ee8 --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaConsumptionRecordAggregate/ValueObjects/MovieConsumptionRule.cs @@ -0,0 +1,35 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Domain.Common.Primitives; +using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.Common.ValueObjects; +using Lumina.Domain.SharedKernel.Common.Enums.MediaConsumption; +using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary; +using System; +#endregion + +namespace Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.MediaConsumptionRecordAggregate.ValueObjects; + +/// +/// Value Object for the consumption rules of a movie. +/// +public sealed class MovieConsumptionRule : DurationThresholdConsumptionRule +{ + /// + /// Gets the kind of media that this rule applies to. + /// + public override MediaKind MediaKind { get; } = MediaKind.Movie; + + /// + /// Gets a value indicating whether the current catalogues support this kind of media. + /// + public override bool IsSupportedByCurrentCatalogues { get; } = false; + + /// + /// Gets the kind of position used to resume this kind of media, if the kind can be resumed. + /// + public override Optional PositionKind { get; } = MediaPositionKind.TimeOffset; + + /// + /// Gets the minimum consumed duration that a session of this kind of media must reach to be counted, when it is not completed. + /// + public override TimeSpan MinimumCountedDuration { get; } = TimeSpan.FromMinutes(3); +} diff --git a/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaConsumptionRecordAggregate/ValueObjects/MusicTrackConsumptionRule.cs b/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaConsumptionRecordAggregate/ValueObjects/MusicTrackConsumptionRule.cs new file mode 100644 index 00000000..ec319036 --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaConsumptionRecordAggregate/ValueObjects/MusicTrackConsumptionRule.cs @@ -0,0 +1,35 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Domain.Common.Primitives; +using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.Common.ValueObjects; +using Lumina.Domain.SharedKernel.Common.Enums.MediaConsumption; +using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary; +using System; +#endregion + +namespace Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.MediaConsumptionRecordAggregate.ValueObjects; + +/// +/// Value Object for the consumption rules of a music track. +/// +public sealed class MusicTrackConsumptionRule : DurationThresholdConsumptionRule +{ + /// + /// Gets the kind of media that this rule applies to. + /// + public override MediaKind MediaKind { get; } = MediaKind.MusicTrack; + + /// + /// Gets a value indicating whether the current catalogues support this kind of media. + /// + public override bool IsSupportedByCurrentCatalogues { get; } = true; + + /// + /// Gets the kind of position used to resume this kind of media, if the kind can be resumed. + /// + public override Optional PositionKind { get; } = MediaPositionKind.TimeOffset; + + /// + /// Gets the minimum consumed duration that a session of this kind of media must reach to be counted, when it is not completed. + /// + public override TimeSpan MinimumCountedDuration { get; } = TimeSpan.FromSeconds(30); +} diff --git a/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaConsumptionRecordAggregate/ValueObjects/PhotoConsumptionRule.cs b/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaConsumptionRecordAggregate/ValueObjects/PhotoConsumptionRule.cs new file mode 100644 index 00000000..3eee03d3 --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaConsumptionRecordAggregate/ValueObjects/PhotoConsumptionRule.cs @@ -0,0 +1,29 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Domain.Common.Primitives; +using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.Common.ValueObjects; +using Lumina.Domain.SharedKernel.Common.Enums.MediaConsumption; +using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary; +#endregion + +namespace Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.MediaConsumptionRecordAggregate.ValueObjects; + +/// +/// Value Object for the consumption rules of a photo. +/// +public sealed class PhotoConsumptionRule : EverySessionConsumptionRule +{ + /// + /// Gets the kind of media that this rule applies to. + /// + public override MediaKind MediaKind { get; } = MediaKind.Photo; + + /// + /// Gets a value indicating whether the current catalogues support this kind of media. + /// + public override bool IsSupportedByCurrentCatalogues { get; } = false; + + /// + /// Gets the kind of position used to resume this kind of media. A photo cannot be resumed, so it has no position kind. + /// + public override Optional PositionKind { get; } = Optional.None(); +} diff --git a/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaConsumptionRecordAggregate/ValueObjects/PodcastEpisodeConsumptionRule.cs b/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaConsumptionRecordAggregate/ValueObjects/PodcastEpisodeConsumptionRule.cs new file mode 100644 index 00000000..257f480a --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaConsumptionRecordAggregate/ValueObjects/PodcastEpisodeConsumptionRule.cs @@ -0,0 +1,35 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Domain.Common.Primitives; +using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.Common.ValueObjects; +using Lumina.Domain.SharedKernel.Common.Enums.MediaConsumption; +using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary; +using System; +#endregion + +namespace Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.MediaConsumptionRecordAggregate.ValueObjects; + +/// +/// Value Object for the consumption rules of a podcast episode. +/// +public sealed class PodcastEpisodeConsumptionRule : DurationThresholdConsumptionRule +{ + /// + /// Gets the kind of media that this rule applies to. + /// + public override MediaKind MediaKind { get; } = MediaKind.PodcastEpisode; + + /// + /// Gets a value indicating whether the current catalogues support this kind of media. + /// + public override bool IsSupportedByCurrentCatalogues { get; } = false; + + /// + /// Gets the kind of position used to resume this kind of media, if the kind can be resumed. + /// + public override Optional PositionKind { get; } = MediaPositionKind.TimeOffset; + + /// + /// Gets the minimum consumed duration that a session of this kind of media must reach to be counted, when it is not completed. + /// + public override TimeSpan MinimumCountedDuration { get; } = TimeSpan.FromSeconds(30); +} diff --git a/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaConsumptionRecordAggregate/ValueObjects/TvEpisodeConsumptionRule.cs b/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaConsumptionRecordAggregate/ValueObjects/TvEpisodeConsumptionRule.cs new file mode 100644 index 00000000..e3193902 --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaConsumptionRecordAggregate/ValueObjects/TvEpisodeConsumptionRule.cs @@ -0,0 +1,35 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Domain.Common.Primitives; +using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.Common.ValueObjects; +using Lumina.Domain.SharedKernel.Common.Enums.MediaConsumption; +using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary; +using System; +#endregion + +namespace Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.MediaConsumptionRecordAggregate.ValueObjects; + +/// +/// Value Object for the consumption rules of a TV show episode. +/// +public sealed class TvEpisodeConsumptionRule : DurationThresholdConsumptionRule +{ + /// + /// Gets the kind of media that this rule applies to. + /// + public override MediaKind MediaKind { get; } = MediaKind.TvEpisode; + + /// + /// Gets a value indicating whether the current catalogues support this kind of media. + /// + public override bool IsSupportedByCurrentCatalogues { get; } = false; + + /// + /// Gets the kind of position used to resume this kind of media, if the kind can be resumed. + /// + public override Optional PositionKind { get; } = MediaPositionKind.TimeOffset; + + /// + /// Gets the minimum consumed duration that a session of this kind of media must reach to be counted, when it is not completed. + /// + public override TimeSpan MinimumCountedDuration { get; } = TimeSpan.FromMinutes(3); +} diff --git a/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaConsumptionRecordAggregate/ValueObjects/VideoConsumptionRule.cs b/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaConsumptionRecordAggregate/ValueObjects/VideoConsumptionRule.cs new file mode 100644 index 00000000..62b1cdc3 --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/MediaConsumptionBoundedContext/MediaConsumptionRecordAggregate/ValueObjects/VideoConsumptionRule.cs @@ -0,0 +1,35 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Domain.Common.Primitives; +using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.Common.ValueObjects; +using Lumina.Domain.SharedKernel.Common.Enums.MediaConsumption; +using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary; +using System; +#endregion + +namespace Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.MediaConsumptionRecordAggregate.ValueObjects; + +/// +/// Value Object for the consumption rules of a video. +/// +public sealed class VideoConsumptionRule : DurationThresholdConsumptionRule +{ + /// + /// Gets the kind of media that this rule applies to. + /// + public override MediaKind MediaKind { get; } = MediaKind.Video; + + /// + /// Gets a value indicating whether the current catalogues support this kind of media. + /// + public override bool IsSupportedByCurrentCatalogues { get; } = false; + + /// + /// Gets the kind of position used to resume this kind of media, if the kind can be resumed. + /// + public override Optional PositionKind { get; } = MediaPositionKind.TimeOffset; + + /// + /// Gets the minimum consumed duration that a session of this kind of media must reach to be counted, when it is not completed. + /// + public override TimeSpan MinimumCountedDuration { get; } = TimeSpan.FromMinutes(3); +} diff --git a/src/Lumina.Infrastructure/Core/Authorization/Policies/LibraryOwnership/LibraryOwnershipPolicy.cs b/src/Lumina.Infrastructure/Core/Authorization/Policies/LibraryOwnership/LibraryOwnershipPolicy.cs index 72c402be..5dfec779 100644 --- a/src/Lumina.Infrastructure/Core/Authorization/Policies/LibraryOwnership/LibraryOwnershipPolicy.cs +++ b/src/Lumina.Infrastructure/Core/Authorization/Policies/LibraryOwnership/LibraryOwnershipPolicy.cs @@ -1,6 +1,7 @@ #region ========================================================================= USING ===================================================================================== using Lumina.Application.Common.DataAccess.Entities.UsersManagement; using Lumina.Application.Common.DataAccess.UoW; +using Lumina.Application.Common.Infrastructure.Authorization; using Lumina.Application.Common.Infrastructure.Authorization.Policies.Common.Base; using Lumina.Application.Common.Infrastructure.Authorization.Policies.LibraryOwnership; using Lumina.Domain.Common.Primitives; @@ -57,7 +58,7 @@ public class LibraryOwnershipPolicy : ILibraryOwnershipPolicy return false; } // Admins can access any media library. - _canAccessAllLibraries = getUserResult.Value.UserRole?.Role.RoleName == "Admin"; + _canAccessAllLibraries = getUserResult.Value.UserRole?.Role.RoleName == AuthorizationRoles.ADMIN; // The user graph already carries the libraries owned by this user, so answering the policy needs no second library read. _ownedLibraryIds = [.. getUserResult.Value.Libraries.Select(library => library.Id)]; _loadedUserId = userId; diff --git a/src/Lumina.Infrastructure/Core/Scheduling/Notifications/ScheduledJobsHub.cs b/src/Lumina.Infrastructure/Core/Scheduling/Notifications/ScheduledJobsHub.cs index 5d498ecb..491e0c38 100644 --- a/src/Lumina.Infrastructure/Core/Scheduling/Notifications/ScheduledJobsHub.cs +++ b/src/Lumina.Infrastructure/Core/Scheduling/Notifications/ScheduledJobsHub.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.SignalR; using Microsoft.Extensions.DependencyInjection; +using Lumina.Application.Common.Infrastructure.Authorization; using System; using System.Security.Claims; using System.Threading; @@ -60,6 +61,6 @@ public class ScheduledJobsHub : Hub { await using AsyncServiceScope scope = _serviceScopeFactory.CreateAsyncScope(); ApplicationAuthorizationService authorizationService = scope.ServiceProvider.GetRequiredService(); - return await authorizationService.IsInRoleAsync(userId, "Admin", CancellationToken.None).ConfigureAwait(false); + return await authorizationService.IsInRoleAsync(userId, AuthorizationRoles.ADMIN, CancellationToken.None).ConfigureAwait(false); } } diff --git a/src/Lumina.Presentation.Api/Core/Endpoints/Admin/Authorization/Roles/GetRoles/GetRolesEndpointSummary.cs b/src/Lumina.Presentation.Api/Core/Endpoints/Admin/Authorization/Roles/GetRoles/GetRolesEndpointSummary.cs index 54b5b52d..2fbfc6de 100644 --- a/src/Lumina.Presentation.Api/Core/Endpoints/Admin/Authorization/Roles/GetRoles/GetRolesEndpointSummary.cs +++ b/src/Lumina.Presentation.Api/Core/Endpoints/Admin/Authorization/Roles/GetRoles/GetRolesEndpointSummary.cs @@ -1,5 +1,6 @@ #region ========================================================================= USING ===================================================================================== using FastEndpoints; +using Lumina.Application.Common.Infrastructure.Authorization; using Lumina.Contracts.Responses.Authorization; using System; using System.Diagnostics.CodeAnalysis; @@ -23,7 +24,7 @@ public class GetRolesEndpointSummary : Summary Response(200, "The authorization roles are returned.", example: new[] { - new RoleResponse(Id: Guid.NewGuid(), "Admin") + new RoleResponse(Id: Guid.NewGuid(), AuthorizationRoles.ADMIN) }); Response(401, "Authentication required.", "application/problem+json", diff --git a/src/Lumina.Presentation.Api/Core/Endpoints/UsersManagement/Authorization/GetAuthorization/GetAuthorizationEndpointSummary.cs b/src/Lumina.Presentation.Api/Core/Endpoints/UsersManagement/Authorization/GetAuthorization/GetAuthorizationEndpointSummary.cs index 062e5e36..b07ca65b 100644 --- a/src/Lumina.Presentation.Api/Core/Endpoints/UsersManagement/Authorization/GetAuthorization/GetAuthorizationEndpointSummary.cs +++ b/src/Lumina.Presentation.Api/Core/Endpoints/UsersManagement/Authorization/GetAuthorization/GetAuthorizationEndpointSummary.cs @@ -1,5 +1,6 @@ #region ========================================================================= USING ===================================================================================== using FastEndpoints; +using Lumina.Application.Common.Infrastructure.Authorization; using Lumina.Contracts.Requests.Authorization; using Lumina.Contracts.Responses.Authorization; using Lumina.Domain.SharedKernel.Common.Enums.Authorization; @@ -33,7 +34,7 @@ public class GetAuthorizationEndpointSummary : Summary() { AuthorizationPermission.CanRegisterUsers, AuthorizationPermission.CanDeleteUsers } )); diff --git a/src/Lumina.Presentation.Api/Core/Endpoints/UsersManagement/Authorization/GetUserRole/GetUserRoleEndpointSummary.cs b/src/Lumina.Presentation.Api/Core/Endpoints/UsersManagement/Authorization/GetUserRole/GetUserRoleEndpointSummary.cs index 9761783b..49a54a90 100644 --- a/src/Lumina.Presentation.Api/Core/Endpoints/UsersManagement/Authorization/GetUserRole/GetUserRoleEndpointSummary.cs +++ b/src/Lumina.Presentation.Api/Core/Endpoints/UsersManagement/Authorization/GetUserRole/GetUserRoleEndpointSummary.cs @@ -1,5 +1,6 @@ #region ========================================================================= USING ===================================================================================== using FastEndpoints; +using Lumina.Application.Common.Infrastructure.Authorization; using Lumina.Contracts.Requests.Authorization; using Lumina.Contracts.Responses.Authorization; using Lumina.Domain.SharedKernel.Common.Enums.Authorization; @@ -33,7 +34,7 @@ public class GetUserRoleEndpointSummary : Summary