Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Bridge][Twig] Decouple the SecurityExtension from its runtime #24692

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion 8 src/Symfony/Bridge/Twig/Extension/SecurityExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs a docblock with @deprecated

{
@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;
}
Expand All @@ -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')),
);
}

Expand Down
48 changes: 48 additions & 0 deletions 48 src/Symfony/Bridge/Twig/Extension/SecurityRuntime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <fabien@symfony.com>
*/
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

<service id="twig.extension.security" class="Symfony\Bridge\Twig\Extension\SecurityExtension">
<tag name="twig.extension" />
</service>

<service id="twig.runtime.security" class="Symfony\Bridge\Twig\Extension\SecurityRuntime">
<tag name="twig.runtime" />
<argument type="service" id="security.authorization_checker" on-invalid="ignore" />
</service>
</services>
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.