Completed
Pull Request — master (#47)
by Emanuele
06:35
created

FeatureSecurity::isGranted()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6.0359

Importance

Changes 0
Metric Value
cc 6
eloc 9
nc 8
nop 1
dl 0
loc 19
ccs 9
cts 10
cp 0.9
crap 6.0359
rs 9.2222
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 6
    public function __construct(
39
        AuthorizationCheckerInterface $context,
40
        TokenStorageInterface $storage,
41
        string $providerKey
42
    ) {
43 6
        $this->context = $context;
44 6
        $this->storage = $storage;
45 6
        $this->providerKey = $providerKey;
46 6
    }
47
48
    /**
49
     * @param Feature $feature
50
     *
51
     * @return bool
52
     */
53 4
    public function isGranted(Feature $feature)
54
    {
55 4
        if (!$feature->isEnabled()) {
56 1
            return false;
57
        }
58
59 3
        if ($feature->getRole()) {
60 2
            if (!$this->context->isGranted($feature->getRole())) {
61
                return false;
62
            }
63
        }
64
65 3
        if ('' !== trim($feature->getParentRole())) {
66 1
            if (!$this->context->isGranted($feature->getParentRole())) {
67 1
                return false;
68
            }
69
        }
70
71 2
        return true;
72
    }
73
74 2
    public function isGrantedForUser(Feature $feature, UserInterface $user): bool
75
    {
76 2
        $oldToken = $this->storage->getToken();
77
78 2
        $this->storage->setToken(new UsernamePasswordToken(
79 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

79
            /** @scrutinizer ignore-type */ $user,
Loading history...
80 2
            null,
81 2
            $this->providerKey,
82 2
            $user->getRoles()
83
        ));
84
85 2
        $granted = $this->isGranted($feature);
86
87 2
        $this->storage->setToken($oldToken);
88
89 2
        return $granted;
90
    }
91
}
92