<?php
namespace App\Security\Voter;
use App\Entity\Form;
use App\Entity\FormLead;
use App\Entity\FormLeadData;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
class FormLeadVoter extends Voter
{
public const VIEW = 'VIEW';
/** @var Security */
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
protected function supports(string $attribute, $formLead): bool
{
return in_array($attribute, [self::VIEW]) && ($formLead instanceof FormLead);
}
protected function voteOnAttribute(string $attribute, $formLead, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof UserInterface) {
return false;
}
if ($this->security->isGranted('ROLE_SUPERADMIN')) {
return true;
}
$campaign = $formLead->getCampaign();
switch ($attribute) {
case self::VIEW:
foreach ($user->getCompanies() as $company) {
if ($company->getCampaigns()->contains($campaign)) {
return true;
}
}
return false;
break;
}
return false;
}
}