<?php
namespace App\Security;
use App\Entity\Report\PaypalPayerDetail;
use App\Entity\System\Employee;
use App\Entity\System\Role;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class PaypalPayerDetailVoter extends Voter
{
public const SHOW = 'show';
private AuthorizationCheckerInterface $authorizationChecker;
public function __construct(AuthorizationCheckerInterface $authorizationChecker)
{
$this->authorizationChecker = $authorizationChecker;
}
/**
* @param PaypalPayerDetail $subject
*/
protected function supports(string $attribute, $subject): bool
{
if (!(get_class($subject) === PaypalPayerDetail::class)) {
return false;
}
return $this->supportsAttribute($attribute);
}
public function supportsAttribute(string $attribute): bool
{
$voter = new \ReflectionClass(__CLASS__);
$allowedValues = $voter->getConstants();
if (!\array_key_exists($attribute, \array_flip($allowedValues))) {
return false;
}
return true;
}
/**
* @param PaypalPayerDetail $paypalPayerDetail
*/
protected function voteOnAttribute(string $attribute, $paypalPayerDetail, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof Employee) {
return false;
}
if ($attribute !== self::SHOW) {
throw new \LogicException('This code should not be reached!');
}
return $this->showAllowed($paypalPayerDetail, $user);
}
private function showAllowed(PaypalPayerDetail $paypalPayerDetail, Employee $user): bool
{
if ($this->authorizationChecker->isGranted(Role::ROLE_DEV)) {
return true;
}
return false;
}
}