src/Security/CorporationVoter.php line 10

  1. <?php
  2. namespace App\Security;
  3. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  4. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  5. use App\Entity\User;
  6. use App\Entity\Corporation;
  7. class CorporationVoter extends Voter
  8. {
  9.     const CAN_EDIT 'cpv_canedit';
  10.     const CAN_REMOVE 'cpv_canremove';
  11.     /**
  12.      * @param string $attribute
  13.      * @param Corporation $subject
  14.      * @param TokenInterface $token
  15.      * @return bool
  16.      */
  17.     protected function voteOnAttribute($attribute$subjectTokenInterface $token) : bool
  18.     {
  19.         $user $token->getUser();
  20.         if (!$user instanceof User) {
  21.             return false;
  22.         }
  23.         if ($attribute === self::CAN_EDIT) {
  24.             return $subject->getIdAdh() === null
  25.                 || ($subject->getIdAdh() !== null && $user->getIsSuperviseur());
  26.         } else if ($attribute === self::CAN_REMOVE) {
  27.             return $subject->getIdAdh() === null
  28.                 || ($subject->getIdAdh() !== null && $user->getIsSuperviseur());
  29.         }
  30.         return false;
  31.     }
  32.     /**
  33.      * @param string $attribute
  34.      * @param Corporation $subject
  35.      * @return bool
  36.      */
  37.     protected function supports($attribute$subject) : bool
  38.     {
  39.         if (!in_array($attribute, [self::CAN_EDITself::CAN_REMOVE])) {
  40.             return false;
  41.         }
  42.         if (!$subject instanceof Corporation) {
  43.             return false;
  44.         }
  45.         return true;
  46.     }
  47. }