src/Security/Voter/ResumeVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\User;
  4. use App\Repository\ResumeTemplateRepository;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. class ResumeVoter extends Voter
  10. {
  11.     const CREATE_RESUME 'create_resume';
  12.     private AuthorizationCheckerInterface $authorizationChecker;
  13.     private ResumeTemplateRepository $resumeTemplateRepo;
  14.     public function __construct(AuthorizationCheckerInterface $authorizationCheckerResumeTemplateRepository $resumeTemplateRepo)
  15.     {
  16.         $this->authorizationChecker $authorizationChecker;
  17.         $this->resumeTemplateRepo $resumeTemplateRepo;
  18.     }
  19.     protected function supports($attribute$subject)
  20.     {
  21.         return in_array($attribute, [
  22.             self::CREATE_RESUME,
  23.         ]);
  24.     }
  25.     /**
  26.      * @param string                   $attribute
  27.      * @param ?\App\Entity\Resume|mixed $subject
  28.      *
  29.      * @return bool
  30.      */
  31.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  32.     {
  33.         /** @var User|string $user */
  34.         $user $subject $subject->getStudent() : $token->getUser();
  35.         // if the user is anonymous, do not grant access
  36.         if (!$user instanceof UserInterface) {
  37.             return false;
  38.         }
  39.         switch ($attribute) {
  40.             case self::CREATE_RESUME:
  41.                 return $user->getResumes()->count() && $this->resumeTemplateRepo->count([]) > 0;
  42.         }
  43.         return false;
  44.     }
  45. }