vendor/symfony/src/Symfony/Bundle/SecurityBundle/Templating/Helper/SecurityHelper.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\Templating\Helper;
       
    13 
       
    14 use Symfony\Component\Security\Acl\Voter\FieldVote;
       
    15 use Symfony\Component\Templating\Helper\Helper;
       
    16 use Symfony\Component\Security\Core\SecurityContextInterface;
       
    17 
       
    18 /**
       
    19  * SecurityHelper provides read-only access to the security context.
       
    20  *
       
    21  * @author Fabien Potencier <fabien@symfony.com>
       
    22  */
       
    23 class SecurityHelper extends Helper
       
    24 {
       
    25     private $context;
       
    26 
       
    27     /**
       
    28      * Constructor.
       
    29      *
       
    30      * @param SecurityContextInterface $context A SecurityContext instance
       
    31      */
       
    32     public function __construct(SecurityContextInterface $context = null)
       
    33     {
       
    34         $this->context = $context;
       
    35     }
       
    36 
       
    37     public function isGranted($role, $object = null, $field = null)
       
    38     {
       
    39         if (null === $this->context) {
       
    40             return false;
       
    41         }
       
    42 
       
    43         if (null !== $field) {
       
    44             $object = new FieldVote($object, $field);
       
    45         }
       
    46 
       
    47         return $this->context->isGranted($role, $object);
       
    48     }
       
    49 
       
    50     /**
       
    51      * Returns the canonical name of this helper.
       
    52      *
       
    53      * @return string The canonical name
       
    54      */
       
    55     public function getName()
       
    56     {
       
    57         return 'security';
       
    58     }
       
    59 }