Completed
Push — master ( ee8444...5701cb )
by Christian
30s
created

DisableCSRFExtension::getExtendedTypes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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