1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
4
|
|
|
* |
5
|
|
|
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics) |
6
|
|
|
* |
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU Affero General Public License as published |
9
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU Affero General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
18
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace App\Entity\LogSystem; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
use App\Entity\Base\AbstractDBElement; |
25
|
|
|
use App\Entity\UserSystem\User; |
26
|
|
|
use App\Events\SecurityEvents; |
27
|
|
|
use Doctrine\ORM\Mapping as ORM; |
28
|
|
|
use Symfony\Component\HttpFoundation\IpUtils; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* This log entry is created when something security related to a user happens. |
32
|
|
|
* |
33
|
|
|
* @ORM\Entity() |
34
|
|
|
*/ |
35
|
|
|
class SecurityEventLogEntry extends AbstractLogEntry |
36
|
|
|
{ |
37
|
|
|
public const SECURITY_TYPE_MAPPING = [ |
38
|
|
|
0 => SecurityEvents::PASSWORD_CHANGED, |
39
|
|
|
1 => SecurityEvents::PASSWORD_RESET, |
40
|
|
|
2 => SecurityEvents::BACKUP_KEYS_RESET, |
41
|
|
|
3 => SecurityEvents::U2F_ADDED, |
42
|
|
|
4 => SecurityEvents::U2F_REMOVED, |
43
|
|
|
5 => SecurityEvents::GOOGLE_ENABLED, |
44
|
|
|
6 => SecurityEvents::GOOGLE_DISABLED, |
45
|
|
|
7 => SecurityEvents::TRUSTED_DEVICE_RESET, |
46
|
|
|
8 => SecurityEvents::TFA_ADMIN_RESET, |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
public function __construct(string $type, string $ip_address, bool $anonymize = true) |
50
|
|
|
{ |
51
|
|
|
parent::__construct(); |
52
|
|
|
$this->level = self::LEVEL_INFO; |
53
|
|
|
$this->setIPAddress($ip_address, $anonymize); |
54
|
|
|
$this->setEventType($type); |
55
|
|
|
$this->level = self::LEVEL_NOTICE; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function setTargetElement(?AbstractDBElement $element): AbstractLogEntry |
59
|
|
|
{ |
60
|
|
|
if (!$element instanceof User) { |
61
|
|
|
throw new \InvalidArgumentException('Target element must be a User object!'); |
62
|
|
|
} |
63
|
|
|
return parent::setTargetElement($element); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Sets the type of this log entry. |
68
|
|
|
* @param string $type |
69
|
|
|
* @return $this |
70
|
|
|
*/ |
71
|
|
|
public function setEventType(string $type): self |
72
|
|
|
{ |
73
|
|
|
$key = array_search($type, static::SECURITY_TYPE_MAPPING); |
74
|
|
|
if ($key === false) { |
75
|
|
|
throw new \InvalidArgumentException('Given event type is not existing!'); |
76
|
|
|
} |
77
|
|
|
$this->extra['e'] = $key; |
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getType(): string |
82
|
|
|
{ |
83
|
|
|
return $this->getEventType(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Return what event this log entry represents (e.g. password_reset) |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function getEventType(): string |
91
|
|
|
{ |
92
|
|
|
$key = $this->extra['e']; |
93
|
|
|
if (isset(static::SECURITY_TYPE_MAPPING[$key])) { |
94
|
|
|
return static::SECURITY_TYPE_MAPPING[$key]; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return 'unkown'; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Return the (anonymized) IP address used to login the user. |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
public function getIPAddress(): string |
106
|
|
|
{ |
107
|
|
|
return $this->extra['i']; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Sets the IP address used to login the user. |
112
|
|
|
* |
113
|
|
|
* @param string $ip The IP address used to login the user. |
114
|
|
|
* @param bool $anonymize Anonymize the IP address (remove last block) to be GPDR compliant |
115
|
|
|
* |
116
|
|
|
* @return $this |
117
|
|
|
*/ |
118
|
|
|
public function setIPAddress(string $ip, bool $anonymize = true): self |
119
|
|
|
{ |
120
|
|
|
if ($anonymize) { |
121
|
|
|
$ip = IpUtils::anonymize($ip); |
122
|
|
|
} |
123
|
|
|
$this->extra['i'] = $ip; |
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
} |