src/Security/ConsultationDraftVoter.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\ConsultationDraft;
  7. class ConsultationDraftVoter extends Voter
  8. {
  9.     const CAN_DRAFT 'cdv_candraft';
  10.     /**
  11.      * @param string $attribute
  12.      * @param ConsultationDraft $subject
  13.      * @param TokenInterface $token
  14.      * @return bool
  15.      */
  16.     protected function voteOnAttribute($attribute$subjectTokenInterface $token) : bool
  17.     {
  18.         $user $token->getUser();
  19.         if (!$user instanceof User) {
  20.             return false;
  21.         }
  22.         if ($attribute === self::CAN_DRAFT) {
  23.             return $user === $subject->getUser();
  24.         }
  25.         return false;
  26.     }
  27.     /**
  28.      * @param string $attribute
  29.      * @param ConsultationDraft $subject
  30.      * @return bool
  31.      */
  32.     protected function supports(string $attribute$subject) : bool
  33.     {
  34.         if (!in_array($attribute, [self::CAN_DRAFT])) {
  35.             return false;
  36.         }
  37.         if (!$subject instanceof ConsultationDraft) {
  38.             return false;
  39.         }
  40.         return true;
  41.     }
  42. }