src/Security/Voter/InformationReportVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\InformationReport;
  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. use Symfony\Component\Security\Core\Security;
  10. class InformationReportVoter extends Voter
  11. {
  12.     public const CREATE 'create_report';
  13.     public const EDIT 'edit_report';
  14.     public const SHOW 'show_report';
  15.     /** @var Security $security */
  16.     private $security;
  17.     /**
  18.      * InformationReportVoter constructor.
  19.      * @param Security $security
  20.      */
  21.     public function __construct(Security $security)
  22.     {
  23.         $this->security $security;
  24.     }
  25.     /**
  26.      * @inheritDoc
  27.      */
  28.     protected function supports($attribute$subject): bool
  29.     {
  30.         // if the attribute isn't one we support, return false
  31.         if (!in_array($attribute, [self::CREATEself::EDITself::SHOW])) {
  32.             return false;
  33.         }
  34.         // only vote on ContractLine objects inside this voter
  35.         if (!$subject instanceof ContractLine && !$subject instanceof InformationReport) {
  36.             return false;
  37.         }
  38.         return true;
  39.     }
  40.     /**
  41.      * @inheritDoc
  42.      */
  43.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  44.     {
  45.         $user $token->getUser();
  46.         if (!$user instanceof User) {
  47.             // the user must be logged in; if not, deny access
  48.             return false;
  49.         }
  50.         switch ($attribute) {
  51.             case self::CREATE:
  52.                 return $this->canCreate($subject);
  53.             case self::EDIT:
  54.                 return $this->canEdit($subject);
  55.             case self::SHOW:
  56.                 return $this->canShow();
  57.         }
  58.         throw new LogicException('This code should not be reached!');
  59.     }
  60.     /**
  61.      * @param ContractLine $collectiveLine
  62.      * @return bool
  63.      */
  64.     private function canCreate(ContractLine $collectiveLine): bool
  65.     {
  66.         return ($this->security->isGranted('ROLE_ATCLIENTE') && $collectiveLine->getEnabledRelevantPeriod());
  67.     }
  68.     /**
  69.      * @param InformationReport $report
  70.      * @return bool
  71.      */
  72.     private function canEdit(InformationReport $report): bool
  73.     {
  74.         $collectiveLine $report->getContractLine();
  75.         return ($this->security->isGranted('ROLE_ATCLIENTE') && $collectiveLine->getEnabledRelevantPeriod());
  76.     }
  77.     /**
  78.      * @return bool
  79.      */
  80.     private function canShow(): bool
  81.     {
  82.         return $this->security->isGranted('ROLE_ATCLIENTE');
  83.     }
  84. }