|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SetBased\Audit\Audit; |
|
4
|
|
|
|
|
5
|
|
|
use SetBased\Audit\AuditTable; |
|
6
|
|
|
use SetBased\Audit\Metadata\TableColumnsMetadata; |
|
7
|
|
|
use SetBased\Audit\MySql\AuditDataLayer; |
|
8
|
|
|
use SetBased\Stratum\Style\StratumStyle; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class for executing auditing actions for tables. |
|
12
|
|
|
*/ |
|
13
|
|
|
class Audit |
|
14
|
|
|
{ |
|
15
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
16
|
|
|
/** |
|
17
|
|
|
* The metadata of the additional audit columns. |
|
18
|
|
|
* |
|
19
|
|
|
* @var TableColumnsMetadata |
|
20
|
|
|
*/ |
|
21
|
|
|
private $additionalAuditColumns; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The names of all tables in audit schema. |
|
25
|
|
|
* |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
private $auditSchemaTables; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The content of the configuration file. |
|
32
|
|
|
* |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
private $config; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The names of all tables in data schema. |
|
39
|
|
|
* |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
private $dataSchemaTables; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* The Output decorator. |
|
46
|
|
|
* |
|
47
|
|
|
* @var StratumStyle |
|
48
|
|
|
*/ |
|
49
|
|
|
private $io; |
|
50
|
|
|
|
|
51
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
52
|
|
|
/** |
|
53
|
|
|
* Object constructor. |
|
54
|
|
|
* |
|
55
|
|
|
* @param array[] $config The content of the configuration file. |
|
56
|
|
|
* @param StratumStyle $io The Output decorator. |
|
57
|
|
|
*/ |
|
58
|
|
|
public function __construct(&$config, $io) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->config = &$config; |
|
61
|
|
|
$this->io = $io; |
|
62
|
|
|
|
|
63
|
|
|
$this->additionalAuditColumns = |
|
64
|
|
|
AuditDataLayer::resolveCanonicalAdditionalAuditColumns($this->config['database']['audit_schema'], |
|
65
|
|
|
$this->config['audit_columns']); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
69
|
|
|
/** |
|
70
|
|
|
* Getting list of all tables from information_schema of database from config file. |
|
71
|
|
|
*/ |
|
72
|
|
|
public function listOfTables() |
|
73
|
|
|
{ |
|
74
|
|
|
$this->dataSchemaTables = AuditDataLayer::getTablesNames($this->config['database']['data_schema']); |
|
75
|
|
|
$this->auditSchemaTables = AuditDataLayer::getTablesNames($this->config['database']['audit_schema']); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
79
|
|
|
/** |
|
80
|
|
|
* The main method: executes the auditing actions for tables. |
|
81
|
|
|
*/ |
|
82
|
|
|
public function main() |
|
83
|
|
|
{ |
|
84
|
|
|
$this->listOfTables(); |
|
85
|
|
|
|
|
86
|
|
|
$this->unknownTables(); |
|
87
|
|
|
|
|
88
|
|
|
$this->obsoleteTables(); |
|
89
|
|
|
|
|
90
|
|
|
$this->knownTables(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
94
|
|
|
/** |
|
95
|
|
|
* Removes tables listed in the config file that are not longer in the data schema from the config file. |
|
96
|
|
|
*/ |
|
97
|
|
|
public function obsoleteTables() |
|
98
|
|
|
{ |
|
99
|
|
|
foreach ($this->config['tables'] as $tableName => $dummy) |
|
100
|
|
|
{ |
|
101
|
|
|
if (AuditDataLayer::searchInRowSet('table_name', $tableName, $this->dataSchemaTables)===null) |
|
102
|
|
|
{ |
|
103
|
|
|
$this->io->writeln(sprintf('<info>Removing obsolete table %s from config file</info>', $tableName)); |
|
104
|
|
|
unset($this->config['tables'][$tableName]); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
110
|
|
|
/** |
|
111
|
|
|
* Compares the tables listed in the config file and the tables found in the data schema. |
|
112
|
|
|
*/ |
|
113
|
|
|
public function unknownTables() |
|
114
|
|
|
{ |
|
115
|
|
|
foreach ($this->dataSchemaTables as $table) |
|
116
|
|
|
{ |
|
117
|
|
|
if (isset($this->config['tables'][$table['table_name']])) |
|
118
|
|
|
{ |
|
119
|
|
|
if (!isset($this->config['tables'][$table['table_name']]['audit'])) |
|
120
|
|
|
{ |
|
121
|
|
|
$this->io->writeln(sprintf('<info>Audit not set for table %s</info>', $table['table_name'])); |
|
122
|
|
|
} |
|
123
|
|
|
else |
|
124
|
|
|
{ |
|
125
|
|
|
if ($this->config['tables'][$table['table_name']]['audit']) |
|
126
|
|
|
{ |
|
127
|
|
|
if (!isset($this->config['tables'][$table['table_name']]['alias'])) |
|
128
|
|
|
{ |
|
129
|
|
|
$this->config['tables'][$table['table_name']]['alias'] = AuditTable::getRandomAlias(); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
else |
|
135
|
|
|
{ |
|
136
|
|
|
$this->io->writeln(sprintf('<info>Found new table %s</info>', $table['table_name'])); |
|
137
|
|
|
$this->config['tables'][$table['table_name']] = ['audit' => null, |
|
138
|
|
|
'alias' => null, |
|
139
|
|
|
'skip' => null]; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
145
|
|
|
/** |
|
146
|
|
|
* Processed known tables. |
|
147
|
|
|
*/ |
|
148
|
|
|
private function knownTables() |
|
149
|
|
|
{ |
|
150
|
|
|
foreach ($this->dataSchemaTables as $table) |
|
151
|
|
|
{ |
|
152
|
|
|
if ($this->config['tables'][$table['table_name']]['audit']) |
|
153
|
|
|
{ |
|
154
|
|
|
$currentTable = new AuditTable($this->io, |
|
155
|
|
|
$this->config['database']['data_schema'], |
|
156
|
|
|
$this->config['database']['audit_schema'], |
|
157
|
|
|
$table['table_name'], |
|
158
|
|
|
$this->additionalAuditColumns, |
|
159
|
|
|
$this->config['tables'][$table['table_name']]['alias'], |
|
160
|
|
|
$this->config['tables'][$table['table_name']]['skip']); |
|
161
|
|
|
|
|
162
|
|
|
// Ensure the audit table exists. |
|
163
|
|
|
if (AuditDataLayer::searchInRowSet('table_name', $table['table_name'], $this->auditSchemaTables)===null) |
|
164
|
|
|
{ |
|
165
|
|
|
$currentTable->createAuditTable(); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
// Drop and create audit triggers and add new columns to the audit table. |
|
169
|
|
|
$currentTable->main($this->config['additional_sql']); |
|
170
|
|
|
} |
|
171
|
|
|
else |
|
172
|
|
|
{ |
|
173
|
|
|
AuditTable::dropAuditTriggers($this->io, $this->config['database']['data_schema'], $table['table_name']); |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
//---------------------------------------------------------------------------------------------------------------------- |
|
182
|
|
|
|