mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 15:11:10 +00:00
Adds library ownership authorization policy
This commit is contained in:
1 parent
65a630155d
commit
a7dfcbe926
23 files changed
+645
-202
No files matched your search
@@ -75,17 +75,18 @@ public class AuthorizationService : IAuthorizationService
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Evaluates whether the specified user meets the conditions defined in the specified authorization policy.
|
||||
/// Evaluates whether the specified user meets the conditions defined in the specified authorization policy, against the resource described by <paramref name="context"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="TAuthorizationPolicy">The type of authorization policy to evaluate.</typeparam>
|
||||
/// <param name="userId">The unique identifier of the user for whom to evaluate the policy.</param>
|
||||
/// <param name="context">The context describing the resource against which the policy is evaluated. Can be <see langword="null"/> for policies that do not require a resource.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns><see langword="true"/> if the user satisfies the policy, <see langword="false"/> otherwise.</returns>
|
||||
public async Task<bool> EvaluatePolicyAsync<TAuthorizationPolicy>(Guid userId, CancellationToken cancellationToken) where TAuthorizationPolicy : IAuthorizationPolicy
|
||||
public async Task<bool> EvaluatePolicyAsync<TAuthorizationPolicy>(Guid userId, PolicyContext? context, CancellationToken cancellationToken) where TAuthorizationPolicy : IAuthorizationPolicy
|
||||
{
|
||||
// resolve the authorization policy dynamically using the factory
|
||||
IAuthorizationPolicy policy = _authorizationPolicyFactory.CreatePolicy<TAuthorizationPolicy>();
|
||||
return await policy.EvaluateAsync(userId, cancellationToken).ConfigureAwait(false);
|
||||
return await policy.EvaluateAsync(userId, context, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
|
||||
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.Infrastructure.Authorization.Policies.Common.Base;
|
||||
using Lumina.Application.Common.Infrastructure.Authorization.Policies.LibraryOwnership;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Infrastructure.Core.Authorization.Policies.LibraryOwnership;
|
||||
|
||||
/// <summary>
|
||||
/// Authorization policy that checks whether a user can access a media library: admins can access any library, while other users can only access the libraries they own.
|
||||
/// </summary>
|
||||
public class LibraryOwnershipPolicy : ILibraryOwnershipPolicy
|
||||
{
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="LibraryOwnershipPolicy"/> class.
|
||||
/// </summary>
|
||||
/// <param name="unitOfWork">Injected unit of work for interacting with the data access layer repositories.</param>
|
||||
public LibraryOwnershipPolicy(IUnitOfWork unitOfWork)
|
||||
{
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Evaluates the policy for the user identified by <paramref name="userId"/> against the media library 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 media library whose access is being evaluated.</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 owns the media library, <see langword="false"/> otherwise.</returns>
|
||||
public async Task<bool> EvaluateAsync(Guid userId, PolicyContext? context, CancellationToken cancellationToken)
|
||||
{
|
||||
if (context is not LibraryOwnershipPolicyContext libraryContext)
|
||||
return false;
|
||||
|
||||
// admins can access any media library
|
||||
Result<UserEntity?> getUserResult = await _unitOfWork.UserRepository.GetByIdAsync(userId, cancellationToken).ConfigureAwait(false);
|
||||
if (getUserResult.IsFailure || getUserResult.Value is null)
|
||||
return false;
|
||||
if (getUserResult.Value.UserRole?.Role.RoleName == "Admin")
|
||||
return true;
|
||||
|
||||
// regular users can only access the media libraries they own
|
||||
Result<LibraryEntity?> getLibraryResult = await _unitOfWork.LibraryRepository.GetByIdAsync(libraryContext.LibraryId, cancellationToken).ConfigureAwait(false);
|
||||
if (getLibraryResult.IsFailure || getLibraryResult.Value is null)
|
||||
return false;
|
||||
return getLibraryResult.Value.UserId == userId;
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Users;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.Infrastructure.Authorization.Policies.Common.Base;
|
||||
using Lumina.Application.Common.Infrastructure.Authorization.Policies.Over18;
|
||||
using Lumina.Application.Common.Infrastructure.Time;
|
||||
using System;
|
||||
@@ -32,12 +33,13 @@ public class Over18Policy : IOver18Policy
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Evaluates the policy for the user identified by <paramref name="userId"/>.
|
||||
/// Evaluates the policy for the user identified by <paramref name="userId"/>. The <paramref name="context"/> is not used by this policy.
|
||||
/// </summary>
|
||||
/// <param name="userId">The unique identifier of the user for which to evaluate the policy.</param>
|
||||
/// <param name="context">The context describing the resource against which the policy is evaluated. Not used by this policy.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns><see langword="true"/> if the policy evaluation succeeds, <see langword="false"/> otherwise.</returns>
|
||||
public async Task<bool> EvaluateAsync(Guid userId, CancellationToken cancellationToken)
|
||||
public async Task<bool> EvaluateAsync(Guid userId, PolicyContext? context, CancellationToken cancellationToken)
|
||||
{
|
||||
Result<UserEntity?> getUserResult = await _unitOfWork.UserRepository.GetByIdAsync(userId, cancellationToken).ConfigureAwait(false);
|
||||
if (getUserResult.IsFailure || getUserResult.Value is null)
|
||||
|
||||
Reference in new issue
Block a user