forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecurityLogger.php
More file actions
103 lines (89 loc) · 3.05 KB
/
SecurityLogger.php
File metadata and controls
103 lines (89 loc) · 3.05 KB
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
<?php
namespace ProcessMaker\Listeners;
use Illuminate\Support\Facades\Auth;
use ProcessMaker\Contracts\SecurityLogEventInterface;
use ProcessMaker\Helpers\SensitiveDataHelper;
use ProcessMaker\Models\SecurityLog;
use WhichBrowser\Parser;
class SecurityLogger
{
private $eventTypes = [
'Illuminate\Auth\Events\Failed' => 'attempt',
'Illuminate\Auth\Events\Login' => 'login',
'Illuminate\Auth\Events\Logout' => 'logout',
];
/**
* Handle the event.
*
* @param \Illuminate\Auth\Events\Failed|\Illuminate\Auth\Events\Login|\Illuminate\Auth\Events\Logout|SecurityLogEventInterface $event
* @return void
*/
public function handle($event)
{
$class = get_class($event);
if ($event instanceof SecurityLogEventInterface) {
$data = $event->getData();
$changes = $event->getChanges();
SecurityLog::create([
'event' => $event->getEventName(),
'ip' => request()->ip(),
'meta' => $this->getMeta(),
'user_id' => isset($event->user) ? $event->user->id : Auth::id(),
'data' => $data ? SensitiveDataHelper::parseArray($data) : null,
'changes' => $changes ? SensitiveDataHelper::parseArray($changes) : null,
]);
} elseif (array_key_exists($class, $this->eventTypes)) {
$eventType = $this->eventTypes[$class];
SecurityLog::create([
'event' => $eventType,
'ip' => request()->ip(),
'meta' => $this->getMeta(),
'user_id' => isset($event->user) ? $event->user->id : null,
]);
}
}
private function getMeta()
{
$userAgent = $this->userAgent();
return [
'user_agent' => $userAgent->string,
'browser' => [
'name' => $userAgent->browser->name,
'version' => $userAgent->browser->version,
],
'os' => [
'name' => $userAgent->os->name,
'version' => $userAgent->os->version,
],
];
}
private function userAgent()
{
$string = request()->headers->get('User-Agent');
$parsed = new Parser($string);
$object = (object) [
'string' => $string,
'browser' => (object) [
'name' => null,
'version' => null,
],
'os' => (object) [
'name' => null,
'version' => null,
],
];
if (isset($parsed->browser->name)) {
$object->browser->name = $parsed->browser->name;
}
if (isset($parsed->browser->version)) {
$object->browser->version = $parsed->browser->version->toString();
}
if (isset($parsed->os->name)) {
$object->os->name = $parsed->os->name;
}
if (isset($parsed->os->version)) {
$object->os->version = $parsed->os->version->toString();
}
return $object;
}
}