Skip to content

Commit ae3b09c

Browse files
natewiebe13Nate Wiebe
authored and
Nate Wiebe
committed
Add is_granted_for_user() function to twig
1 parent dd882db commit ae3b09c

File tree

4 files changed

+24
-0
lines changed

4 files changed

+24
-0
lines changed

src/Symfony/Bridge/Twig/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.3
5+
---
6+
7+
* Add `is_granted_for_user()` Twig function
8+
49
7.2
510
---
611

src/Symfony/Bridge/Twig/Extension/SecurityExtension.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
use Symfony\Component\Security\Acl\Voter\FieldVote;
1515
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
16+
use Symfony\Component\Security\Core\Authorization\UserAuthorizationCheckerInterface;
1617
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
18+
use Symfony\Component\Security\Core\User\UserInterface;
1719
use Symfony\Component\Security\Http\Impersonate\ImpersonateUrlGenerator;
1820
use Twig\Extension\AbstractExtension;
1921
use Twig\TwigFunction;
@@ -28,6 +30,7 @@ final class SecurityExtension extends AbstractExtension
2830
public function __construct(
2931
private ?AuthorizationCheckerInterface $securityChecker = null,
3032
private ?ImpersonateUrlGenerator $impersonateUrlGenerator = null,
33+
private ?UserAuthorizationCheckerInterface $userSecurityChecker = null,
3134
) {
3235
}
3336

@@ -48,6 +51,19 @@ public function isGranted(mixed $role, mixed $object = null, ?string $field = nu
4851
}
4952
}
5053

54+
public function isGrantedForUser(UserInterface $user, mixed $attribute, mixed $subject = null, ?string $field = null): bool
55+
{
56+
if (!$this->userSecurityChecker) {
57+
throw new \LogicException(\sprintf('An instance of "%s" must be provided to use "%s()".', UserAuthorizationCheckerInterface::class, __METHOD__));
58+
}
59+
60+
if ($field) {
61+
$subject = new FieldVote($subject, $field);
62+
}
63+
64+
return $this->userSecurityChecker->isGrantedForUser($user, $attribute, $subject);
65+
}
66+
5167
public function getImpersonateExitUrl(?string $exitTo = null): string
5268
{
5369
if (null === $this->impersonateUrlGenerator) {
@@ -88,6 +104,7 @@ public function getFunctions(): array
88104
{
89105
return [
90106
new TwigFunction('is_granted', $this->isGranted(...)),
107+
new TwigFunction('is_granted_for_user', $this->isGrantedForUser(...)),
91108
new TwigFunction('impersonation_exit_url', $this->getImpersonateExitUrl(...)),
92109
new TwigFunction('impersonation_exit_path', $this->getImpersonateExitPath(...)),
93110
new TwigFunction('impersonation_url', $this->getImpersonateUrl(...)),

src/Symfony/Bridge/Twig/UndefinedCallableHandler.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class UndefinedCallableHandler
6161
'logout_url' => 'security-http',
6262
'logout_path' => 'security-http',
6363
'is_granted' => 'security-core',
64+
'is_granted_for_user' => 'security-core',
6465
'impersonation_path' => 'security-http',
6566
'impersonation_url' => 'security-http',
6667
'impersonation_exit_path' => 'security-http',

src/Symfony/Bundle/SecurityBundle/Resources/config/templating_twig.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
->args([
2727
service('security.authorization_checker')->ignoreOnInvalid(),
2828
service('security.impersonate_url_generator')->ignoreOnInvalid(),
29+
service('security.user_authorization_checker')->ignoreOnInvalid(),
2930
])
3031
->tag('twig.extension')
3132
;

0 commit comments

Comments
 (0)