Completed
Push — feature/EVO-6305_worker_authen... ( 500766...23d108 )
by Bastian
61:47
created

SameSubnetStrategy::getRoles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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
    /**
25
     * @param String $subnet Subnet to be checked (e.g. 10.2.0.0/24)
26 4
     */
27
    public function __construct($subnet)
28 4
    {
29 4
        $this->subnet= $subnet;
30
    }
31
32
    /**
33
     * Applies the defined strategy on the provided request.
34
     *
35
     * @param Request $request request to handle
36
     *
37
     * @return string
38
     */
39
    public function apply(Request $request)
40
    {
41
        if (IpUtils::checkIp($request->getClientIp(), $this->subnet)) {
42
            return 'graviton_subnet_user';
43
        }
44
45
        throw new \InvalidArgumentException('Provided request information are not valid.');
46
    }
47
48
    /**
49
     * Decider to stop other strategies running after from being considered.
50
     *
51
     * @return boolean
52
     */
53
    public function stopPropagation()
54
    {
55
        return false;
56
    }
57
58
    /**
59
     * Provides the list of registered roles.
60
     *
61
     * @return Role[]
62
     */
63
    public function getRoles()
64
    {
65
        return [SecurityUser::ROLE_SUBNET];
66
    }
67
}
68