classlib/ExpenseTracker.Application/Accounts/Commands/Update/UpdateAccountCommandAuthorized.cs
2024-08-07 21:12:02 +03:00

33 lines
1.1 KiB
C#

using MediatR.Behaviors.Authorization;
using ExpenseTracker.Application.Common.Authorization;
using ExpenseTracker.Application.Common.Interfaces.Services;
using ExpenseTracker.Application.Common.Models;
namespace ExpenseTracker.Application.Accounts.Commands.Update;
public class UpdateAccountCommandAuthorizer : AbstractRequestAuthorizer<UpdateAccountCommand>
{
private readonly ISessionUserService _sessionUserService;
public UpdateAccountCommandAuthorizer(ISessionUserService currentUserService)
{
_sessionUserService = currentUserService;
}
public override void BuildPolicy(UpdateAccountCommand request)
{
UseRequirement(new MustBeAuthenticatedRequirement
{
IsAuthenticated = _sessionUserService.IsAuthenticated
});
UseRequirement(new MustBeInRolesWhenInteractingWithUnOwnedEntityRequirement
{
UserId = _sessionUserService.Id,
UserRoles = _sessionUserService.Roles,
RequiredUserId = request.UserId,
RequiredRoles = new[] { IdentityRoles.Administrator.ToString() }
});
}
}