src/Security/Voter/TrainingVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Authentication\DeveloperApp;
  4. use App\Entity\Authentication\Person;
  5. use App\Entity\Training\TrainingAnnouncement;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. class TrainingVoter extends Voter
  9. {
  10.     public const EDIT = 'edit';
  11.     protected function supports($attribute, $subject): bool
  12.     {
  13.         return in_array($attribute, [self::EDIT])
  14.             && $subject instanceof TrainingAnnouncement;
  15.     }
  16.     protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
  17.     {
  18.         $user = $token->getUser();
  19.         if (!$user instanceof Person || !$user->getDeveloperApp() instanceof DeveloperApp) {
  20.             return false;
  21.         }
  22.         /** @var TrainingAnnouncement $training */
  23.         $training = $subject;
  24.         switch ($attribute) {
  25.             case self::EDIT:
  26.                 return $this->canEdit($training, $user->getDeveloperApp());
  27.         }
  28.         return false;
  29.     }
  30.     private function canEdit(TrainingAnnouncement $training, DeveloperApp $provider)
  31.     {
  32.         return $provider === $training->getProvider();
  33.     }
  34. }