<?php
namespace App\Security\Voter;
use App\Entity\Enum\RoleEnum;
use App\Entity\RoleManagement;
use App\Entity\Transaction;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
class TransactionVoter extends Voter
{
public function __construct(private Security $security)
{}
protected function supports($attribute, $subject): bool
{
$supportsAttribute = in_array($attribute, ['TRANSACTION_CREATE', 'TRANSACTION_READ', 'TRANSACTION_EDIT', 'TRANSACTION_DELETE']);
$supportsSubject = $subject instanceof Transaction;
return $supportsAttribute && $supportsSubject;
}
/**
* @param string $attribute
* @param Transaction $subject
* @param TokenInterface $token
* @return bool
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
if($this->security->isGranted(RoleEnum::SUPER_ADMIN->value)) return true;
// TODO belongs to user
// refactor api
return false;
}
}