vendor/symfony/src/Symfony/Bundle/SecurityBundle/Twig/Extension/SecurityExtension.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony package.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.com>
       
     7  *
       
     8  * For the full copyright and license information, please view the LICENSE
       
     9  * file that was distributed with this source code.
       
    10  */
       
    11 
       
    12 namespace Symfony\Bundle\SecurityBundle\Twig\Extension;
       
    13 
       
    14 use Symfony\Component\Security\Acl\Voter\FieldVote;
       
    15 use Symfony\Component\Security\Core\SecurityContextInterface;
       
    16 
       
    17 /**
       
    18  * SecurityExtension exposes security context features.
       
    19  *
       
    20  * @author Fabien Potencier <fabien@symfony.com>
       
    21  */
       
    22 class SecurityExtension extends \Twig_Extension
       
    23 {
       
    24     private $context;
       
    25 
       
    26     public function __construct(SecurityContextInterface $context = null)
       
    27     {
       
    28         $this->context = $context;
       
    29     }
       
    30 
       
    31     public function isGranted($role, $object = null, $field = null)
       
    32     {
       
    33         if (null === $this->context) {
       
    34             return false;
       
    35         }
       
    36 
       
    37         if (null !== $field) {
       
    38             $object = new FieldVote($object, $field);
       
    39         }
       
    40 
       
    41         return $this->context->isGranted($role, $object);
       
    42     }
       
    43 
       
    44     /**
       
    45      * {@inheritdoc}
       
    46      */
       
    47     public function getFunctions()
       
    48     {
       
    49         return array(
       
    50             'is_granted' => new \Twig_Function_Method($this, 'isGranted'),
       
    51         );
       
    52     }
       
    53 
       
    54     /**
       
    55      * Returns the name of the extension.
       
    56      *
       
    57      * @return string The extension name
       
    58      */
       
    59     public function getName()
       
    60     {
       
    61         return 'security';
       
    62     }
       
    63 }