/
LoginAttemptRepository.php
149 lines (127 loc) · 4.34 KB
/
LoginAttemptRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
namespace Concrete\Core\Entity\User;
use Carbon\Carbon;
use Concrete\Core\User\UserInfo;
use DateTime;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\UnexpectedResultException;
class LoginAttemptRepository extends EntityRepository
{
/**
* Get a list of login attempts prior to a date
*
* @param \DateTime $before Must be in UTC
* @param \Concrete\Core\Entity\User\User|int $user
* @param bool $count Whether we return an integer count, or an iterator of matches
*
* @return \Iterator|\Concrete\Core\Entity\User\LoginAttempt[]|int
*/
public function before(DateTime $before, $user = null, $count = false)
{
// Validate and normalize input
$before = $this->validateTimezone($before);
$user = $this->validateUser($user);
// Build the query builder
$qb = $this->createQueryBuilder('a');
if ($count) {
$qb->select('count(a)');
} else {
$qb->select();
}
$qb->where('a.utcDate < :before')->setParameter('before', $before);
// Pivot on user id if needed
if ($user) {
$qb->andWhere('a.userId=:user')->setParameter('user', $user);
}
if ($count) {
try {
return $qb->getQuery()->getSingleScalarResult();
} catch (UnexpectedResultException $e) {
return 0;
}
} else {
// Return an iterator that contains all login attempts before a given date
return $qb->getQuery()->iterate();
}
}
/**
* Get a list of login attempts after a given date
*
* @param \DateTime $after Must be in UTC
* @param \Concrete\Core\Entity\User\User|int $user
* @param bool $count Whether we return an integer count, or an iterator of matches
*
* @return \Iterator|\Concrete\Core\Entity\User\LoginAttempt[]|int
*/
public function after(DateTime $after, $user = null, $count = false)
{
// Validate and normalize input
$after = $this->validateTimezone($after);
$user = $this->validateUser($user);
// Build the query builder
$qb = $this->createQueryBuilder('a');
if ($count) {
$qb->select('count(a)');
} else {
$qb->select();
}
$qb->where('a.utcDate > :after')->setParameter('after', $after->getTimestamp());
// Pivot on user id if needed
if ($user) {
$qb->andWhere('a.userId=:user')->setParameter('user', $user);
}
if ($count) {
try {
return $qb->getQuery()->getSingleScalarResult();
} catch (UnexpectedResultException $e) {
return 0;
}
} else {
// Return an iterator that contains all login attempts before a given date
return $qb->getQuery()->iterate();
}
}
/**
* @param \DateTime $dateTime
*
* @return \Carbon\Carbon
*
* @throws \InvalidArgumentException
*/
private function validateTimezone(DateTime $dateTime)
{
$dateTime = Carbon::instance($dateTime);
// Make sure we have the proper timezone. This is to prevent a developer accidentally removing more than they
// expect to remove
if ($dateTime->getTimezone()->getName() !== 'UTC') {
throw new \InvalidArgumentException('Passed datetime is not in UTC timezone.');
}
return $dateTime;
}
/**
* Validate a passed user value and resolve the ID
*
* @param mixed $user
* @param bool $requireValue Whether a user value is required
*
* @return int|null
*
* @throws \InvalidArgumentException
*/
private function validateUser($user, $requireValue = false)
{
// If we're passed something falsy, just return null
if (!$user && !$requireValue) {
return null;
}
// If we have a known supported type, resolve the id and return it
if ($user instanceof User || $user instanceof UserInfo) {
return (int) $user->getUserID();
}
// If what we're left with is numeric, return it as an int
if (is_numeric($user)) {
return (int) $user;
}
throw new \InvalidArgumentException('Invalid user value passed, must be User instance or int');
}
}