Skip to content

Commit 1dc15f7

Browse files
Adicionado tipo e verificacao do tipo no log
1 parent 1921854 commit 1dc15f7

File tree

3 files changed

+111
-51
lines changed

3 files changed

+111
-51
lines changed

src/OwenIt/Auditing/Auditing.php

+73-43
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,45 @@
77

88
class Auditing extends Model
99
{
10-
/**
11-
* @var array
12-
*/
13-
private $originalData = [];
14-
15-
/**
16-
* @var array
17-
*/
18-
private $updatedData = [];
19-
20-
/**
21-
* @var boolean
22-
*/
23-
private $updating = false;
24-
25-
/**
26-
* @var array
27-
*/
28-
protected $dontKeep = [];
29-
30-
/**
31-
* @var array
32-
*/
33-
protected $doKeep = [];
34-
35-
/**
36-
* @var array
37-
*/
38-
protected $dirtyData = [];
39-
40-
/**
41-
* @var bool
42-
*/
43-
public $auditEnabled = true;
10+
/**
11+
* @var array
12+
*/
13+
private $originalData = [];
14+
15+
/**
16+
* @var array
17+
*/
18+
private $updatedData = [];
19+
20+
/**
21+
* @var boolean
22+
*/
23+
private $updating = false;
24+
25+
/**
26+
* @var array
27+
*/
28+
protected $dontKeep = [];
29+
30+
/**
31+
* @var array
32+
*/
33+
protected $doKeep = [];
34+
35+
/**
36+
* @var array
37+
*/
38+
protected $dirtyData = [];
39+
40+
/**
41+
* @var bool
42+
*/
43+
protected $auditEnabled = true;
44+
45+
/**
46+
* @var array
47+
*/
48+
protected $auditableTypes = ['created', 'saved', 'deleted'];
4449

4550
/**
4651
* Init auditing
@@ -54,22 +59,25 @@ public static function boot()
5459
$model->prepareAudit();
5560
});
5661

57-
static::saved(function ($model)
62+
static::created(function($model)
5863
{
59-
$model->auditUpdate();
64+
if($model->isTypeAuditable('created'))
65+
$model->auditCreation();
6066
});
6167

62-
static::created(function($model)
68+
static::saved(function ($model)
6369
{
64-
$model->auditCreation();
70+
if($model->isTypeAuditable('saved'))
71+
$model->auditUpdate();
6572
});
6673

6774
static::deleted(function($model)
6875
{
69-
$model->prepareAudit();
70-
$model->auditDeletion();
76+
if($model->isTypeAuditable('deleted')){
77+
$model->prepareAudit();
78+
$model->auditDeletion();
79+
}
7180
});
72-
7381
}
7482

7583
/**
@@ -144,6 +152,7 @@ public function auditCreation()
144152
'owner_type' => get_class($this),
145153
'owner_id' => $this->getKey(),
146154
'user_id' => $this->getUserId(),
155+
'type' => 'created',
147156
'created_at' => new \DateTime(),
148157
'updated_at' => new \DateTime(),
149158
]);
@@ -171,7 +180,7 @@ public function auditUpdate()
171180
$changes_to_record = $this->changedAuditingFields();
172181
if(count($changes_to_record))
173182
{
174-
$log = [];
183+
$log = ['type' => 'updated'];
175184
foreach ($changes_to_record as $key => $change)
176185
{
177186
$log['old_value'][$key] = array_get($this->originalData, $key);
@@ -185,7 +194,6 @@ public function auditUpdate()
185194

186195
/**
187196
* Auditing deletion
188-
*
189197
*/
190198
public function auditDeletion()
191199
{
@@ -198,6 +206,7 @@ public function auditDeletion()
198206
'owner_type' => get_class($this),
199207
'owner_id' => $this->getKey(),
200208
'user_id' => $this->getUserId(),
209+
'type' => 'deleted',
201210
'created_at' => new \DateTime(),
202211
'updated_at' => new \DateTime(),
203212
]);
@@ -215,6 +224,7 @@ public function audit(array $log)
215224
'owner_type' => get_class($this),
216225
'owner_id' => $this->getKey(),
217226
'user_id' => $this->getUserId(),
227+
'type' => $log['type'],
218228
'created_at' => new \DateTime(),
219229
'updated_at' => new \DateTime(),
220230
];
@@ -300,4 +310,24 @@ public function identifiableName()
300310
return $this->getKey();
301311
}
302312

