src/Daikin/FrontendBundle/Security/OrderVoter.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Daikin\FrontendBundle\Security;
  3. use App\Daikin\BaseBundle\Entity\EmergencyStock\Order;
  4. use App\Daikin\BaseBundle\Entity\User;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. /**
  9.  * Class OrderVoter
  10.  * @package App\Daikin\FrontendBundle\Security
  11.  */
  12. class OrderVoter extends Voter
  13. {
  14.     const VIEW 'view';
  15.     const REMOVE 'remove';
  16.     /**
  17.      * @var AccessDecisionManagerInterface
  18.      */
  19.     private $decisionManager;
  20.     /**
  21.      * OrderVoter constructor.
  22.      * @param AccessDecisionManagerInterface $decisionManager
  23.      */
  24.     public function __construct(AccessDecisionManagerInterface $decisionManager)
  25.     {
  26.         $this->decisionManager $decisionManager;
  27.     }
  28.     protected function supports($attribute$subject): bool
  29.     {
  30.         if ($attribute != self::VIEW && $attribute != self::REMOVE) {
  31.             return false;
  32.         }
  33.         if (!$subject instanceof Order) {
  34.             return false;
  35.         }
  36.         return true;
  37.     }
  38.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  39.     {
  40.         if ($this->decisionManager->decide($token, ['ROLE_SPARE_PARTS_FULLADMIN'])) {
  41.             return true;
  42.         }
  43.         //admin can do anything
  44.         if ($attribute == self::VIEW && $this->decisionManager->decide($token, ['ROLE_SPARE_PARTS_PARTS_ADMIN'])) {
  45.             return true;
  46.         }
  47.         $user $token->getUser();
  48.         if (!$user instanceof User) {
  49.             return false;
  50.         }
  51.         /** @var Order $order */
  52.         $order $subject;
  53.         if ($order->getCustomer()->getCompany() != $user->getCompany()) {
  54.             return false;
  55.         }
  56.         return true;
  57.     }
  58. }