Completed
Pull Request — develop (#619)
by
unknown
04:25
created

SameSubnetStrategy::determineName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * strategy for validating auth through the ip address.
4
 */
5
6
namespace Graviton\SecurityBundle\Authentication\Strategies;
7
8
use Graviton\SecurityBundle\Entities\SecurityUser;
9
use Symfony\Component\HttpFoundation\IpUtils;
10
use Symfony\Component\HttpFoundation\Request;
11
12
/**
13
 * Class SameSubnetStrategy
14
 *
15
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
16
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
17
 * @link     http://swisscom.ch
18
 */
19
class SameSubnetStrategy extends AbstractHttpStrategy
20
{
21
    /** @var String */
22
    protected $subnet;
23
24
    /** @var String */
25
    protected $headerField;
26
27
    /**
28
     * @param String $subnet      Subnet to be checked (e.g. 10.2.0.0/24)
29
     * @param String $headerField Http header field to be searched for the 'username'
30
     */
31 2
    public function __construct($subnet, $headerField = 'x-graviton-authentication')
32
    {
33 2
        $this->subnet= $subnet;
34 2
        $this->headerField = $headerField;
35 2
    }
36
37
    /**
38
     * Ip subnet check
39
     * @param string $subnet IpAddress
40
     * @return void
41
     */
42
    public function setSubnetIp($subnet)
43
    {
44
        $this->subnet = $subnet;
45
    }
46
47
    /**
48
     * Applies the defined strategy on the provided request.
49
     *
50
     * @param Request $request request to handle
51
     *
52
     * @return string
53
     */
54 2
    public function apply(Request $request)
55
    {
56 2
        $ip = $request->getClientIp();
57 2
        if (IpUtils::checkIp($request->getClientIp(), $this->subnet)) {
58 2
            $name = $this->extractFieldInfo($request->headers, $this->headerField);
59 2
            if (!empty($name)) {
60 2
                return $name;
61
            }
62
        }
63
64
        return '';
65
    }
66
67
    /**
68
     * Provides the list of registered roles.
69
     *
70
     * @return Role[]
71
     */
72 2
    public function getRoles()
73
    {
74 2
        return [SecurityUser::ROLE_USER, SecurityUser::ROLE_SUBNET];
75
    }
76
}
77