vendor/symfony/twig-bridge/Extension/SecurityExtension.php line 36

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Bridge\Twig\Extension;
  11. use Symfony\Component\Security\Acl\Voter\FieldVote;
  12. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  13. use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
  14. use Twig\Extension\AbstractExtension;
  15. use Twig\TwigFunction;
  16. /**
  17.  * SecurityExtension exposes security context features.
  18.  *
  19.  * @author Fabien Potencier <fabien@symfony.com>
  20.  *
  21.  * @final since Symfony 4.4
  22.  */
  23. class SecurityExtension extends AbstractExtension
  24. {
  25.     private $securityChecker;
  26.     public function __construct(AuthorizationCheckerInterface $securityChecker null)
  27.     {
  28.         $this->securityChecker $securityChecker;
  29.     }
  30.     public function isGranted($role$object null$field null)
  31.     {
  32.         if (null === $this->securityChecker) {
  33.             return false;
  34.         }
  35.         if (null !== $field) {
  36.             $object = new FieldVote($object$field);
  37.         }
  38.         try {
  39.             return $this->securityChecker->isGranted($role$object);
  40.         } catch (AuthenticationCredentialsNotFoundException $e) {
  41.             return false;
  42.         }
  43.     }
  44.     /**
  45.      * {@inheritdoc}
  46.      *
  47.      * @return TwigFunction[]
  48.      */
  49.     public function getFunctions()
  50.     {
  51.         return [
  52.             new TwigFunction('is_granted', [$this'isGranted']),
  53.         ];
  54.     }
  55.     /**
  56.      * {@inheritdoc}
  57.      */
  58.     public function getName()
  59.     {
  60.         return 'security';
  61.     }
  62. }