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 bf8a1bf

Browse filesBrowse files
natewiebe13Nate Wiebe
authored and
Nate Wiebe
committed
Add user_is_granted() function to twig
1 parent 4612ff2 commit bf8a1bf
Copy full SHA for bf8a1bf

File tree

4 files changed

+24
-0
lines changed
Filter options

4 files changed

+24
-0
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 `user_is_granted()` 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
+17Lines 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

@@ -84,6 +87,19 @@ public function getImpersonatePath(string $identifier): string
8487
return $this->impersonateUrlGenerator->generateImpersonationPath($identifier);
8588
}
8689

90+
public function userIsGranted(UserInterface $user, mixed $attribute, mixed $subject = null, ?string $field = null): bool
91+
{
92+
if (null === $this->userSecurityChecker) {
93+
return false;
94+
}
95+
96+
if (null !== $field) {
97+
$subject = new FieldVote($subject, $field);
98+
}
99+
100+
return $this->userSecurityChecker->userIsGranted($user, $attribute, $subject);
101+
}
102+
87103
public function getFunctions(): array
88104
{
89105
return [
@@ -92,6 +108,7 @@ public function getFunctions(): array
92108
new TwigFunction('impersonation_exit_path', $this->getImpersonateExitPath(...)),
93109
new TwigFunction('impersonation_url', $this->getImpersonateUrl(...)),
94110
new TwigFunction('impersonation_path', $this->getImpersonatePath(...)),
111+
new TwigFunction('user_is_granted', $this->userIsGranted(...)),
95112
];
96113
}
97114
}

‎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
@@ -72,6 +72,7 @@ class UndefinedCallableHandler
7272
'preconnect' => 'web-link',
7373
'prefetch' => 'web-link',
7474
'prerender' => 'web-link',
75+
'user_is_granted' => 'security-core',
7576
'workflow_can' => 'workflow',
7677
'workflow_transitions' => 'workflow',
7778
'workflow_transition' => 'workflow',

‎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.