2023-11-22 12:22:05 -08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Observers;
|
|
|
|
|
|
|
|
use App\Models\Actionlog;
|
|
|
|
use App\Models\User;
|
|
|
|
use Auth;
|
|
|
|
|
|
|
|
class UserObserver
|
|
|
|
{
|
|
|
|
/**
|
2023-11-22 13:35:12 -08:00
|
|
|
* Listen to the User updating event. This fires automatically every time an existing asset is saved.
|
2023-11-22 12:22:05 -08:00
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function updating(User $user)
|
|
|
|
{
|
|
|
|
|
|
|
|
$changed = [];
|
|
|
|
foreach ($user->getRawOriginal() as $key => $value) {
|
2023-11-22 12:28:58 -08:00
|
|
|
|
2023-11-22 12:22:05 -08:00
|
|
|
if ($user->getRawOriginal()[$key] != $user->getAttributes()[$key]) {
|
|
|
|
|
2023-11-22 12:50:46 -08:00
|
|
|
$changed[$key]['old'] = $user->getRawOriginal()[$key];
|
|
|
|
$changed[$key]['new'] = $user->getAttributes()[$key];
|
|
|
|
|
2023-11-22 12:22:05 -08:00
|
|
|
// Do not store the hashed password in changes
|
2023-11-22 12:50:46 -08:00
|
|
|
if ($key == 'password') {
|
|
|
|
$changed['password']['old'] = '*************';
|
|
|
|
$changed['password']['new'] = '*************';
|
2023-11-22 12:22:05 -08:00
|
|
|
}
|
2023-11-22 12:28:58 -08:00
|
|
|
|
2023-11-22 12:50:46 -08:00
|
|
|
// Do not store last login in changes
|
|
|
|
if ($key == 'last_login') {
|
|
|
|
unset($changed['last_login']);
|
|
|
|
unset($changed['last_login']);
|
2023-11-22 12:28:58 -08:00
|
|
|
}
|
|
|
|
|
2023-11-22 12:50:46 -08:00
|
|
|
if ($key == 'permissions') {
|
|
|
|
unset($changed['permissions']);
|
|
|
|
unset($changed['permissions']);
|
2023-11-22 12:28:58 -08:00
|
|
|
}
|
2023-11-22 12:22:05 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$logAction = new Actionlog();
|
|
|
|
$logAction->item_type = User::class;
|
|
|
|
$logAction->item_id = $user->id;
|
2023-11-22 12:50:46 -08:00
|
|
|
$logAction->target_type = User::class; // can we instead say $logAction->item = $asset ?
|
|
|
|
$logAction->target_id = $user->id;
|
2023-11-22 12:22:05 -08:00
|
|
|
$logAction->created_at = date('Y-m-d H:i:s');
|
|
|
|
$logAction->user_id = Auth::id();
|
|
|
|
$logAction->log_meta = json_encode($changed);
|
|
|
|
$logAction->logaction('update');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-11-22 13:35:12 -08:00
|
|
|
* Listen to the User created event, and increment
|
2023-11-22 12:22:05 -08:00
|
|
|
* the next_auto_tag_base value in the settings table when i
|
|
|
|
* a new asset is created.
|
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function created(User $user)
|
|
|
|
{
|
|
|
|
$logAction = new Actionlog();
|
|
|
|
$logAction->item_type = User::class; // can we instead say $logAction->item = $asset ?
|
|
|
|
$logAction->item_id = $user->id;
|
|
|
|
$logAction->created_at = date('Y-m-d H:i:s');
|
|
|
|
$logAction->user_id = Auth::id();
|
|
|
|
$logAction->logaction('create');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Listen to the User deleting event.
|
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function deleting(User $user)
|
|
|
|
{
|
|
|
|
$logAction = new Actionlog();
|
|
|
|
$logAction->item_type = User::class;
|
|
|
|
$logAction->item_id = $user->id;
|
2023-11-22 12:50:46 -08:00
|
|
|
$logAction->target_type = User::class; // can we instead say $logAction->item = $asset ?
|
|
|
|
$logAction->target_id = $user->id;
|
2023-11-22 12:22:05 -08:00
|
|
|
$logAction->created_at = date('Y-m-d H:i:s');
|
|
|
|
$logAction->user_id = Auth::id();
|
|
|
|
$logAction->logaction('delete');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Listen to the User deleting event.
|
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function restoring(User $user)
|
|
|
|
{
|
|
|
|
$logAction = new Actionlog();
|
|
|
|
$logAction->item_type = User::class;
|
|
|
|
$logAction->item_id = $user->id;
|
2023-11-22 12:50:46 -08:00
|
|
|
$logAction->target_type = User::class; // can we instead say $logAction->item = $asset ?
|
|
|
|
$logAction->target_id = $user->id;
|
2023-11-22 12:22:05 -08:00
|
|
|
$logAction->created_at = date('Y-m-d H:i:s');
|
|
|
|
$logAction->user_id = Auth::id();
|
|
|
|
$logAction->logaction('restore');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|