|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Observers; |
|
4
|
|
|
|
|
5
|
|
|
use App\Models\User; |
|
6
|
|
|
use App\Models\UserActivity; |
|
7
|
|
|
|
|
8
|
|
|
class UserActivityObserver |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Handle the User "created" event. |
|
12
|
|
|
*/ |
|
13
|
|
|
public function created(User $user): void |
|
14
|
|
|
{ |
|
15
|
|
|
UserActivity::create([ |
|
16
|
|
|
'user_id' => $user->id, |
|
17
|
|
|
'username' => $user->username, |
|
18
|
|
|
'activity_type' => 'registered', |
|
19
|
|
|
'description' => "New user registered: {$user->username}", |
|
20
|
|
|
'metadata' => [ |
|
21
|
|
|
'email' => $user->email, |
|
22
|
|
|
'ip_address' => request()->ip(), |
|
23
|
|
|
], |
|
24
|
|
|
]); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Handle the User "updated" event. |
|
29
|
|
|
*/ |
|
30
|
|
|
public function updated(User $user): void |
|
31
|
|
|
{ |
|
32
|
|
|
// Check if the role was changed |
|
33
|
|
|
if ($user->isDirty('roles_id')) { |
|
34
|
|
|
$oldRoleId = $user->getOriginal('roles_id'); |
|
35
|
|
|
$newRoleId = $user->roles_id; |
|
36
|
|
|
|
|
37
|
|
|
// Get role names |
|
38
|
|
|
$oldRoleName = $this->getRoleName($oldRoleId); |
|
39
|
|
|
$newRoleName = $this->getRoleName($newRoleId); |
|
40
|
|
|
|
|
41
|
|
|
UserActivity::create([ |
|
42
|
|
|
'user_id' => $user->id, |
|
43
|
|
|
'username' => $user->username, |
|
44
|
|
|
'activity_type' => 'role_updated', |
|
45
|
|
|
'description' => "User role updated: {$user->username} → {$newRoleName}", |
|
46
|
|
|
'metadata' => [ |
|
47
|
|
|
'old_role_id' => $oldRoleId, |
|
48
|
|
|
'new_role_id' => $newRoleId, |
|
49
|
|
|
'old_role_name' => $oldRoleName, |
|
50
|
|
|
'new_role_name' => $newRoleName, |
|
51
|
|
|
'updated_by' => auth()->user()?->username ?? 'System', |
|
52
|
|
|
], |
|
53
|
|
|
]); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Handle the User "deleted" event (soft delete). |
|
59
|
|
|
*/ |
|
60
|
|
|
public function deleted(User $user): void |
|
61
|
|
|
{ |
|
62
|
|
|
// Only log if it's a soft delete (not force delete) |
|
63
|
|
|
if ($user->trashed()) { |
|
64
|
|
|
UserActivity::create([ |
|
65
|
|
|
'user_id' => $user->id, |
|
66
|
|
|
'username' => $user->username, |
|
67
|
|
|
'activity_type' => 'deleted', |
|
68
|
|
|
'description' => "User deleted: {$user->username}", |
|
69
|
|
|
'metadata' => [ |
|
70
|
|
|
'email' => $user->email, |
|
71
|
|
|
'deleted_by' => auth()->user()?->username ?? 'System', |
|
72
|
|
|
], |
|
73
|
|
|
]); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Handle the User "restored" event. |
|
79
|
|
|
*/ |
|
80
|
|
|
public function restored(User $user): void |
|
81
|
|
|
{ |
|
82
|
|
|
UserActivity::create([ |
|
83
|
|
|
'user_id' => $user->id, |
|
84
|
|
|
'username' => $user->username, |
|
85
|
|
|
'activity_type' => 'registered', |
|
86
|
|
|
'description' => "User restored: {$user->username}", |
|
87
|
|
|
'metadata' => [ |
|
88
|
|
|
'restored_by' => auth()->user()?->username ?? 'System', |
|
89
|
|
|
], |
|
90
|
|
|
]); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Handle the User "force deleted" event. |
|
95
|
|
|
*/ |
|
96
|
|
|
public function forceDeleted(User $user): void |
|
97
|
|
|
{ |
|
98
|
|
|
// Log permanent deletion |
|
99
|
|
|
UserActivity::create([ |
|
100
|
|
|
'user_id' => null, // User no longer exists |
|
101
|
|
|
'username' => $user->username, |
|
102
|
|
|
'activity_type' => 'deleted', |
|
103
|
|
|
'description' => "User permanently deleted: {$user->username}", |
|
104
|
|
|
'metadata' => [ |
|
105
|
|
|
'email' => $user->email, |
|
106
|
|
|
'deleted_by' => auth()->user()?->username ?? 'System', |
|
107
|
|
|
'permanent' => true, |
|
108
|
|
|
], |
|
109
|
|
|
]); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Get role name by ID |
|
114
|
|
|
*/ |
|
115
|
|
|
private function getRoleName(?int $roleId): string |
|
116
|
|
|
{ |
|
117
|
|
|
if (! $roleId) { |
|
|
|
|
|
|
118
|
|
|
return 'None'; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
try { |
|
122
|
|
|
$role = \Spatie\Permission\Models\Role::find($roleId); |
|
123
|
|
|
|
|
124
|
|
|
return $role ? $role->name : "Role #{$roleId}"; |
|
125
|
|
|
} catch (\Exception $e) { |
|
126
|
|
|
return "Role #{$roleId}"; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: