|
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 Doctrine\Bundle\MongoDBBundle\ManagerRegistry; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class StoreManager |
|
13
|
|
|
* @package Graviton\AuditTrackingBundle\Manager |
|
14
|
|
|
* |
|
15
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
|
16
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
17
|
|
|
* @link http://swisscom.ch |
|
18
|
|
|
*/ |
|
19
|
|
|
class StoreManager |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var ActivityManager */ |
|
22
|
|
|
private $activityManager; |
|
23
|
|
|
|
|
24
|
|
|
/** @var DocumentManager */ |
|
25
|
|
|
private $documentManager; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Avoid saving same action again |
|
29
|
|
|
* @var |
|
30
|
|
|
*/ |
|
31
|
|
|
private $saved; |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* StoreManager constructor. |
|
35
|
|
|
* @param ActivityManager $activityManager Main activity manager |
|
36
|
|
|
* @param ManagerRegistry $doctrine Doctrine document mapper |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct( |
|
39
|
|
|
ActivityManager $activityManager, |
|
40
|
|
|
ManagerRegistry $doctrine |
|
41
|
|
|
) { |
|
42
|
|
|
$this->activityManager = $activityManager; |
|
43
|
|
|
|
|
44
|
|
|
$this->documentManager = $doctrine->getManager(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Save data to DB |
|
49
|
|
|
* onKernelResponse |
|
50
|
|
|
* |
|
51
|
|
|
* @return void |
|
52
|
|
|
*/ |
|
53
|
|
|
public function persistEvents() |
|
54
|
|
|
{ |
|
55
|
|
|
$events = $this->activityManager->getEvents(); |
|
56
|
|
|
$thread = $this->generateUUID(); |
|
57
|
|
|
$username = $this->activityManager->getSecurityUsername(); |
|
58
|
|
|
|
|
59
|
|
|
foreach ($events as $event) { |
|
60
|
|
|
$this->trackEvent($event, $thread, $username); |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Save the event to DB |
|
66
|
|
|
* |
|
67
|
|
|
* @param AuditTracking $event Performed by user |
|
68
|
|
|
* @param string $thread The thread ID |
|
69
|
|
|
* @param string $username User connected name |
|
70
|
|
|
* @return void |
|
71
|
|
|
*/ |
|
72
|
|
|
private function trackEvent($event, $thread, $username) |
|
73
|
|
|
{ |
|
74
|
|
|
// Request information |
|
75
|
|
|
$event->setThread($thread); |
|
76
|
|
|
$event->setUsername($username); |
|
77
|
|
|
|
|
78
|
|
|
try { |
|
79
|
|
|
$this->documentManager->persist($event); |
|
80
|
|
|
$this->documentManager->flush($event); |
|
81
|
|
|
} catch (\Exception $e) { |
|
82
|
|
|
die(var_dump($e->getMessage())); |
|
|
|
|
|
|
83
|
|
|
// TODO LOG the error and event |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Generate a unique identifer |
|
91
|
|
|
* |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
|
|
private function generateUUID() |
|
95
|
|
|
{ |
|
96
|
|
|
if (!function_exists('openssl_random_pseudo_bytes')) { |
|
97
|
|
|
return uniqid('unq', true); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$data = openssl_random_pseudo_bytes(16); |
|
101
|
|
|
$data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100 |
|
102
|
|
|
$data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10 |
|
103
|
|
|
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.