src/Voter/SupplierVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Voter;
  3. use App\Entity\Supplier;
  4. use App\Entity\User as UserEntity;
  5. use App\Model\SupplierFactory;
  6. use App\Model\User\User;
  7. use Exception;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  10. class SupplierVoter extends Voter
  11. {
  12.     public const CREATE_GUEST_SUPPLIER 'create_guest_supplier';
  13.     public const MANAGE_GUEST_SUPPLIER 'manage_guest_supplier';
  14.     public const DECLARE_SUPPLIER_ACCOUNT 'declare_supplier_account';
  15.     public const VIEW_SUPPLIER 'view_supplier';
  16.     private $user;
  17.     /**
  18.      * @var SupplierFactory
  19.      */
  20.     private $supplierFactory;
  21.     public function __construct(User $userSupplierFactory $supplierFactory)
  22.     {
  23.         $this->user            $user;
  24.         $this->supplierFactory $supplierFactory;
  25.     }
  26.     protected function supports($attribute$subject): bool
  27.     {
  28.         // if the attribute isn't one we support, return false
  29.         if (! in_array($attribute, [
  30.             self::CREATE_GUEST_SUPPLIER,
  31.             self::MANAGE_GUEST_SUPPLIER,
  32.             self::DECLARE_SUPPLIER_ACCOUNT,
  33.             self::VIEW_SUPPLIER
  34.         ])) {
  35.             return false;
  36.         }
  37.         // only vote on Supplier objects inside this voter
  38.         if (! $subject instanceof Supplier) {
  39.             return false;
  40.         }
  41.         return true;
  42.     }
  43.     /**
  44.      * @param string $attribute
  45.      * @param Supplier $entity
  46.      *
  47.      * @return bool
  48.      * @throws Exception
  49.      */
  50.     protected function voteOnAttribute($attribute$entityTokenInterface $token): bool
  51.     {
  52.         $userEntity $token->getUser();
  53.         if (! $userEntity instanceof UserEntity) {
  54.             // the user must be logged in; if not, deny access
  55.             return false;
  56.         }
  57.         switch ($attribute) {
  58.             case self::CREATE_GUEST_SUPPLIER:
  59.                 return $this->user->canManageCurrentShopGuestSuppliers();
  60.             case self::MANAGE_GUEST_SUPPLIER:
  61.                 $supplier $this->supplierFactory->createModel();
  62.                 $supplier->setEntity($entity);
  63.                 return $supplier->canBeManagedBy($this->user);
  64.             case self::DECLARE_SUPPLIER_ACCOUNT:
  65.                 $supplier $this->supplierFactory->createModel();
  66.                 $supplier->setEntity($entity);
  67.                 return $supplier->accountCanBeDeclaredBy($this->user);
  68.             case self::VIEW_SUPPLIER:
  69.                 $supplier $this->supplierFactory->createModel();
  70.                 $supplier->setEntity($entity);
  71.                 return $supplier->canBeViewedBy($this->user);
  72.         }
  73.         return false;
  74.     }
  75. }