src/AppBundle/Security/FieldVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace AppBundle\Security;
  3. use AppBundle\Entity\Field\Field;
  4. use AppBundle\Manager\FieldManager;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. /**
  8.  * Class FieldVoter.
  9.  */
  10. class FieldVoter extends Voter
  11. {
  12.     const EDIT 'EDIT';
  13.     const DELETE 'DELETE';
  14.     /**
  15.      * @var FieldManager
  16.      */
  17.     private $fieldManager;
  18.     /**
  19.      * FieldVoter constructor.
  20.      * @param FieldManager $fieldManager
  21.      */
  22.     public function __construct(FieldManager $fieldManager)
  23.     {
  24.         $this->fieldManager $fieldManager;
  25.     }
  26.     /**
  27.      * {@inheritdoc}
  28.      */
  29.     protected function supports($attribute$subject)
  30.     {
  31.         return (self::EDIT === $attribute || self::DELETE === $attribute)
  32.             && $subject instanceof Field;
  33.     }
  34.     /**
  35.      * {@inheritdoc}
  36.      * @param Field $subject
  37.      */
  38.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  39.     {
  40.         switch ($attribute) {
  41.             case self::EDIT:
  42.                 return true;
  43.             case self::DELETE:
  44.                 if ($subject->isLocked()) {
  45.                     return false;
  46.                 }
  47.                 return !$this->fieldManager->isFieldUsed($subject);
  48.             default:
  49.                 throw new \Exception(sprintf('Attribute "%s" not supported.'$attribute));
  50.         }
  51.     }
  52. }