src/Security/Voter/JobAdVoter.php line 9

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  4. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  5. use Symfony\Component\Security\Core\User\UserInterface;
  6. class JobAdVoter extends Voter
  7. {
  8.     public const MANAGE 'JOB_MANAGE';
  9.     protected function supports(string $attribute$jobad): bool
  10.     {
  11.         // replace with your own logic
  12.         // https://symfony.com/doc/current/security/voters.html
  13.         return in_array($attribute, [self::MANAGE])
  14.             && $jobad instanceof \App\Entity\JobAd;
  15.     }
  16.     protected function voteOnAttribute(string $attribute$jobadTokenInterface $token): bool
  17.     {
  18.         $user $token->getUser();
  19.         // if the user is anonymous, do not grant access
  20.         if (!$user instanceof UserInterface) {
  21.             return false;
  22.         }
  23.         if ($this->canManage($jobad,  $user)) {
  24.             return true;
  25.         } 
  26.         return false;
  27.     }
  28.     protected function canManage($jobad$user) {
  29.         return $user === $jobad->getUser();
  30.     }
  31. }