src/Security/ApplicantVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\Applicant;
  4. use App\Entity\User;
  5. use LogicException;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. class ApplicantVoter extends Voter
  9. {
  10.     // these strings are just invented: you can use anything
  11.     public const VIEW 'view';
  12.     public const EDIT 'edit';
  13.     /**
  14.      * @param string $attribute
  15.      * @param mixed  $subject
  16.      */
  17.     protected function supports($attribute$subject): bool
  18.     {
  19.         // if the attribute isn't one we support, return false
  20.         if (!\in_array($attribute, [self::VIEWself::EDIT], true)) {
  21.             return false;
  22.         }
  23.         // only vote on Vacancy objects inside this voter
  24.         if (!$subject instanceof Applicant) {
  25.             return false;
  26.         }
  27.         return true;
  28.     }
  29.     /**
  30.      * @param string $attribute
  31.      * @param mixed  $subject
  32.      */
  33.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  34.     {
  35.         $user $token->getUser();
  36.         if (!$user instanceof User) {
  37.             // the user must be logged in; if not, deny access
  38.             return false;
  39.         }
  40.         // you know $subject is a Applicant object, thanks to supports
  41.         /** @var Applicant $applicant */
  42.         $applicant $subject;
  43.         switch ($attribute) {
  44.             case self::VIEW:
  45.                 return $this->canView($applicant$user);
  46.             case self::EDIT:
  47.                 return $this->canEdit($applicant$user);
  48.         }
  49.         throw new LogicException('This code should not be reached!');
  50.     }
  51.     private function canView(Applicant $applicantUser $user): bool
  52.     {
  53.         // if they can edit, they can view
  54.         if ($this->canEdit($applicant$user)) {
  55.             return true;
  56.         }
  57.         if ($user->hasRole('ROLE_ADMIN_USER') || $user->isSuperAdmin()) {
  58.             return true;
  59.         }
  60.         if ($user->hasRole('ROLE_ADMIN_HIRING_MANAGER')) {
  61.             if ($applicant->getVacancy()->getManagers()->count() && $user->getCompanies()->isEmpty()) {
  62.                 return $applicant->getVacancy()->getManagers()->contains($user);
  63.             }
  64.             if ($user->getCompanies()->contains($applicant->getVacancy()->getCompany())) {
  65.                 return true;
  66.             }
  67.         }
  68.         if ($user->hasRole('ROLE_ADMIN_JOB_OWNER')) {
  69.             if ($applicant->getVacancy()
  70.                 && $applicant->getVacancy()->getManagers()->count()
  71.                 && $applicant->getVacancy()->getManagers()->contains($user)
  72.             ) {
  73.                 return true;
  74.             }
  75.             if ($applicant->getVacancy() && $user->getCompanies()->contains($applicant->getVacancy()->getCompany())) {
  76.                 return true;
  77.             }
  78.         }
  79.         return false;
  80.     }
  81.     private function canEdit(Applicant $applicantUser $user): bool
  82.     {
  83.         return false;
  84.     }
  85. }