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