313+
314+
/**
315+
* Verify is type auditable
316+
*
317+
* @param $key
318+
* @return bool
319+
*/
320+
public function isTypeAuditable($key)
321+
{
322+
if (static::$unguarded) {
323+
return true;
324+
}
325+
326+
if (in_array($key, $this->auditableTypes)) {
327+
return true;
328+
}
329+
330+
return;
331+
}
332+
303333
}

src/OwenIt/Auditing/AuditingTrait.php

+37-8
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,28 @@ public static function bootAuditingTrait()
4747
$model->prepareAudit();
4848
});
4949

50-
static::saved(function ($model)
50+
static::created(function($model)
5151
{
52-
$model->auditUpdate();
52+
if($model->isTypeAuditable('created'))
53+
$model->auditCreation();
5354
});
5455

55-
static::created(function($model)
56+
static::saved(function ($model)
5657
{
57-
$model->auditCreation();
58+
if($model->isTypeAuditable('saved'))
59+
$model->auditUpdate();
5860
});
5961

6062
static::deleted(function($model)
6163
{
62-
$model->prepareAudit();
63-
$model->auditDeletion();
64+
if($model->isTypeAuditable('deleted')){
65+
$model->prepareAudit();
66+
$model->auditDeletion();
67+
}
6468
});
6569
}
6670

71+
6772
/**
6873
* Get list of logs
6974
* @return mixed
@@ -136,6 +141,7 @@ public function auditCreation()
136141
'owner_type' => get_class($this),
137142
'owner_id' => $this->getKey(),
138143
'user_id' => $this->getUserId(),
144+
'type' => 'created',
139145
'created_at' => new \DateTime(),
140146
'updated_at' => new \DateTime(),
141147
]);
@@ -163,7 +169,7 @@ public function auditUpdate()
163169
$changes_to_record = $this->changedAuditingFields();
164170
if(count($changes_to_record))
165171
{
166-
$log = [];
172+
$log = ['type' => 'updated'];
167173
foreach ($changes_to_record as $key => $change)
168174
{
169175
$log['old_value'][$key] = array_get($this->originalData, $key);
@@ -177,7 +183,6 @@ public function auditUpdate()
177183

178184
/**
179185
* Auditing deletion
180-
*
181186
*/
182187
public function auditDeletion()
183188
{
@@ -190,6 +195,7 @@ public function auditDeletion()
190195
'owner_type' => get_class($this),
191196
'owner_id' => $this->getKey(),
192197
'user_id' => $this->getUserId(),
198+
'type' => 'deleted',
193199
'created_at' => new \DateTime(),
194200
'updated_at' => new \DateTime(),
195201
]);
@@ -207,6 +213,7 @@ public function audit(array $log)
207213
'owner_type' => get_class($this),
208214
'owner_id' => $this->getKey(),
209215
'user_id' => $this->getUserId(),
216+
'type' => $log['type'],
210217
'created_at' => new \DateTime(),
211218
'updated_at' => new \DateTime(),
212219
];
@@ -293,4 +300,26 @@ public function identifiableName()
293300
}
294301

295302

303+
/**
304+
* Verify is type auditable
305+
*
306+
* @param $key
307+
* @return bool
308+
*/
309+
public function isTypeAuditable($key)
310+
{
311+
if (static::$unguarded) {
312+
return true;
313+
}
314+
315+
if(!isset($this->auditableTypes))
316+
return true;
317+
318+
if (in_array($key, $this->auditableTypes)) {
319+
return true;
320+
}
321+
322+
return;
323+
}
324+
296325
}

src/migrations/2015_08_01_104512_create_log_table.php

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public function up()
1818
$table->integer('owner_id');
1919
$table->text('old_value')->nullable();
2020
$table->text('new_value')->nullable();
21+
$table->string('type');
2122
$table->timestamps();
2223
});
2324
}

0 commit comments

Comments
 (0)