Skip to content

Commit

Permalink
Add is_granted_for_user() function to twig
Browse files Browse the repository at this point in the history
  • Loading branch information
natewiebe13 authored and Nate Wiebe committed Dec 17, 2024
1 parent dd882db commit f00878d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Symfony/Bridge/Twig/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.3
---

* Add `is_granted_for_user()` Twig function

7.2
---

Expand Down
17 changes: 17 additions & 0 deletions src/Symfony/Bridge/Twig/Extension/SecurityExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

use Symfony\Component\Security\Acl\Voter\FieldVote;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Authorization\UserAuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Http\Impersonate\ImpersonateUrlGenerator;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
Expand All @@ -28,6 +30,7 @@ final class SecurityExtension extends AbstractExtension
public function __construct(
private ?AuthorizationCheckerInterface $securityChecker = null,
private ?ImpersonateUrlGenerator $impersonateUrlGenerator = null,
private ?UserAuthorizationCheckerInterface $userSecurityChecker = null,
) {
}

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

public function isGrantedForUser(UserInterface $user, mixed $attribute, mixed $subject = null, ?string $field = null): bool
{
if (!$this->userSecurityChecker) {
return false;
}

if (!$field) {
$subject = new FieldVote($subject, $field);
}

return $this->userSecurityChecker->isGrantedForUser($user, $attribute, $subject);
}

public function getImpersonateExitUrl(?string $exitTo = null): string
{
if (null === $this->impersonateUrlGenerator) {
Expand Down Expand Up @@ -88,6 +104,7 @@ public function getFunctions(): array
{
return [
new TwigFunction('is_granted', $this->isGranted(...)),
new TwigFunction('is_granted_for_user', $this->isGrantedForUser(...)),
new TwigFunction('impersonation_exit_url', $this->getImpersonateExitUrl(...)),
new TwigFunction('impersonation_exit_path', $this->getImpersonateExitPath(...)),
new TwigFunction('impersonation_url', $this->getImpersonateUrl(...)),
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bridge/Twig/UndefinedCallableHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class UndefinedCallableHandler
'logout_url' => 'security-http',
'logout_path' => 'security-http',
'is_granted' => 'security-core',
'is_granted_for_user' => 'security-core',
'impersonation_path' => 'security-http',
'impersonation_url' => 'security-http',
'impersonation_exit_path' => 'security-http',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
->args([
service('security.authorization_checker')->ignoreOnInvalid(),
service('security.impersonate_url_generator')->ignoreOnInvalid(),
service('security.user_authorization_checker')->ignoreOnInvalid(),
])
->tag('twig.extension')
;
Expand Down

0 comments on commit f00878d

Please sign in to comment.