Skip to content

Commit 78f4d9a

Browse files
committed
feature #59129 [SecurityBundle][TwigBridge] Add is_granted_for_user() function (natewiebe13)
This PR was merged into the 7.3 branch. Discussion ---------- [SecurityBundle][TwigBridge] Add `is_granted_for_user()` function | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | N/A | License | MIT Twig function to accompany #48142 Commits ------- 82ff3a5 Add is_granted_for_user() function to twig
2 parents b23badd + 82ff3a5 commit 78f4d9a

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

src/Symfony/Bridge/Twig/CHANGELOG.md

+5
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

+23-1
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) {
@@ -86,12 +102,18 @@ public function getImpersonatePath(string $identifier): string
86102

87103
public function getFunctions(): array
88104
{
89-
return [
105+
$functions = [
90106
new TwigFunction('is_granted', $this->isGranted(...)),
91107
new TwigFunction('impersonation_exit_url', $this->getImpersonateExitUrl(...)),
92108
new TwigFunction('impersonation_exit_path', $this->getImpersonateExitPath(...)),
93109
new TwigFunction('impersonation_url', $this->getImpersonateUrl(...)),
94110
new TwigFunction('impersonation_path', $this->getImpersonatePath(...)),
95111
];
112+
113+
if ($this->userSecurityChecker) {
114+
$functions[] = new TwigFunction('is_granted_for_user', $this->isGrantedForUser(...));
115+
}
116+
117+
return $functions;
96118
}
97119
}

src/Symfony/Bridge/Twig/UndefinedCallableHandler.php

+1
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

+1
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)