|
12 | 12 | namespace FOS\RestBundle\Form\Extension; |
13 | 13 |
|
14 | 14 | use Symfony\Component\Form\AbstractTypeExtension; |
| 15 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
15 | 16 | use Symfony\Component\OptionsResolver\OptionsResolverInterface; |
16 | 17 | use Symfony\Component\Security\Core\SecurityContextInterface; |
| 18 | +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
| 19 | +use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
17 | 20 |
|
18 | 21 | /** |
19 | 22 | * Class DisableCSRFExtension |
|
22 | 25 | */ |
23 | 26 | class DisableCSRFExtension extends AbstractTypeExtension |
24 | 27 | { |
25 | | - private $securityContext; |
| 28 | + /** |
| 29 | + * @var SecurityContextInterface|TokenStorageInterface |
| 30 | + */ |
| 31 | + private $tokenStorage; |
26 | 32 | private $role; |
| 33 | + private $authorizationChecker; |
27 | 34 |
|
28 | | - public function __construct(SecurityContextInterface $securityContext, $role) |
| 35 | + public function __construct($tokenStorage, $role, $authorizationChecker = null) |
29 | 36 | { |
30 | | - $this->securityContext = $securityContext; |
| 37 | + $this->tokenStorage = $tokenStorage; |
31 | 38 | $this->role = $role; |
| 39 | + $this->authorizationChecker = $authorizationChecker; |
| 40 | + |
| 41 | + if (!$tokenStorage instanceof TokenStorageInterface && !$tokenStorage instanceof SecurityContextInterface) { |
| 42 | + throw new \InvalidArgumentException('Argument 1 should be an instance of Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface or Symfony\Component\Security\Core\SecurityContextInterface'); |
| 43 | + } |
32 | 44 | } |
33 | 45 |
|
34 | | - public function setDefaultOptions(OptionsResolverInterface $resolver) |
| 46 | + public function configureOptions(OptionsResolver $resolver) |
35 | 47 | { |
36 | | - if (!$this->securityContext->getToken()) { |
37 | | - return; |
38 | | - } |
| 48 | + if ($this->authorizationChecker instanceof AuthorizationCheckerInterface) { |
| 49 | + if (!$this->tokenStorage->getToken()) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + if (!$this->authorizationChecker->isGranted($this->role)) { |
| 54 | + return; |
| 55 | + } |
| 56 | + } else { |
| 57 | + if (!$this->tokenStorage->getToken()) { |
| 58 | + return; |
| 59 | + } |
39 | 60 |
|
40 | | - if (!$this->securityContext->isGranted($this->role)) { |
41 | | - return; |
| 61 | + if (!$this->tokenStorage->isGranted($this->role)) { |
| 62 | + return; |
| 63 | + } |
42 | 64 | } |
43 | 65 |
|
44 | 66 | $resolver->setDefaults(array( |
45 | 67 | 'csrf_protection' => false, |
46 | 68 | )); |
47 | 69 | } |
48 | 70 |
|
| 71 | + // BC for < 2.7 |
| 72 | + public function setDefaultOptions(OptionsResolverInterface $resolver) |
| 73 | + { |
| 74 | + $this->configureOptions($resolver); |
| 75 | + } |
| 76 | + |
49 | 77 | public function getExtendedType() |
50 | 78 | { |
51 | 79 | return 'form'; |
|
0 commit comments