classlib/ExpenseTracker.Application/Accounts/Commands/Create/CreateAccountCommandAuthorizer.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.Create;
public class CreateAccountCommandAuthorizer : AbstractRequestAuthorizer<CreateAccountCommand>
{
private readonly ISessionUserService _sessionUserService;
public CreateAccountCommandAuthorizer(ISessionUserService currentUserService)
{
_sessionUserService = currentUserService;
}
public override void BuildPolicy(CreateAccountCommand request)
{
UseRequirement(new MustBeAuthenticatedRequirement
{
IsAuthenticated = _sessionUserService.IsAuthenticated
});
UseRequirement(new MustBeInRolesWhenInteractingWithUnOwnedEntityRequirement
{
UserId = _sessionUserService.Id,
UserRoles = _sessionUserService.Roles,
RequiredUserId = request.UserId ?? _sessionUserService.Id,
RequiredRoles = new[] { IdentityRoles.Administrator.ToString() }
});
}
}