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

FeatureSecurity::isGrantedForUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 2
dl 0
loc 16
ccs 0
cts 0
cp 0
crap 2
rs 9.9666
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
10
/**
11
 * Controls access to a Feature.
12
 *
13
 * @author Carlo Forghieri <[email protected]>
14
 */
15
class FeatureSecurity
16
{
17
    /**
18
     * @param AuthorizationCheckerInterface
19
     */
20
    protected $context;
21
22
    /**
23 8
     * @param TokenStorageInterface
24
     */
25 8
    private $storage;
26 8
27
    /**
28
     * @param string
29
     */
30
    private $providerKey;
31
32
    /**
33 8
     * @param AuthorizationCheckerInterface $context
34
     * @param TokenStorageInterface         $storage
35 8
     * @param string                        $providerKey
36
     */
37
    public function __construct(
38
        AuthorizationCheckerInterface $context,
39 8
        TokenStorageInterface $storage,
40 2
        string $providerKey
41
    ) {
42
        $this->context = $context;
43 6
        $this->storage = $storage;
44 4
        $this->providerKey = $providerKey;
45
    }
46
47
    /**
48
     * @param Feature $feature
49 6
     *
50 2
     * @return bool
51 2
     */
52
    public function isGranted(Feature $feature)
53
    {
54
        if (!$feature->isEnabled()) {
55 4
            return false;
56
        }
57
58
        if ($feature->getRole()) {
59
            if (!$this->context->isGranted($feature->getRole())) {
60
                return false;
61
            }
62
        }
63
64
        if ('' !== trim($feature->getParentRole())) {
65
            if (!$this->context->isGranted($feature->getParentRole())) {
66
                return false;
67
            }
68
        }
69
70
        return true;
71
    }
72
73
    public function isGrantedForUser(Feature $feature, UserInterface $user): bool
0 ignored issues
show
Bug introduced by
The type Ae\FeatureBundle\Security\UserInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
74
    {
75
        $oldToken = $this->storage->getToken();
76
77
        $this->storage->setToken(new UsernamePasswordToken(
78
            $user,
79
            null,
80
            $this->providerKey,
81
            $user->getRoles()
82
        ));
83
84
        $granted = $this->isGranted($feature);
85
86
        $this->storage->setToken($oldToken);
87
88
        return $granted;
89
    }
90
}
91