1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Zikula package. |
7
|
|
|
* |
8
|
|
|
* Copyright Zikula Foundation - https://ziku.la/ |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Zikula\SecurityCenterModule\Entity\Repository; |
15
|
|
|
|
16
|
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; |
17
|
|
|
use Doctrine\ORM\QueryBuilder; |
18
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
19
|
|
|
use InvalidArgumentException; |
20
|
|
|
use Zikula\Bundle\CoreBundle\Doctrine\Paginator; |
21
|
|
|
use Zikula\SecurityCenterModule\Entity\IntrusionEntity; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Repository class used to implement own convenience methods for performing certain DQL queries. |
25
|
|
|
* |
26
|
|
|
* This is the repository class for intrusion entities. |
27
|
|
|
*/ |
28
|
|
|
class IntrusionRepository extends ServiceEntityRepository |
29
|
|
|
{ |
30
|
|
|
public function __construct(ManagerRegistry $registry) |
31
|
|
|
{ |
32
|
|
|
parent::__construct($registry, IntrusionEntity::class); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Returns intrusions for given arguments. |
37
|
|
|
*/ |
38
|
|
|
public function getIntrusions(array $filters = [], array $sorting = [], int $page = 1, int $pageSize = 25): Paginator |
39
|
|
|
{ |
40
|
|
|
$qb = $this->createQueryBuilder('tbl') |
41
|
|
|
->select('tbl'); |
42
|
|
|
|
43
|
|
|
$qb = $this->addCommonFilters($qb, $filters); |
44
|
|
|
|
45
|
|
|
// add clause for ordering |
46
|
|
|
if (isset($sorting['username'])) { |
47
|
|
|
$sortdir = $sorting['username']; |
48
|
|
|
unset($sorting['username']); |
49
|
|
|
|
50
|
|
|
$qb->from('ZikulaUsersModule:UserEntity', 'u'); |
51
|
|
|
$qb->andWhere($qb->expr()->eq('tbl.user', 'u.uid')); |
52
|
|
|
$qb->addOrderBy('u.uname', $sortdir); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (count($sorting) > 0) { |
56
|
|
|
foreach ($sorting as $sort => $sortdir) { |
57
|
|
|
$qb->addOrderBy('tbl.' . $sort, $sortdir); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return (new Paginator($qb, $pageSize))->paginate($page); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Adds common filters to the given query builder. |
66
|
|
|
*/ |
67
|
|
|
private function addCommonFilters(QueryBuilder $qb, array $filters = []): QueryBuilder |
68
|
|
|
{ |
69
|
|
|
// add clause for user |
70
|
|
|
if (isset($filters['uid'])) { |
71
|
|
|
$uid = $filters['uid']; |
72
|
|
|
unset($filters['uid']); |
73
|
|
|
|
74
|
|
|
if ($uid > 0) { |
75
|
|
|
$qb->from('ZikulaUsersModule:UserEntity', 'u'); |
76
|
|
|
$qb->andWhere($qb->expr()->eq('tbl.user', 'u.uid')); |
77
|
|
|
$qb->andWhere($qb->expr()->eq('tbl.user', ':uid')) |
78
|
|
|
->setParameter('uid', $uid); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// add clauses for where |
83
|
|
|
if (count($filters) > 0) { |
84
|
|
|
$i = 1; |
85
|
|
|
foreach ($filters as $w_key => $w_value) { |
86
|
|
|
$qb->andWhere($qb->expr()->eq('tbl.' . $w_key, '?' . $i)) |
87
|
|
|
->setParameter($i, $w_value); |
88
|
|
|
$i++; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $qb; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Selects a list of distinct values for a given field. |
97
|
|
|
* |
98
|
|
|
* @throws InvalidArgumentException Thrown if invalid parameters are received |
99
|
|
|
*/ |
100
|
|
|
public function getDistinctFieldValues(string $fieldName): array |
101
|
|
|
{ |
102
|
|
|
if (!in_array($fieldName, ['uid', 'name', 'tag', 'value', 'page', 'ip', 'impact'])) { |
103
|
|
|
throw new InvalidArgumentException('Invalid field name received for distinct values selection!'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$qb = $this->createQueryBuilder('tbl'); |
107
|
|
|
|
108
|
|
|
if ('uid' === $fieldName) { |
109
|
|
|
$qb->select('DISTINCT(u.uname)') |
110
|
|
|
->from('ZikulaUsersModule:UserEntity', 'u') |
111
|
|
|
->where($qb->expr()->eq('tbl.user', 'u.uid')) |
112
|
|
|
->addOrderBy('u.uname', 'ASC'); |
113
|
|
|
} else { |
114
|
|
|
$qb->select('DISTINCT(tbl.' . $fieldName . ')') |
115
|
|
|
->addOrderBy('tbl.' . $fieldName, 'ASC'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $qb->getQuery()->getResult(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Helper method for truncating the table. |
123
|
|
|
*/ |
124
|
|
|
public function truncateTable(): void |
125
|
|
|
{ |
126
|
|
|
$qb = $this->_em->createQueryBuilder('tbl') |
|
|
|
|
127
|
|
|
->delete(IntrusionEntity::class, 'tbl'); |
128
|
|
|
$query = $qb->getQuery(); |
129
|
|
|
|
130
|
|
|
$query->execute(); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.