Skip to content

Navigation Menu

Sign in
Appearance settings

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

Commit 82ff3a5

Browse filesBrowse files
natewiebe13Nate Wiebe
authored and
Nate Wiebe
committed
Add is_granted_for_user() function to twig
1 parent dd882db commit 82ff3a5
Copy full SHA for 82ff3a5

File tree

4 files changed

+30
-1
lines changed
Filter options

4 files changed

+30
-1
lines changed

‎src/Symfony/Bridge/Twig/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/CHANGELOG.md
+5Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Extension/SecurityExtension.php
+23-1Lines changed: 23 additions & 1 deletion
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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/UndefinedCallableHandler.php
+1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/SecurityBundle/Resources/config/templating_twig.php
+1Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.