Passed
Push — master ( 3408ae...8f5960 )
by Emanuele
37s queued 10s
created

FeatureSecurity::isGranted()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 25
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 7.0283

Importance

Changes 0
Metric Value
cc 7
eloc 11
nc 9
nop 1
dl 0
loc 25
ccs 11
cts 12
cp 0.9167
crap 7.0283
rs 8.8333
c 0
b 0
f 0
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
    /**
24
     * @param TokenStorageInterface
25
     */
26
    private $storage;
27
28
    /**
29
     * @param string
30
     */
31
    private $providerKey;
32
33
    /**
34
     * @param AuthorizationCheckerInterface $context
35
     * @param TokenStorageInterface         $storage
36
     * @param string                        $providerKey
37
     */
38 8
    public function __construct(
39
        AuthorizationCheckerInterface $context,
40
        TokenStorageInterface $storage,
41
        string $providerKey
42
    ) {
43 8
        $this->context = $context;
44 8
        $this->storage = $storage;
45 8
        $this->providerKey = $providerKey;
46 8
    }
47
48
    /**
49
     * @param Feature $feature
50
     *
51
     * @return bool
52
     */
53 6
    public function isGranted(Feature $feature)
54
    {
55
        // feature is enabled without required roles
56
        // there's no need to check on user roles
57 6
        if (!$feature->requiresRoleCheck()) {
58 2
            return $feature->isEnabled();
59
        }
60
61 4
        if (!$feature->isEnabled()) {
62 1
            return false;
63
        }
64
65 3
        if ($feature->getRole()) {
66 2
            if (!$this->context->isGranted($feature->getRole())) {
67
                return false;
68
            }
69
        }
70
71 3
        if ('' !== trim($feature->getParentRole())) {
72 1
            if (!$this->context->isGranted($feature->getParentRole())) {
73 1
                return false;
74
            }
75
        }
76
77 2
        return true;
78
    }
79
80 2
    public function isGrantedForUser(Feature $feature, UserInterface $user): bool
81
    {
82 2
        $oldToken = $this->storage->getToken();
83
84 2
        $this->storage->setToken(new UsernamePasswordToken(
85 2
            $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 2
            null,
87 2
            $this->providerKey,
88 2
            $user->getRoles()
89
        ));
90
91 2
        $granted = $this->isGranted($feature);
92
93 2
        $this->storage->setToken($oldToken);
94
95 2
        return $granted;
96
    }
97
}
98