1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Laravel Auditing package. |
4
|
|
|
* |
5
|
|
|
* @author Antério Vieira <[email protected]> |
6
|
|
|
* @author Quetzy Garcia <[email protected]> |
7
|
|
|
* @author Raphael França <[email protected]> |
8
|
|
|
* @copyright 2015-2018 |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, |
11
|
|
|
* please view the LICENSE.md file that was distributed |
12
|
|
|
* with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace OwenIt\Auditing; |
16
|
|
|
|
17
|
|
|
use OwenIt\Auditing\Contracts\Auditable; |
18
|
|
|
use OwenIt\Auditing\Facades\Auditor; |
19
|
|
|
|
20
|
|
|
class AuditableObserver |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Is the model being restored? |
24
|
|
|
* |
25
|
|
|
* @var bool |
26
|
|
|
*/ |
27
|
|
|
public static $restoring = false; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Handle the retrieved event. |
31
|
|
|
* |
32
|
|
|
* @param \OwenIt\Auditing\Contracts\Auditable $model |
33
|
|
|
* |
34
|
|
|
* @return void |
35
|
|
|
*/ |
36
|
|
|
public function retrieved(Auditable $model) |
37
|
|
|
{ |
38
|
|
|
Auditor::execute($model->setAuditEvent('retrieved')); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Handle the created event. |
43
|
|
|
* |
44
|
|
|
* @param \OwenIt\Auditing\Contracts\Auditable $model |
45
|
|
|
* |
46
|
|
|
* @return void |
47
|
|
|
*/ |
48
|
|
|
public function created(Auditable $model) |
49
|
|
|
{ |
50
|
|
|
Auditor::execute($model->setAuditEvent('created')); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Handle the updated event. |
55
|
|
|
* |
56
|
|
|
* @param \OwenIt\Auditing\Contracts\Auditable $model |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
public function updated(Auditable $model) |
61
|
|
|
{ |
62
|
|
|
// Ignore the updated event when restoring |
63
|
|
|
if (!static::$restoring) { |
64
|
|
|
Auditor::execute($model->setAuditEvent('updated')); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Handle the deleted event. |
70
|
|
|
* |
71
|
|
|
* @param \OwenIt\Auditing\Contracts\Auditable $model |
72
|
|
|
* |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
|
|
public function deleted(Auditable $model) |
76
|
|
|
{ |
77
|
|
|
Auditor::execute($model->setAuditEvent('deleted')); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Handle the restoring event. |
82
|
|
|
* |
83
|
|
|
* @param \OwenIt\Auditing\Contracts\Auditable $model |
84
|
|
|
* |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
|
|
public function restoring(Auditable $model) |
88
|
|
|
{ |
89
|
|
|
// When restoring a model, an updated event is also fired. |
90
|
|
|
// By keeping track of the main event that took place, |
91
|
|
|
// we avoid creating a second audit with wrong values |
92
|
|
|
static::$restoring = true; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Handle the restored event. |
97
|
|
|
* |
98
|
|
|
* @param \OwenIt\Auditing\Contracts\Auditable $model |
99
|
|
|
* |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
|
|
public function restored(Auditable $model) |
103
|
|
|
{ |
104
|
|
|
Auditor::execute($model->setAuditEvent('restored')); |
105
|
|
|
|
106
|
|
|
// Once the model is restored, we need to put everything back |
107
|
|
|
// as before, in case a legitimate update event is fired |
108
|
|
|
static::$restoring = false; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|