13
13
14
14
use Symfony \Component \Security \Acl \Voter \FieldVote ;
15
15
use Symfony \Component \Security \Core \Authorization \AuthorizationCheckerInterface ;
16
+ use Symfony \Component \Security \Core \Authorization \UserAuthorizationCheckerInterface ;
16
17
use Symfony \Component \Security \Core \Exception \AuthenticationCredentialsNotFoundException ;
18
+ use Symfony \Component \Security \Core \User \UserInterface ;
17
19
use Symfony \Component \Security \Http \Impersonate \ImpersonateUrlGenerator ;
18
20
use Twig \Extension \AbstractExtension ;
19
21
use Twig \TwigFunction ;
@@ -28,6 +30,7 @@ final class SecurityExtension extends AbstractExtension
28
30
public function __construct (
29
31
private ?AuthorizationCheckerInterface $ securityChecker = null ,
30
32
private ?ImpersonateUrlGenerator $ impersonateUrlGenerator = null ,
33
+ private ?UserAuthorizationCheckerInterface $ userSecurityChecker = null ,
31
34
) {
32
35
}
33
36
@@ -48,6 +51,19 @@ public function isGranted(mixed $role, mixed $object = null, ?string $field = nu
48
51
}
49
52
}
50
53
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
+
51
67
public function getImpersonateExitUrl (?string $ exitTo = null ): string
52
68
{
53
69
if (null === $ this ->impersonateUrlGenerator ) {
@@ -88,6 +104,7 @@ public function getFunctions(): array
88
104
{
89
105
return [
90
106
new TwigFunction ('is_granted ' , $ this ->isGranted (...)),
107
+ new TwigFunction ('is_granted_for_user ' , $ this ->isGrantedForUser (...)),
91
108
new TwigFunction ('impersonation_exit_url ' , $ this ->getImpersonateExitUrl (...)),
92
109
new TwigFunction ('impersonation_exit_path ' , $ this ->getImpersonateExitPath (...)),
93
110
new TwigFunction ('impersonation_url ' , $ this ->getImpersonateUrl (...)),
0 commit comments