Test Failed
Pull Request — master (#47)
by Emanuele
02:15
created

FeatureSecurity   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 88.24%

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 80
ccs 15
cts 17
cp 0.8824
rs 10
c 0
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isGrantedForUser() 0 16 1
B isGranted() 0 25 7
A __construct() 0 8 1
1
<?php
2
3
namespace Ae\FeatureBundle\Security;
4
5
use Ae\FeatureBundle\Entity\Feature;
6
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
7
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
8
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
9
use Symfony\Component\Security\Core\User\UserInterface;
10
11
/**
12
 * Controls access to a Feature.
13
 *
14
 * @author Carlo Forghieri <[email protected]>
15
 */
16
class FeatureSecurity
17
{
18
    /**
19
     * @param AuthorizationCheckerInterface
20
     */
21
    protected $context;
22
23 12
    /**
24
     * @param TokenStorageInterface
25 12
     */
26 12
    private $storage;
27
28
    /**
29
     * @param string
30
     */
31
    private $providerKey;
32
33 12
    /**
34
     * @param AuthorizationCheckerInterface $context
35
     * @param TokenStorageInterface         $storage
36
     * @param string                        $providerKey
37 12
     */
38 4
    public function __construct(
39
        AuthorizationCheckerInterface $context,
40
        TokenStorageInterface $storage,
41 8
        string $providerKey
42
    ) {
43
        $this->context = $context;
44
        $this->storage = $storage;
45 8
        $this->providerKey = $providerKey;
46 2
    }
47
48
    /**
49 6
     * @param Feature $feature
50 4
     *
51
     * @return bool
52
     */
53
    public function isGranted(Feature $feature)
54
    {
55 6
        // feature is enabled without required roles
56 2
        // there's no need to check on user roles
57 2
        if (!$feature->requiresRoleCheck()) {
58
            return $feature->isEnabled();
59
        }
60
61 4
        if (!$feature->isEnabled()) {
62
            return false;
63
        }
64
65
        if ($feature->getRole()) {
66
            if (!$this->context->isGranted($feature->getRole())) {
67
                return false;
68
            }
69
        }
70
71
        if ('' !== trim($feature->getParentRole())) {
72
            if (!$this->context->isGranted($feature->getParentRole())) {
73
                return false;
74
            }
75
        }
76
77
        return true;
78
    }
79
80
    public function isGrantedForUser(Feature $feature, UserInterface $user): bool
81
    {
82
        $oldToken = $this->storage->getToken();
83
84
        $this->storage->setToken(new UsernamePasswordToken(
85
            $user,
0 ignored issues
show
Bug introduced by
$user of type Symfony\Component\Security\Core\User\UserInterface is incompatible with the type string expected by parameter $user of Symfony\Component\Securi...ordToken::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

85
            /** @scrutinizer ignore-type */ $user,
Loading history...
86
            null,
87
            $this->providerKey,
88
            $user->getRoles()
89
        ));
90
91
        $granted = $this->isGranted($feature);
92
93
        $this->storage->setToken($oldToken);
94
95
        return $granted;
96
    }
97
}
98