src/Security/SuperheroFreight/ReplenishmentOrderVoter.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Security\SuperheroFreight;
  3. use App\Entity\CommonData\User;
  4. use App\Entity\SuperheroFreight\ReplenishmentOrder;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. /**
  8.  * Class ReplenishmentOrderVoter
  9.  *
  10.  * @package App\Security\SuperheroFreight
  11.  */
  12. class ReplenishmentOrderVoter extends Voter
  13. {
  14.     /**
  15.      * @var string
  16.      */
  17.     const VIEW 'view';
  18.     /**
  19.      * @var string
  20.      */
  21.     const EDIT 'edit';
  22.     /**
  23.      * @param string $attribute
  24.      * @param mixed  $subject
  25.      *
  26.      * @return bool
  27.      */
  28.     protected function supports($attribute$subject): bool
  29.     {
  30.         if (!in_array($attribute, [self::VIEWself::EDIT])) {
  31.             return false;
  32.         }
  33.         if (!$subject instanceof ReplenishmentOrder) {
  34.             return false;
  35.         }
  36.         return true;
  37.     }
  38.     /**
  39.      * @param string         $attribute
  40.      * @param mixed          $subject
  41.      * @param TokenInterface $token
  42.      *
  43.      * @return bool
  44.      */
  45.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  46.     {
  47.         $user $token->getUser();
  48.         if (!$user instanceof User) {
  49.             return false;
  50.         }
  51.         /** @var ReplenishmentOrder $replenishmentOrder */
  52.         $replenishmentOrder $subject;
  53.         switch ($attribute) {
  54.             case self::VIEW:
  55.                 return $this->canView($replenishmentOrder$user);
  56.             case self::EDIT:
  57.                 return $this->canEdit($replenishmentOrder$user);
  58.         }
  59.         throw new \LogicException('This code should not be reached!');
  60.     }
  61.     /**
  62.      * @param ReplenishmentOrder $replenishmentOrder
  63.      * @param User               $user
  64.      *
  65.      * @return bool
  66.      */
  67.     private function canView(ReplenishmentOrder $replenishmentOrderUser $user): bool
  68.     {
  69.         return $user === $replenishmentOrder->getShfUser()->getUser() && !$replenishmentOrder->isDeleted();
  70.     }
  71.     /**
  72.      * @param ReplenishmentOrder $replenishmentOrder
  73.      * @param User               $user
  74.      *
  75.      * @return bool
  76.      */
  77.     private function canEdit(ReplenishmentOrder $replenishmentOrderUser $user): bool
  78.     {
  79.         return $replenishmentOrder->isEditable() &&
  80.             $user === $replenishmentOrder->getShfUser()->getUser() &&
  81.             !$replenishmentOrder->isDeleted();
  82.     }
  83. }