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