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

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 class SecurityExtension extends AbstractExtension
  22. {
  23.     private $securityChecker;
  24.     public function __construct(AuthorizationCheckerInterface $securityChecker null)
  25.     {
  26.         $this->securityChecker $securityChecker;
  27.     }
  28.     /**
  29.      * @param mixed $object
  30.      */
  31.     public function isGranted($role$object nullstring $field null): bool
  32.     {
  33.         if (null === $this->securityChecker) {
  34.             return false;
  35.         }
  36.         if (null !== $field) {
  37.             $object = new FieldVote($object$field);
  38.         }
  39.         try {
  40.             return $this->securityChecker->isGranted($role$object);
  41.         } catch (AuthenticationCredentialsNotFoundException $e) {
  42.             return false;
  43.         }
  44.     }
  45.     /**
  46.      * {@inheritdoc}
  47.      */
  48.     public function getFunctions(): array
  49.     {
  50.         return [
  51.             new TwigFunction('is_granted', [$this'isGranted']),
  52.         ];
  53.     }
  54. }