|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Application\Repository; |
|
6
|
|
|
|
|
7
|
|
|
use Cake\Chronos\Chronos; |
|
8
|
|
|
use Doctrine\DBAL\Connection; |
|
9
|
|
|
use Zend\Log\Logger; |
|
10
|
|
|
|
|
11
|
|
|
class LogRepository extends AbstractRepository |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Log message to be used when user log in |
|
15
|
|
|
*/ |
|
16
|
|
|
const LOGIN = 'login'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Log message to be used when user cannot log in |
|
20
|
|
|
*/ |
|
21
|
|
|
const LOGIN_FAILED = 'login failed'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Log message to be used when user change his password |
|
25
|
|
|
*/ |
|
26
|
|
|
const UPDATE_PASSWORD = 'update password'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Log message to be used when user cannot change his password |
|
30
|
|
|
*/ |
|
31
|
|
|
const UPDATE_PASSWORD_FAILED = 'update password failed'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* This should NOT be called directly, instead use `_log()` to log stuff |
|
35
|
|
|
* |
|
36
|
|
|
* @param array $event |
|
37
|
|
|
*/ |
|
38
|
5 |
|
public function log(array $event): void |
|
39
|
|
|
{ |
|
40
|
5 |
|
$event['creation_date'] = Chronos::instance($event['creation_date'])->toIso8601String(); |
|
41
|
5 |
|
$event['extra'] = json_encode($event['extra']); |
|
42
|
|
|
|
|
43
|
5 |
|
$this->getEntityManager()->getConnection()->insert('log', $event); |
|
44
|
5 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Returns whether the current IP often failed to login |
|
48
|
|
|
* |
|
49
|
|
|
* @return bool |
|
50
|
|
|
*/ |
|
51
|
2 |
|
public function loginFailedOften(): bool |
|
52
|
|
|
{ |
|
53
|
2 |
|
return $this->failedOften(self::LOGIN, self::LOGIN_FAILED); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
3 |
|
public function updatePasswordFailedOften(): bool |
|
57
|
|
|
{ |
|
58
|
3 |
|
return $this->failedOften(self::UPDATE_PASSWORD, self::UPDATE_PASSWORD_FAILED); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
5 |
|
private function failedOften(string $success, string $failed): bool |
|
62
|
|
|
{ |
|
63
|
5 |
|
if (PHP_SAPI === 'cli') { |
|
64
|
5 |
|
$ip = 'script'; |
|
65
|
|
|
} else { |
|
66
|
|
|
$ip = $_SERVER['REMOTE_ADDR'] ?? ''; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
5 |
|
$select = $this->getEntityManager()->getConnection()->createQueryBuilder() |
|
70
|
5 |
|
->select('message') |
|
71
|
5 |
|
->from('log') |
|
72
|
5 |
|
->andWhere('priority = :priority') |
|
73
|
5 |
|
->setParameter('priority', Logger::INFO) |
|
74
|
5 |
|
->andWhere('message IN (:message)') |
|
75
|
5 |
|
->setParameter('message', [$success, $failed], Connection::PARAM_STR_ARRAY) |
|
76
|
5 |
|
->andWhere('creation_date > DATE_SUB(NOW(), INTERVAL 30 MINUTE)') |
|
77
|
5 |
|
->andWhere('ip = :ip') |
|
78
|
5 |
|
->setParameter('ip', $ip) |
|
79
|
5 |
|
->orderBy('id', 'DESC'); |
|
80
|
|
|
|
|
81
|
5 |
|
$events = $select->execute()->fetchAll(\PDO::FETCH_COLUMN); |
|
82
|
|
|
|
|
83
|
|
|
// Goes from present to past and count failure, until the last time we succeeded logging in |
|
84
|
5 |
|
$failureCount = 0; |
|
85
|
5 |
|
foreach ($events as $event) { |
|
86
|
2 |
|
if ($event === $success) { |
|
87
|
|
|
break; |
|
88
|
|
|
} |
|
89
|
2 |
|
++$failureCount; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
5 |
|
return $failureCount > 5; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Delete log entries which are errors/warnings and older than one month |
|
97
|
|
|
* We always keep Logger::INFO level because we use it for statistics |
|
98
|
|
|
* |
|
99
|
|
|
* @return int the count deleted logs |
|
100
|
|
|
*/ |
|
101
|
1 |
|
public function deleteOldLogs(): int |
|
102
|
|
|
{ |
|
103
|
1 |
|
$connection = $this->getEntityManager()->getConnection(); |
|
104
|
1 |
|
$query = $connection->createQueryBuilder() |
|
105
|
1 |
|
->delete('log') |
|
106
|
1 |
|
->andWhere('log.priority != :priority OR message = :message') |
|
107
|
1 |
|
->setParameter('priority', Logger::INFO) |
|
108
|
1 |
|
->setParameter('message', self::LOGIN_FAILED) |
|
109
|
1 |
|
->andWhere('log.creation_date < DATE_SUB(NOW(), INTERVAL 1 MONTH)'); |
|
110
|
|
|
|
|
111
|
1 |
|
$connection->query('LOCK TABLES `log` WRITE;'); |
|
112
|
1 |
|
$count = $query->execute(); |
|
113
|
1 |
|
$connection->query('UNLOCK TABLES;'); |
|
114
|
|
|
|
|
115
|
1 |
|
return $count; |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|