src/Security/Authorization/Voter/ReportVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Security\Authorization\Voter;
  3. use App\Entity\User;
  4. use App\Service\Report\DeliveredItemsReport;
  5. use App\Service\Report\OfferReport;
  6. use App\Service\Report\ReportInterface;
  7. use App\Service\Report\SupportCaseReport;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  10. class ReportVoter extends Voter
  11. {
  12.     const READ 'read';
  13.     /**
  14.      * {@inheritDoc}
  15.      */
  16.     protected function supports($attribute$subject): bool
  17.     {
  18.         if ($attribute != self::READ) {
  19.             return false;
  20.         }
  21.         if (is_string($subject)) {
  22.             if (!(class_exists($subject) && is_a($subjectReportInterface::class, true))) {
  23.                 return false;
  24.             }
  25.         } elseif (is_object($subject)) {
  26.             if (!($subject instanceof ReportInterface)) {
  27.                 return false;
  28.             }
  29.         }
  30.         return true;
  31.     }
  32.     /**
  33.      * {@inheritDoc}
  34.      */
  35.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  36.     {
  37.         $user $token->getUser();
  38.         if (!($user instanceof User)) {
  39.             return false;
  40.         }
  41.         if (is_object($subject)) {
  42.             $subject get_class($subject);
  43.         }
  44.         /* @var string $reportClass */
  45.         $reportClass $subject;
  46.         switch ($attribute) {
  47.             case self::READ:
  48.                 if (in_array($reportClass, [OfferReport::class, SupportCaseReport::class, DeliveredItemsReport::class])) {
  49.                     return true;
  50.                 }
  51.                 if ($user->isSuperAdmin()) {
  52.                     return true;
  53.                 }
  54.                 break;
  55.         }
  56.         return false;
  57.     }
  58. }