<?php
namespace App\Security\Voter;
use App\Roles;
use App\Entity\User;
use App\Entity\Entreprise;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
class AllowDeleteEntrepriseVoter extends Voter
{
/** @var Security */
protected $security;
const PERMISSIONS = ['CAN_DELETE_ENTREPRISE'];
public function __construct(Security $security)
{
$this->security = $security;
}
protected function supports($attribute, $subject): bool
{
if (!($subject instanceof Entreprise)) {
return false;
}
if (!in_array($attribute, self::PERMISSIONS)) {
return false;
}
return true;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
// On vérifie que l'utilisateur est un des nôtres
$user = $token->getUser();
if (!$user instanceof User) {
return false;
}
// On vérifie qu'il a les droits
if (!$this->security->isGranted(Roles::ROLE_OPERATOR)) {
return false;
}
// On vérifie si des dossiers sont attachés à l'entreprise
if(count($subject->getDossiers()) > 0) {
return false;
}
return true;
}
}