DisableCSRFExtension::configureOptions()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 12
cp 0
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
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\AbstractTypeExtension;
15
use Symfony\Component\Form\Extension\Core\Type\FormType;
16
use Symfony\Component\OptionsResolver\OptionsResolver;
17
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
18
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
19
20
/**
21
 * Class DisableCSRFExtension.
22
 *
23
 * @author Grégoire Pineau
24
 *
25
 * @internal
26
 */
27
class DisableCSRFExtension extends AbstractTypeExtension
28
{
29
    private $tokenStorage;
30
    private $role;
31
    private $authorizationChecker;
32
33
    public function __construct(TokenStorageInterface $tokenStorage, string $role, AuthorizationCheckerInterface $authorizationChecker)
34
    {
35
        $this->tokenStorage = $tokenStorage;
36
        $this->role = $role;
37
        $this->authorizationChecker = $authorizationChecker;
38
    }
39
40
    public function configureOptions(OptionsResolver $resolver): void
41
    {
42
        if (!$this->tokenStorage->getToken()) {
43
            return;
44
        }
45
46
        if (!$this->authorizationChecker->isGranted($this->role)) {
47
            return;
48
        }
49
50
        $resolver->setDefaults([
51
            'csrf_protection' => false,
52
        ]);
53
    }
54
55
    public static function getExtendedTypes(): iterable
56
    {
57
        return array(FormType::class);
58
    }
59
}
60