src/Security/Voters/PrimeVoter.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voters;
  3. use App\Entity\Collab;
  4. use App\Entity\CollabContract;
  5. use App\Entity\Prime;
  6. use App\Entity\User;
  7. use App\Security\SecurityTrait;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  10. use Symfony\Component\Security\Core\Security;
  11. class PrimeVoter extends Voter
  12. {
  13.     use SecurityTrait;
  14.     const LIST = "list_prime";
  15.     const EDIT "edit_prime";
  16.     const FIRST_LEVEL_VALIDATION "first_level_validation_prime";
  17.     const SECOND_LEVEL_VALIDATION "second_level_validation_prime";
  18.     private $security;
  19.     public function __construct(Security $security)
  20.     {
  21.         $this->security $security;
  22.     }
  23.     protected function supports($attribute$subject)
  24.     {
  25.         return in_array($attribute, [self::LIST, self::FIRST_LEVEL_VALIDATIONself::SECOND_LEVEL_VALIDATION]) ||
  26.             (in_array($attribute, [
  27.                 self::EDIT
  28.             ]) && $subject instanceof Prime);
  29.     }
  30.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  31.     {
  32.         $user $token->getUser();
  33.         if (!$user instanceof User) {
  34.             return false;
  35.         }
  36.         switch ($attribute) {
  37.             case self::LIST:
  38.                 return $this->isAdmin();
  39.             case self::EDIT:
  40.                 return $this->isAdmin();
  41.             case self::FIRST_LEVEL_VALIDATION:
  42.                 return $this->isAdmin() || $this->isSalesManager();
  43.             case self::SECOND_LEVEL_VALIDATION:
  44.                 return $this->isAdmin() || $this->isGeneralManager();
  45.         }
  46.         throw new \LogicException('This code should not be reached!');
  47.     }
  48. }