src/Security/Voter/CollectiveVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Collective;
  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 CollectiveVoter extends Voter
  9. {
  10.     public const LIST = 'collective-list';
  11.     public const SHOW 'collective-show';
  12.     public const CREATE 'collective-create';
  13.     public const EDIT 'collective-edit';
  14.     /**
  15.      * @inheritDoc
  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::LIST, self::SHOWself::CREATEself::EDIT])) {
  21.             return false;
  22.         }
  23.         if (!$subject instanceof Collective && $subject) {
  24.             return false;
  25.         }
  26.         return true;
  27.     }
  28.     /**
  29.      * @inheritDoc
  30.      */
  31.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  32.     {
  33.         $user $token->getUser();
  34.         if (!$user instanceof User) {
  35.             // the user must be logged in; if not, deny access
  36.             return false;
  37.         }
  38.         switch ($attribute) {
  39.             case self::LIST:
  40.                 return $this->canList($user);
  41.             case self::SHOW:
  42.                 return $this->canShow($user$subject);
  43.             case self::CREATE:
  44.                 return $this->canCreate($user);
  45.             case self::EDIT:
  46.                 return $this->canEdit($user);
  47.         }
  48.         throw new LogicException('This code should not be reached!');
  49.     }
  50.     /**
  51.      * @param User $user
  52.      * @return bool
  53.      */
  54.     private function canList(User $user): bool
  55.     {
  56.         if (
  57.             $user->hasRole('ROLE_ATCLIENTE') ||
  58.             $user->hasRole('ROLE_ATCLIENTE_ADMIN') ||
  59.             $user->hasRole('ROLE_ANALYST')
  60.         ) {
  61.             return true;
  62.         }
  63.         return false;
  64.     }
  65.     /**
  66.      * @param User $user
  67.      * @param Collective $collective
  68.      * @return bool
  69.      */
  70.     private function canShow(User $userCollective $collective): bool
  71.     {
  72.         // If there isn't any Collective translation or contract
  73.         $collectiveTranslations $collective->getCollectiveTranslations()->toArray();
  74.         if (empty($collectiveTranslations) || empty($collective->getContracts())) {
  75.             return false;
  76.         }
  77.         // If there isn't any ContractLine translation
  78.         foreach (($collectiveTranslations[0])->getCollective()->getCollectiveLines() as $contractLine) {
  79.             // TODO
  80. //            if (!sizeof($contractLine->getTranslations())) {
  81. //                return false;
  82. //            }
  83.         }
  84.         // Roles
  85.         if (
  86.             !$user->hasRole('ROLE_ATCLIENTE') &&
  87.             !$user->hasRole('ROLE_ATCLIENTE_ADMIN') &&
  88.             !$user->hasRole('ROLE_ANALYST')
  89.         ) {
  90.             return false;
  91.         }
  92.         return true;
  93.     }
  94.     /**
  95.      * @param User $user
  96.      * @return bool
  97.      */
  98.     private function canCreate(User $user): bool
  99.     {
  100.         if ($this->canEdit($user)) {
  101.             return true;
  102.         }
  103.         return false;
  104.     }
  105.     /**
  106.      * @param User $user
  107.      * @return bool
  108.      */
  109.     private function canEdit(User $user): bool
  110.     {
  111.         if ($user->hasRole('ROLE_ATCLIENTE_ADMIN')) {
  112.             return true;
  113.         }
  114.         return false;
  115.     }
  116. }