src/Security/Voter/ContractLineVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Collective;
  4. use App\Entity\ContractLine;
  5. use App\Entity\User;
  6. use LogicException;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. class ContractLineVoter extends Voter
  10. {
  11.     public const EDIT 'contract-line-edit';
  12.     /**
  13.      * @inheritDoc
  14.      */
  15.     protected function supports($attribute$subject): bool
  16.     {
  17.         // if the attribute isn't one we support, return false
  18.         if ($attribute != self::EDIT) {
  19.             return false;
  20.         }
  21.         if (!$subject instanceof ContractLine) {
  22.             return false;
  23.         }
  24.         return true;
  25.     }
  26.     /**
  27.      * @inheritDoc
  28.      */
  29.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  30.     {
  31.         $user $token->getUser();
  32.         if (!$user instanceof User) {
  33.             // the user must be logged in; if not, deny access
  34.             return false;
  35.         }
  36.         switch ($attribute) {
  37.             case self::EDIT:
  38.                 return $this->canEdit($user$subject);
  39.         }
  40.         throw new LogicException('This code should not be reached!');
  41.     }
  42.     /**
  43.      * @param User $user
  44.      * @param ContractLine $line
  45.      * @return bool
  46.      */
  47.     private function canEdit(User $userContractLine $line): bool
  48.     {
  49.         return ($user->hasRole('ROLE_ATCLIENTE_ADMIN') && $line->getEnabledRelevantPeriod());
  50.     }
  51. }