14
14
use Symfony \Component \DependencyInjection \ContainerInterface ;
15
15
use Symfony \Component \Security \Core \Authentication \Token \AnonymousToken ;
16
16
use Symfony \Component \Security \Core \Authentication \Token \TokenInterface ;
17
+ use Symfony \Component \Security \Core \Authentication \Token \TokenStorageInterface ;
17
18
use Symfony \Component \Security \Core \Authorization \AccessDecisionManagerInterface ;
19
+ use Symfony \Component \Security \Core \Authorization \AuthorizationCheckerInterface ;
18
20
use Symfony \Component \Security \Core \SecurityContextInterface ;
19
21
20
22
/**
@@ -75,6 +77,16 @@ class PublishWorkflowChecker implements SecurityContextInterface
75
77
*/
76
78
private $ token ;
77
79
80
+ /**
81
+ * @var TokenStorageInterface
82
+ */
83
+ private $ tokenStorage = false ;
84
+
85
+ /**
86
+ * @var AuthorizationCheckerInterface
87
+ */
88
+ private $ authorizationChecker = false ;
89
+
78
90
/**
79
91
* @param ContainerInterface $container To get the security context from.
80
92
* @param AccessDecisionManagerInterface $accessDecisionManager Service to do the actual decision.
@@ -98,16 +110,15 @@ public function __construct(ContainerInterface $container, AccessDecisionManager
98
110
*/
99
111
public function getToken ()
100
112
{
101
- if (null === $ this ->token ) {
102
- if ($ this ->container ->has ('security.token_storage ' )) {
103
- return $ this ->container ->get ('security.token_storage ' )->getToken ();
104
- } elseif ($ this ->container ->has ('security.context ' )) {
105
- // to be BC with Symfony 2.3
106
- return $ this ->container ->get ('security.context ' )->getToken ();
107
- }
113
+ if (null !== $ this ->token ) {
114
+ return $ this ->token ;
115
+ }
116
+
117
+ if (null === $ this ->getTokenStorage ()) {
118
+ return ;
108
119
}
109
120
110
- return $ this ->token ;
121
+ return $ this ->getTokenStorage ()-> getToken () ;
111
122
}
112
123
113
124
/**
@@ -139,20 +150,11 @@ public function isGranted($attributes, $object = null)
139
150
$ attributes = array ($ attributes );
140
151
}
141
152
142
- $ tokenStorage = $ authorizationChecker = null ;
143
- if ($ this ->container ->has ('security.token_storage ' )) {
144
- $ tokenStorage = $ this ->container ->get ('security.token_storage ' );
145
- $ authorizationChecker = $ this ->container ->get ('security.authorization_checker ' );
146
- } elseif ($ this ->container ->has ('security.context ' )) {
147
- // to be BC with Symfony <2.6
148
- $ authorizationChecker = $ tokenStorage = $ this ->container ->get ('security.context ' );
149
- }
150
-
151
- if ((count ($ attributes ) === 1 )
153
+ if (1 === count ($ attributes )
152
154
&& self ::VIEW_ATTRIBUTE === reset ($ attributes )
153
- && null !== $ tokenStorage
154
- && null !== $ tokenStorage ->getToken ()
155
- && $ authorizationChecker ->isGranted ($ this ->bypassingRole )
155
+ && null !== $ this -> getTokenStorage ()
156
+ && null !== $ this -> getTokenStorage () ->getToken ()
157
+ && $ this -> getAuthorizationChecker () ->isGranted ($ this ->bypassingRole )
156
158
) {
157
159
return true ;
158
160
}
@@ -166,4 +168,36 @@ public function isGranted($attributes, $object = null)
166
168
167
169
return $ this ->accessDecisionManager ->decide ($ token , $ attributes , $ object );
168
170
}
171
+
172
+ private function getTokenStorage ()
173
+ {
174
+ if (false === $ this ->tokenStorage ) {
175
+ if ($ this ->container ->has ('security.token_storage ' )) {
176
+ $ this ->tokenStorage = $ this ->container ->get ('security.token_storage ' );
177
+ } elseif ($ this ->container ->has ('security.context ' )) {
178
+ // for Symfony <2.6 compatibility
179
+ $ this ->tokenStorage = $ this ->container ->get ('security.context ' );
180
+ } else {
181
+ $ this ->tokenStorage = null ;
182
+ }
183
+ }
184
+
185
+ return $ this ->tokenStorage ;
186
+ }
187
+
188
+ private function getAuthorizationChecker ()
189
+ {
190
+ if (false === $ this ->authorizationChecker ) {
191
+ if ($ this ->container ->has ('security.authorization_checker ' )) {
192
+ $ this ->authorizationChecker = $ this ->container ->get ('security.authorization_checker ' );
193
+ } elseif ($ this ->container ->has ('security.context ' )) {
194
+ // for Symfony <2.6 compatibility
195
+ $ this ->authorizationChecker = $ this ->container ->get ('security.context ' );
196
+ } else {
197
+ $ this ->authorizationChecker = null ;
198
+ }
199
+ }
200
+
201
+ return $ this ->authorizationChecker ;
202
+ }
169
203
}
0 commit comments