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 { 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() } }); } }