File tree Expand file tree Collapse file tree
DependencyInjection/Compiler Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ <?php
2+
3+ /*
4+ * This file is part of the FOSHttpCacheBundle package.
5+ *
6+ * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+ *
8+ * For the full copyright and license information, please view the LICENSE
9+ * file that was distributed with this source code.
10+ */
11+
12+ namespace FOS \HttpCacheBundle \DependencyInjection \Compiler ;
13+
14+ use Symfony \Component \DependencyInjection \Compiler \CompilerPassInterface ;
15+ use Symfony \Component \DependencyInjection \ContainerBuilder ;
16+ use Symfony \Component \DependencyInjection \Reference ;
17+
18+ /**
19+ * In Symfony < 2.6, replace the new security.token_storage service with the
20+ * deprecated security.context service.
21+ */
22+ class SecurityContextPass implements CompilerPassInterface
23+ {
24+ const ROLE_PROVIDER_SERVICE = 'fos_http_cache.user_context.role_provider ' ;
25+
26+ /**
27+ * {@inheritdoc}
28+ */
29+ public function process (ContainerBuilder $ container )
30+ {
31+ if (!$ container ->has (self ::ROLE_PROVIDER_SERVICE )) {
32+ return ;
33+ }
34+
35+ if (!$ container ->has ('security.token_storage ' )) {
36+ $ definition = $ container ->getDefinition (self ::ROLE_PROVIDER_SERVICE );
37+ $ definition ->replaceArgument (0 , new Reference ('security.context ' ));
38+ }
39+ }
40+ }
Original file line number Diff line number Diff line change 1212namespace FOS \HttpCacheBundle ;
1313
1414use FOS \HttpCacheBundle \DependencyInjection \Compiler \LoggerPass ;
15+ use FOS \HttpCacheBundle \DependencyInjection \Compiler \SecurityContextPass ;
1516use FOS \HttpCacheBundle \DependencyInjection \Compiler \TagSubscriberPass ;
1617use FOS \HttpCacheBundle \DependencyInjection \Compiler \HashGeneratorPass ;
1718use Symfony \Component \DependencyInjection \ContainerBuilder ;
@@ -25,6 +26,7 @@ class FOSHttpCacheBundle extends Bundle
2526 public function build (ContainerBuilder $ container )
2627 {
2728 $ container ->addCompilerPass (new LoggerPass ());
29+ $ container ->addCompilerPass (new SecurityContextPass ());
2830 $ container ->addCompilerPass (new TagSubscriberPass ());
2931 $ container ->addCompilerPass (new HashGeneratorPass ());
3032 }
Original file line number Diff line number Diff line change 3131 </service >
3232
3333 <service id =" fos_http_cache.user_context.role_provider" class =" %fos_http_cache.user_context.role_provider.class%" abstract =" true" >
34- <argument type =" service" id =" security.context " on-invalid =" ignore" />
34+ <argument type =" service" id =" security.token_storage " on-invalid =" ignore" />
3535 </service >
3636
3737 <service id =" fos_http_cache.user_context.logout_handler" class =" %fos_http_cache.user_context.logout_handler.class%" >
Original file line number Diff line number Diff line change @@ -22,8 +22,8 @@ public function testProvider()
2222 $ roles = array (new Role ('ROLE_USER ' ));
2323
2424 $ token = \Mockery::mock ('Symfony\Component\Security\Core\Authentication\Token\TokenInterface ' );
25- $ securityContext = \Mockery:: mock ( ' \Symfony\Component\Security\Core\SecurityContext ' );
26-
25+
26+ $ securityContext = $ this -> getTokenStorageMock ();
2727 $ securityContext ->shouldReceive ('getToken ' )->andReturn ($ token );
2828 $ token ->shouldReceive ('getRoles ' )->andReturn ($ roles );
2929
@@ -39,8 +39,7 @@ public function testProvider()
3939
4040 public function testProviderWithoutToken ()
4141 {
42- $ securityContext = \Mockery::mock ('\Symfony\Component\Security\Core\SecurityContext ' );
43-
42+ $ securityContext = $ this ->getTokenStorageMock ();
4443 $ securityContext ->shouldReceive ('getToken ' )->andReturn (null );
4544
4645 $ userContext = new UserContext ();
@@ -59,4 +58,15 @@ public function testNotUnderFirewall()
5958 $ roleProvider = new RoleProvider ();
6059 $ roleProvider ->updateUserContext (new UserContext ());
6160 }
61+
62+ private function getTokenStorageMock ()
63+ {
64+ if (interface_exists ('\Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface ' )) {
65+ // Symfony >= 2.6
66+ return \Mockery::mock ('\Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface ' );
67+ }
68+
69+ // Symfony < 2.6 compatibility
70+ return \Mockery::mock ('\Symfony\Component\Security\Core\SecurityContext ' );
71+ }
6272}
Original file line number Diff line number Diff line change 1414use FOS \HttpCache \UserContext \ContextProviderInterface ;
1515use FOS \HttpCache \UserContext \UserContext ;
1616use Symfony \Component \Config \Definition \Exception \InvalidConfigurationException ;
17+ use Symfony \Component \Security \Core \Authentication \Token \Storage \TokenStorageInterface ;
1718use Symfony \Component \Security \Core \Role \RoleInterface ;
1819use Symfony \Component \Security \Core \SecurityContextInterface ;
1920
@@ -34,10 +35,19 @@ class RoleProvider implements ContextProviderInterface
3435 * firewall. It is however not valid to call updateUserContext when not in
3536 * a firewall context.
3637 *
37- * @param SecurityContextInterface|null $context
38+ * @param SecurityContextInterface|TokenStorageInterface $context
3839 */
39- public function __construct (SecurityContextInterface $ context = null )
40+ public function __construct ($ context = null )
4041 {
42+ if ($ context
43+ && !$ context instanceof SecurityContextInterface
44+ && !$ context instanceof TokenStorageInterface
45+ ) {
46+ throw new \InvalidArgumentException (
47+ 'Context must implement either TokenStorageInterface or SecurityContextInterface '
48+ );
49+ }
50+
4151 $ this ->context = $ context ;
4252 }
4353
You can’t perform that action at this time.
0 commit comments