<?php
declare(strict_types=1);
namespace Bdm\PublicApiBundle\Security\Authorization\Voter;
use Bdm\OAuth2Bundle\Entity\AccessToken;
use Bdm\PublicApiBundle\Entity\VerificationDocument;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class VerificationDocumentVoter extends Voter
{
const VIEW = 'VIEW';
public function __construct(private readonly EntityManager $oEntityManager)
{
}
/**
* @param string $sAttribute attribute
* @param mixed $oSubject subject
* @return bool
*/
public function supports($sAttribute, $oSubject)
{
if (!in_array($sAttribute, [self::VIEW])) {
return false;
}
if (!$oSubject instanceof VerificationDocument) {
return false;
}
return true;
}
/**
* @param string $sAttribute attribute
* @param VerificationDocument $oVerificationDoc VerificationDocument
* @param TokenInterface $oToken token
*
* @return bool
*/
public function voteOnAttribute($sAttribute, $oVerificationDoc, TokenInterface $oToken)
{
$oAccessToken = $this->oEntityManager
->getRepository(AccessToken::class)
->findOneBy(['token' => $oToken->getToken()]);
$oMerchant = $oAccessToken->getClient()->getMerchant();
return $oMerchant == $oVerificationDoc->getMerchant();
}
}