src/Security/Voter/AllowEditEntrepriseVoter.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Roles;
  4. use App\Entity\User;
  5. use App\Entity\Entreprise;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. use Symfony\Component\Security\Core\Security;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11. class AllowEditEntrepriseVoter extends Voter
  12. {
  13.     /** @var Security */
  14.     protected $security;
  15.     const PERMISSIONS = ['CAN_EDIT_ENTREPRISE'];
  16.     
  17.     public function __construct(Security $security)
  18.     {
  19.         $this->security $security;
  20.     }
  21.     protected function supports($attribute$subject): bool
  22.     {
  23.         if (!($subject instanceof Entreprise)) {
  24.             return false;
  25.         }
  26.         if (!in_array($attributeself::PERMISSIONS)) {
  27.             return false;
  28.         }
  29.         return true;
  30.     }
  31.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  32.     {
  33.         // On vérifie que l'utilisateur est un des nôtres
  34.        $user $token->getUser();
  35.        if (!$user instanceof User) {
  36.            return false;
  37.        }
  38.         // On vérifie qu'il a les droits 
  39.         if (!$this->security->isGranted(Roles::ROLE_OPERATOR))  {
  40.             return false;
  41.         }
  42.         
  43.         // On vérifie si des dossiers sont attachés à l'entreprise
  44.         if(count($subject->getDossiers()) > 0) {
  45.             return false;
  46.         }
  47.        
  48.         return true;
  49.     }
  50. }