<?php
declare(strict_types=1);
namespace App\Security\Voters;
use App\Services\Accounts\ClientAccount\ClientAccountPermissionService;
use App\Entity\Permissions\AccountPermissionSetting;
use App\Entity\Transaction;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
class TransactionVoter extends Voter
{
private $clientAccountPermissionService;
public function __construct(ClientAccountPermissionService $permissionService)
{
$this->clientAccountPermissionService = $permissionService;
}
protected function supports($attribute, $subject)
{
if (!in_array($attribute, $this->getSupportedPermissions())) {
return false;
}
return !(!$subject instanceof Transaction)
;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
if (!in_array($attribute, $this->getSupportedPermissions())) {
return false;
}
/** @var Transaction $transaction */
$transaction = $subject;
$payload = $this->clientAccountPermissionService->checkTransactions([$transaction->getId()], $attribute);
return !$payload->hasError();
}
private function getSupportedPermissions(): array
{
return [
AccountPermissionSetting::VIEW_ACCOUNT_PERMISSION,
AccountPermissionSetting::SIGN_TRANSACTIONS_PERMISSION,
AccountPermissionSetting::INTERNAL_PAYMENT_PERMISSION,
AccountPermissionSetting::SEPA_PAYMENT_PERMISSION,
];
}
}