<?php
declare(strict_types=1);
namespace Bdm\PublicApiBundle\Security\Authorization\Voter;
use Bdm\OAuth2Bundle\Entity\AccessToken;
use Bdm\PublicApiBundle\Entity\StoredFile;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class StoredFileVoter 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 StoredFile) {
return false;
}
return true;
}
/**
* @param string $sAttribute attribute
* @param StoredFile $oStoredFile VerificationDocument
* @param TokenInterface $oToken token
*
* @return bool
*/
public function voteOnAttribute($sAttribute, $oStoredFile, TokenInterface $oToken)
{
$oAccessToken = $this->oEntityManager
->getRepository(AccessToken::class)
->findOneBy(['token' => $oToken->getToken()]);
$oMerchant = $oAccessToken->getClient()->getMerchant();
return $oMerchant == $oStoredFile->getMerchant();
}
}