1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSRestBundle 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\RestBundle\Form\Extension; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Form\AbstractType; |
15
|
|
|
use Symfony\Component\Form\AbstractTypeExtension; |
16
|
|
|
use Symfony\Component\Form\Extension\Core\Type\FormType; |
17
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
18
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
19
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class DisableCSRFExtension. |
23
|
|
|
* |
24
|
|
|
* @author Grégoire Pineau |
25
|
|
|
*/ |
26
|
|
|
class DisableCSRFExtension extends AbstractTypeExtension |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var TokenStorageInterface |
30
|
|
|
*/ |
31
|
|
|
private $tokenStorage; |
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $role; |
36
|
|
|
/** |
37
|
|
|
* @var AuthorizationCheckerInterface |
38
|
|
|
*/ |
39
|
|
|
private $authorizationChecker; |
40
|
|
|
|
41
|
|
|
public function __construct(TokenStorageInterface $tokenStorage, $role, AuthorizationCheckerInterface $authorizationChecker) |
42
|
|
|
{ |
43
|
|
|
$this->tokenStorage = $tokenStorage; |
44
|
|
|
$this->role = $role; |
45
|
|
|
$this->authorizationChecker = $authorizationChecker; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function configureOptions(OptionsResolver $resolver) |
49
|
|
|
{ |
50
|
|
|
if (!$this->tokenStorage->getToken()) { |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (!$this->authorizationChecker->isGranted($this->role)) { |
55
|
|
|
return; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$resolver->setDefaults([ |
59
|
|
|
'csrf_protection' => false, |
60
|
|
|
]); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function getExtendedType() |
64
|
|
|
{ |
65
|
|
|
return method_exists(AbstractType::class, 'getBlockPrefix') |
66
|
|
|
? FormType::class |
67
|
|
|
: 'form' // SF <2.8 BC |
68
|
|
|
; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public static function getExtendedTypes() |
72
|
|
|
{ |
73
|
|
|
return array(FormType::class); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|