File tree Expand file tree Collapse file tree 6 files changed +73
-7
lines changed
DependencyInjection/Compiler
Functional/Fixtures/app/config Expand file tree Collapse file tree 6 files changed +73
-7
lines changed 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 12
12
namespace FOS \HttpCacheBundle ;
13
13
14
14
use FOS \HttpCacheBundle \DependencyInjection \Compiler \LoggerPass ;
15
+ use FOS \HttpCacheBundle \DependencyInjection \Compiler \SecurityContextPass ;
15
16
use FOS \HttpCacheBundle \DependencyInjection \Compiler \TagSubscriberPass ;
16
17
use FOS \HttpCacheBundle \DependencyInjection \Compiler \HashGeneratorPass ;
17
18
use Symfony \Component \DependencyInjection \ContainerBuilder ;
@@ -25,6 +26,7 @@ class FOSHttpCacheBundle extends Bundle
25
26
public function build (ContainerBuilder $ container )
26
27
{
27
28
$ container ->addCompilerPass (new LoggerPass ());
29
+ $ container ->addCompilerPass (new SecurityContextPass ());
28
30
$ container ->addCompilerPass (new TagSubscriberPass ());
29
31
$ container ->addCompilerPass (new HashGeneratorPass ());
30
32
}
Original file line number Diff line number Diff line change 31
31
</service >
32
32
33
33
<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" />
35
35
</service >
36
36
37
37
<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 @@ -5,6 +5,10 @@ framework:
5
5
test : ~
6
6
session :
7
7
storage_id : session.test_storage
8
+ # We need to specify templating because of a bug in Symfony:
9
+ # see https://github.com/symfony/symfony/issues/13710
10
+ templating :
11
+ engines : ['php']
8
12
9
13
fos_http_cache :
10
14
cache_control :
Original file line number Diff line number Diff line change @@ -22,8 +22,8 @@ public function testProvider()
22
22
$ roles = array (new Role ('ROLE_USER ' ));
23
23
24
24
$ 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 ();
27
27
$ securityContext ->shouldReceive ('getToken ' )->andReturn ($ token );
28
28
$ token ->shouldReceive ('getRoles ' )->andReturn ($ roles );
29
29
@@ -39,8 +39,7 @@ public function testProvider()
39
39
40
40
public function testProviderWithoutToken ()
41
41
{
42
- $ securityContext = \Mockery::mock ('\Symfony\Component\Security\Core\SecurityContext ' );
43
-
42
+ $ securityContext = $ this ->getTokenStorageMock ();
44
43
$ securityContext ->shouldReceive ('getToken ' )->andReturn (null );
45
44
46
45
$ userContext = new UserContext ();
@@ -59,4 +58,15 @@ public function testNotUnderFirewall()
59
58
$ roleProvider = new RoleProvider ();
60
59
$ roleProvider ->updateUserContext (new UserContext ());
61
60
}
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
+ }
62
72
}
Original file line number Diff line number Diff line change 14
14
use FOS \HttpCache \UserContext \ContextProviderInterface ;
15
15
use FOS \HttpCache \UserContext \UserContext ;
16
16
use Symfony \Component \Config \Definition \Exception \InvalidConfigurationException ;
17
+ use Symfony \Component \Security \Core \Authentication \Token \Storage \TokenStorageInterface ;
17
18
use Symfony \Component \Security \Core \Role \RoleInterface ;
18
19
use Symfony \Component \Security \Core \SecurityContextInterface ;
19
20
@@ -34,10 +35,19 @@ class RoleProvider implements ContextProviderInterface
34
35
* firewall. It is however not valid to call updateUserContext when not in
35
36
* a firewall context.
36
37
*
37
- * @param SecurityContextInterface|null $context
38
+ * @param SecurityContextInterface|TokenStorageInterface $context
38
39
*/
39
- public function __construct (SecurityContextInterface $ context = null )
40
+ public function __construct ($ context = null )
40
41
{
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
+
41
51
$ this ->context = $ context ;
42
52
}
43
53
You can’t perform that action at this time.
0 commit comments