Completed
Push — master ( 265a2d...f9e310 )
by jerome
02:58
created

InstallerVoter::vote()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.2
cc 4
eloc 8
nc 4
nop 3
1
<?php
2
3
namespace DP\Core\DistributionBundle\Security;
4
5
use Symfony\Component\DependencyInjection\ContainerInterface;
6
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
7
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
8
9
class InstallerVoter implements VoterInterface
10
{
11
    const ATTR = 'IS_INSTALLER_USER';
12
13
    /** @var \Symfony\Component\HttpFoundation\Request $request */
14
    private $request;
15
16
    /** @var array $whitelisted Contains the list of authorized IP address */
17
    private $whitelisted;
18
19
    public function __construct(ContainerInterface $container, $filepath)
20
    {
21
        $this->request     = $container->get('request');
22
        $this->whitelisted = [];
23
24
        if (file_exists($filepath)) {
25
            $this->whitelisted = explode(PHP_EOL, file_get_contents($filepath));
26
        }
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function supportsAttribute($attribute)
33
    {
34
        return self::ATTR === $attribute;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function supportsClass($class)
41
    {
42
        return true;
43
    }
44
45
    /**
46
     * {@inheritodc}
47
     */
48
    public function vote(TokenInterface $token, $object, array $attributes)
49
    {
50
        foreach ($attributes AS $attribute) {
51
            if (!$this->supportsAttribute($attribute)) {
52
                continue;
53
            }
54
55
            if (in_array($this->request->getClientIp(), $this->whitelisted)) {
56
                return VoterInterface::ACCESS_GRANTED;
57
            }
58
59
            return VoterInterface::ACCESS_DENIED;
60
        }
61
62
        return VoterInterface::ACCESS_ABSTAIN;
63
    }
64
}
65