Passed
Push — master ( 2dd807...470cd2 )
by Jan
04:52
created

SecurityEventLoggerSubscriber::u2f_removed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 3
rs 10
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\EventSubscriber;
22
23
24
use App\Entity\LogSystem\SecurityEventLogEntry;
25
use App\Events\SecurityEvent;
26
use App\Events\SecurityEvents;
27
use App\Services\LogSystem\EventLogger;
28
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
29
use Symfony\Component\HttpFoundation\Request;
30
use Symfony\Component\HttpFoundation\RequestStack;
31
32
final class SecurityEventLoggerSubscriber implements EventSubscriberInterface
33
{
34
35
    private $requestStack;
36
    private $gpdr_compliant;
37
    private $eventLogger;
38
39
    public function __construct(RequestStack $requestStack, EventLogger $eventLogger, bool $gpdr_compliance)
40
    {
41
        $this->requestStack = $requestStack;
42
        $this->gpdr_compliant = $gpdr_compliance;
43
        $this->eventLogger = $eventLogger;
44
    }
45
46
    protected function addLog(string $type, SecurityEvent $event): void
47
    {
48
        $anonymize = $this->gpdr_compliant;
49
50
        $request = $this->requestStack->getCurrentRequest();
51
        if ($request !== null) {
52
            $ip = $request->getClientIp() ?? 'unknown';
53
        } else {
54
            $ip = "Console";
55
            //Dont try to apply IP filter rules to non numeric string
56
            $anonymize = false;
57
        }
58
59
        $log = new SecurityEventLogEntry($type, $ip, $anonymize);
60
        $log->setTargetElement($event->getTargetUser());
61
        $this->eventLogger->logAndFlush($log);
62
    }
63
64
    /**
65
     * @inheritDoc
66
     */
67
    public static function getSubscribedEvents()
68
    {
69
        return [
70
            SecurityEvents::U2F_ADDED => 'u2f_added',
71
            SecurityEvents::PASSWORD_CHANGED => 'password_changed',
72
            SecurityEvents::TRUSTED_DEVICE_RESET => 'trusted_device_reset',
73
            SecurityEvents::U2F_REMOVED => 'u2f_removed',
74
            SecurityEvents::BACKUP_KEYS_RESET => 'backup_keys_reset',
75
            SecurityEvents::PASSWORD_RESET => 'password_reset',
76
            SecurityEvents::GOOGLE_DISABLED => 'google_disabled',
77
            SecurityEvents::GOOGLE_ENABLED => 'google_enabled',
78
            SecurityEvents::TFA_ADMIN_RESET => 'tfa_admin_reset',
79
        ];
80
    }
81
82
    public function tfa_admin_reset(SecurityEvent $event): void
83
    {
84
        $this->addLog(SecurityEvents::TFA_ADMIN_RESET, $event);
85
    }
86
87
    public function google_enabled(SecurityEvent $event): void
88
    {
89
        $this->addLog(SecurityEvents::GOOGLE_ENABLED, $event);
90
    }
91
92
    public function google_disabled(SecurityEvent $event): void
93
    {
94
        $this->addLog(SecurityEvents::GOOGLE_DISABLED, $event);
95
    }
96
97
    public function password_reset(SecurityEvent $event): void
98
    {
99
        $this->addLog(SecurityEvents::PASSWORD_RESET, $event);
100
    }
101
102
    public function backup_keys_reset(SecurityEvent $event): void
103
    {
104
        $this->addLog(SecurityEvents::BACKUP_KEYS_RESET, $event);
105
    }
106
107
    public function u2f_removed(SecurityEvent $event): void
108
    {
109
        $this->addLog(SecurityEvents::U2F_REMOVED, $event);
110
    }
111
112
    public function u2f_added(SecurityEvent $event): void
113
    {
114
        $this->addLog(SecurityEvents::U2F_ADDED, $event);
115
    }
116
117
    public function password_changed(SecurityEvent $event): void
118
    {
119
        $this->addLog(SecurityEvents::PASSWORD_CHANGED, $event);
120
    }
121
122
    public function trusted_device_reset(SecurityEvent $event): void
123
    {
124
        $this->addLog(SecurityEvents::TRUSTED_DEVICE_RESET, $event);
125
    }
126
}