<?php
namespace App\Voters;
use App\Entity\Users\Vendor;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
class VendorVoter extends Voter
{
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
/**
* Determines if the attribute and subject are supported by this voter.
*
* @param string $attribute An attribute
* @param mixed $subject The subject to secure, e.g. an object the user wants to access or any other PHP type
*
* @return bool True if the attribute and subject are supported, false otherwise
*/
protected function supports(string $attribute, $subject)
{
if (0 !== strpos($attribute, 'EA_')) {
return false;
}
if($subject->getInstance()){
if( $subject->getInstance() instanceof Vendor){
return true;
}
}
// TODO: Implement supports() method.
}
/**
* Perform a single access check operation on a given attribute, subject and token.
* It is safe to assume that $attribute and $subject already passed the "supports()" method check.
*
* @param mixed $subject
*
* @return bool
*/
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token)
{
$user = $token->getUser();
if ($this->security->isGranted('ROLE_ADMIN') || $this->security->isGranted('ROLE_SUPERVISOR') || $this->security->isGranted('ROLE_REPRESENTATIVE') || $this->security->isGranted('ROLE_LABOADMIN') ) {
// if($user->getLaboratory() == )
// check if vendor has access to the page
$vendor = $subject->getInstance();
return true;
}
throw new \LogicException('This code should not be reached!');
// TODO: Implement voteOnAttribute() method.
}
}