1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* The MIT License (MIT) |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2014-2015 Spomky-Labs |
7
|
|
|
* |
8
|
|
|
* This software may be modified and distributed under the terms |
9
|
|
|
* of the MIT license. See the LICENSE file for details. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace SpomkyLabs\IpFilterBundle\Model; |
13
|
|
|
|
14
|
|
|
use SpomkyLabs\IpFilterBundle\Tool\Network; |
15
|
|
|
use Symfony\Bridge\Doctrine\RegistryInterface; |
16
|
|
|
|
17
|
|
|
class RangeManager implements RangeManagerInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var \Doctrine\Common\Persistence\ObjectManager |
21
|
|
|
*/ |
22
|
|
|
protected $entity_manager; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $class; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var \Doctrine\Common\Persistence\ObjectRepository |
31
|
|
|
*/ |
32
|
|
|
protected $entity_repository; |
33
|
|
|
|
34
|
|
|
public function __construct(RegistryInterface $registry, $class) |
35
|
|
|
{ |
36
|
|
|
$this->class = $class; |
37
|
|
|
$this->entity_manager = $registry->getManagerForClass($class); |
38
|
|
|
$this->entity_repository = $this->entity_manager->getRepository($class); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function findByIpAddress($ip, $environment) |
45
|
|
|
{ |
46
|
|
|
return $this->getEntityRepository()->createQueryBuilder('r') |
47
|
|
|
->where('r.start_ip <= :ip') |
48
|
|
|
->andWhere('r.end_ip >= :ip') |
49
|
|
|
->andWhere("r.environment LIKE :environment OR r.environment='a:0:{}'") |
50
|
|
|
->orderBy('r.authorized', 'DESC') |
51
|
|
|
->setParameter('ip', $ip) |
52
|
|
|
->setParameter('environment', "%$environment%") |
53
|
|
|
->getQuery() |
54
|
|
|
->execute(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function createRangeFromNetwork($network) |
58
|
|
|
{ |
59
|
|
|
$range = $this->createRange(); |
60
|
|
|
$values = Network::getRange($network); |
61
|
|
|
$range->setStartIp($values['start']); |
62
|
|
|
$range->setEndIp($values['end']); |
63
|
|
|
|
64
|
|
|
return $range; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return \Doctrine\Common\Persistence\ObjectManager |
69
|
|
|
*/ |
70
|
|
|
protected function getEntityManager() |
71
|
|
|
{ |
72
|
|
|
return $this->entity_manager; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
|
|
protected function getEntityRepository() |
79
|
|
|
{ |
80
|
|
|
return $this->entity_repository; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
|
public function createRange() |
87
|
|
|
{ |
88
|
|
|
$class = $this->class; |
89
|
|
|
|
90
|
|
|
return new $class(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
|
|
public function saveRange(RangeInterface $range) |
97
|
|
|
{ |
98
|
|
|
$this->getEntityManager()->persist($range); |
99
|
|
|
$this->getEntityManager()->flush(); |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
|
|
public function deleteRange(RangeInterface $range) |
108
|
|
|
{ |
109
|
|
|
$this->getEntityManager()->remove($range); |
110
|
|
|
$this->getEntityManager()->flush(); |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|