diff --git a/src/Symfony/Bridge/Twig/Extension/SecurityExtension.php b/src/Symfony/Bridge/Twig/Extension/SecurityExtension.php index 193726a684371..be1b91bbebb36 100644 --- a/src/Symfony/Bridge/Twig/Extension/SecurityExtension.php +++ b/src/Symfony/Bridge/Twig/Extension/SecurityExtension.php @@ -28,11 +28,17 @@ class SecurityExtension extends AbstractExtension public function __construct(AuthorizationCheckerInterface $securityChecker = null) { + if (null !== $securityChecker) { + @trigger_error(sprintf('The $securityChecker parameter is deprecated since 3.4 and will be removed in 4.0. Use %s instead.', SecurityRuntime::class), E_USER_DEPRECATED); + } + $this->securityChecker = $securityChecker; } public function isGranted($role, $object = null, $field = null) { + @trigger_error(sprintf('The %s() method is deprecated since version 3.4 and will be removed in 4.0. Use the %s::%s() method instead.', __METHOD__, SecurityRuntime::class, __FUNCTION__), E_USER_DEPRECATED); + if (null === $this->securityChecker) { return false; } @@ -54,7 +60,7 @@ public function isGranted($role, $object = null, $field = null) public function getFunctions() { return array( - new TwigFunction('is_granted', array($this, 'isGranted')), + new TwigFunction('is_granted', array($this->securityChecker ? $this : SecurityRuntime::class, 'isGranted')), ); } diff --git a/src/Symfony/Bridge/Twig/Extension/SecurityRuntime.php b/src/Symfony/Bridge/Twig/Extension/SecurityRuntime.php new file mode 100644 index 0000000000000..777acf5f47f13 --- /dev/null +++ b/src/Symfony/Bridge/Twig/Extension/SecurityRuntime.php @@ -0,0 +1,48 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Twig\Extension; + +use Symfony\Component\Security\Acl\Voter\FieldVote; +use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; +use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException; + +/** + * SecurityRuntime exposes security context features. + * + * @author Fabien Potencier + */ +class SecurityRuntime +{ + private $securityChecker; + + public function __construct(AuthorizationCheckerInterface $securityChecker = null) + { + $this->securityChecker = $securityChecker; + } + + public function isGranted($role, $object = null, $field = null) + { + if (null === $this->securityChecker) { + return false; + } + + if (null !== $field) { + $object = new FieldVote($object, $field); + } + + try { + return $this->securityChecker->isGranted($role, $object); + } catch (AuthenticationCredentialsNotFoundException $e) { + return false; + } + } +} diff --git a/src/Symfony/Bundle/SecurityBundle/Resources/config/templating_twig.xml b/src/Symfony/Bundle/SecurityBundle/Resources/config/templating_twig.xml index a575ca2fa768f..17a15403b92c9 100644 --- a/src/Symfony/Bundle/SecurityBundle/Resources/config/templating_twig.xml +++ b/src/Symfony/Bundle/SecurityBundle/Resources/config/templating_twig.xml @@ -14,6 +14,10 @@ + + + +