Completed
Push — develop ( c1e8ff...b43a0e )
by
unknown
57:29 queued 47:29
created

ServiceAllowedVoter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 71.43%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
lcom 2
cbo 1
dl 0
loc 59
rs 10
c 1
b 0
f 0
ccs 5
cts 7
cp 0.7143

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A voteOnAttribute() 0 4 1
A supports() 0 4 2
1
<?php
2
/**
3
 * Voter deciding, if the provided object is
4
 */
5
namespace Graviton\SecurityBundle\Voter;
6
7
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
8
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
9
10
/**
11
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
12
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
13
 * @link     http://swisscom.ch
14
 */
15
class ServiceAllowedVoter extends Voter
16
{
17
    /** @var array List of services always allowed to be called. */
18
    private $whitelist = array();
19
20
    /**
21
     * supported classes
22
     *
23
     * @var array
24
     */
25
    protected $supportedClasses = [
26
        'Symfony\Component\HttpFoundation\Request'
27
    ];
28
29
    /**
30
     * supported attributes
31
     *
32
     * @var array
33
     */
34
    protected $supportedAttributes = [
35
        'VIEW'
36
    ];
37
38
    /**
39
     * @param array $whiteList Set of services to be allowed to be called.
40
     */
41 4
    public function __construct($whiteList = array())
42
    {
43 4
        $this->whitelist = $whiteList;
44 4
    }
45
46
    /**
47
     * Determines if the attribute and subject are supported by this voter.
48
     *
49
     * @param string $attribute An attribute
50
     * @param mixed  $subject   The subject to secure, e.g. an object the user wants to access or any other PHP type
51
     *
52
     * @return bool True if the attribute and subject are supported, false otherwise
53
     */
54
    protected function supports($attribute, $subject)
55
    {
56
        return (isset($this->supportedAttributes[$attribute]) && isset($this->supportedClasses[$subject]));
57
    }
58
59
    /**
60
     * Perform a single access check operation on a given attribute, subject and token.
61
     * It is safe to assume that $attribute and $subject already passed the "supports()" method check.
62
     *
63
     * @param string         $attribute attribute
64
     * @param mixed          $subject   subject
65
     * @param TokenInterface $token     token
66
     *
67
     * @return bool
68
     */
69 2
    protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
70
    {
71 2
        return in_array($subject->getPathInfo(), $this->whitelist);
72
    }
73
}
74