Passed
Push — master ( e8f83f...f116c2 )
by Jan
05:37
created

SecurityEventLoggerSubscriber::addLog()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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