<?php
namespace App\Security;
use App\Entity\BFFestival;
use App\Entity\BFEdition;
use App\Entity\BFChallenge;
use App\Entity\BFChallengeEdition;
use App\Entity\BFOption;
use App\Utilities\OptionUtilities;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
class BFAdminLicencesVoter extends Voter
{
private $security;
//the string options are managed by the utility
public function __construct(Security $security)
{
$this->security = $security;
}
protected function supports($attribute, $subject)
{
// if the attribute isn't one we support, return false
if (!in_array($attribute, OptionUtilities::OptionArray)) {
return false;
}
// only vote on `BFFestival` objects
if (($subject instanceof BFEdition) || ($subject instanceof BFFestival || ($subject instanceof BFChallenge) || ($subject instanceof BFChallengeEdition))) { // ||
return true;
}
return false;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
$bfadminlicences = $subject->getBFAdminLicences();
$datetimenow = \DateTime::createFromFormat('Y-m-d', date('Y-m-d'));
foreach($bfadminlicences as $bfadminlicence)
{
//check options
if($bfadminlicence->getOptionname() == $attribute
&& $bfadminlicence->getValiditydate()>=$datetimenow)
{
return true;
}
//check offer
if(in_array($bfadminlicence->getOptionname(), OptionUtilities::OfferArray) //check is offer
&& in_array($attribute, OptionUtilities::OfferArray['$bfadminlicence->getOptionname()']) //check offer options
&& $bfadminlicence->getValiditydate()>=$datetimenow //check date
)
{
return true;
}
}
return false;
}
}
?>