DisableCSRFExtension   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 33
ccs 0
cts 22
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configureOptions() 0 14 3
A getExtendedTypes() 0 4 1
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