<?php
/*
* Author: Dominik Piekarski <code@dompie.de>
* Created at: 2022/07/05 06:50
*/
declare(strict_types=1);
namespace App\Lib\Payment\Voter;
use App\Dictionary\Permission;
use App\Entity\Account;
use App\Security\ApiUser;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class PaymentLimitsVoter extends Voter
{
protected function supports(string $attribute, $subject): bool
{
return Permission::ALLOWED_TO_BOOK === $attribute && $subject instanceof Account;
}
/**
* @param Account $subject
*
* @return bool|int
*/
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
/** @var ApiUser $apiUser */
$apiUser = $token->getUser();
if ($apiUser->getIsAdmin()) {
return true;
}
$currentAccount = $apiUser->getAccount();
if ($currentAccount instanceof Account && $currentAccount->getId() !== $subject->getId()) {
throw PaymentVoterException::notYourAccount();
}
if (null === $member = $subject->getMember()) {
throw PaymentVoterException::notAMember();
}
return true;
}
}