|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* To manage the data to be saved into DB as last thing to do. |
|
4
|
|
|
*/ |
|
5
|
|
|
namespace Graviton\AuditTrackingBundle\Manager; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
|
8
|
|
|
use Graviton\AuditTrackingBundle\Document\AuditTracking; |
|
9
|
|
|
use Symfony\Bridge\Doctrine\ManagerRegistry; |
|
10
|
|
|
use Doctrine\Bundle\MongoDBBundle\ManagerRegistry as Doctrine; |
|
11
|
|
|
|
|
12
|
|
|
class StoreManager |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var ActivityManager */ |
|
15
|
|
|
private $activityManager; |
|
16
|
|
|
|
|
17
|
|
|
/** @var AuditTracking */ |
|
18
|
|
|
private $documentCollection; |
|
19
|
|
|
|
|
20
|
|
|
/** @var DocumentManager */ |
|
21
|
|
|
private $documentManager; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Avoid saving same action again |
|
25
|
|
|
* @var |
|
26
|
|
|
*/ |
|
27
|
|
|
private $saved; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct($activityManager, AuditTracking $documentCollection, Doctrine $doctrine) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->activityManager = $activityManager; |
|
32
|
|
|
|
|
33
|
|
|
$this->documentCollection = $documentCollection; |
|
34
|
|
|
|
|
35
|
|
|
$this->documentManager = $doctrine->getManager(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Save data to DB |
|
40
|
|
|
* onKernelResponse |
|
41
|
|
|
*/ |
|
42
|
|
|
public function persistEvents() |
|
43
|
|
|
{ |
|
44
|
|
|
$events = $this->activityManager->getEvents(); |
|
45
|
|
|
$thread = $this->generateUUID(); |
|
46
|
|
|
$username = $this->activityManager->getSecurityUsername(); |
|
47
|
|
|
|
|
48
|
|
|
foreach ($events as $event) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->trackEvent($event, $thread, $username); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Save the event to DB |
|
56
|
|
|
* |
|
57
|
|
|
* @param array $event Performed by user |
|
58
|
|
|
* @return void |
|
59
|
|
|
*/ |
|
60
|
|
|
private function trackEvent($event, $thread, $username) |
|
61
|
|
|
{ |
|
62
|
|
|
$action = $event['action']; |
|
63
|
|
|
$type = $event['type']; |
|
64
|
|
|
$location = $event['location']; |
|
65
|
|
|
$data = $event['data']; |
|
66
|
|
|
|
|
67
|
|
|
if (!is_object($data)) { |
|
68
|
|
|
$data = (object) $data; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$check = $action.$location.json_encode($data); |
|
72
|
|
|
if ($check == $this->saved) { |
|
73
|
|
|
return; |
|
74
|
|
|
} |
|
75
|
|
|
$this->saved = $check; |
|
76
|
|
|
|
|
77
|
|
|
/** @var AuditTracking $event */ |
|
78
|
|
|
$event = new $this->documentCollection(); |
|
79
|
|
|
|
|
80
|
|
|
// Request information |
|
81
|
|
|
$event->setThread($thread); |
|
82
|
|
|
$event->setAction($action); |
|
83
|
|
|
$event->setType($type); |
|
84
|
|
|
$event->setUsername($username); |
|
85
|
|
|
// Document information, collection and data |
|
86
|
|
|
$event->setLocation($location); |
|
87
|
|
|
$event->setData($data); |
|
88
|
|
|
$event->setCreatedAt(new \DateTime()); |
|
89
|
|
|
|
|
90
|
|
|
try { |
|
91
|
|
|
$this->documentManager->persist($event); |
|
92
|
|
|
$this->documentManager->flush($event); |
|
93
|
|
|
} catch (\Exception $e) {} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Generate a unique identifer |
|
100
|
|
|
* |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
|
|
private function generateUUID() |
|
104
|
|
|
{ |
|
105
|
|
|
if (!function_exists('openssl_random_pseudo_bytes')) { |
|
106
|
|
|
return uniqid('unq', true); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$data = openssl_random_pseudo_bytes(16); |
|
110
|
|
|
$data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100 |
|
111
|
|
|
$data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10 |
|
112
|
|
|
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)); |
|
113
|
|
|
} |
|
114
|
|
|
} |