fix(activity): 修正使用者更新時產生雙重紀錄的問題
- 使用 saveQuietly 避免原生 update 事件觸發紀錄 - 手動合併屬性變更 (Attributes) 與角色變更 (Roles) 為單一操作紀錄
This commit is contained in:
@@ -117,7 +117,6 @@ class UserController extends Controller
|
||||
public function update(Request $request, string $id)
|
||||
{
|
||||
$user = User::findOrFail($id);
|
||||
$oldRoles = $user->roles()->pluck('display_name')->join(', ');
|
||||
|
||||
$validated = $request->validate([
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
@@ -130,6 +129,7 @@ class UserController extends Controller
|
||||
'password.confirmed' => '密碼確認不符',
|
||||
]);
|
||||
|
||||
// 1. Prepare data and detect changes
|
||||
$userData = [
|
||||
'name' => $validated['name'],
|
||||
'email' => $validated['email'],
|
||||
@@ -140,25 +140,55 @@ class UserController extends Controller
|
||||
$userData['password'] = Hash::make($validated['password']);
|
||||
}
|
||||
|
||||
$user->update($userData);
|
||||
$user->fill($userData);
|
||||
|
||||
// Capture dirty attributes for manual logging
|
||||
$dirty = $user->getDirty();
|
||||
$oldAttributes = [];
|
||||
$newAttributes = [];
|
||||
|
||||
foreach ($dirty as $key => $value) {
|
||||
$oldAttributes[$key] = $user->getOriginal($key);
|
||||
$newAttributes[$key] = $value;
|
||||
}
|
||||
|
||||
// Save without triggering events (prevents duplicate log)
|
||||
$user->saveQuietly();
|
||||
|
||||
// 2. Handle Roles
|
||||
$roleChanges = null;
|
||||
if (isset($validated['roles'])) {
|
||||
$oldRoles = $user->roles()->pluck('display_name')->join(', ');
|
||||
$user->syncRoles($validated['roles']);
|
||||
|
||||
$newRoles = $user->roles()->pluck('display_name')->join(', ');
|
||||
|
||||
if ($oldRoles !== $newRoles) {
|
||||
$roleChanges = [
|
||||
'old' => $oldRoles,
|
||||
'new' => $newRoles
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Manually Log activity (Single Consolidated Log)
|
||||
if (!empty($newAttributes) || $roleChanges) {
|
||||
$properties = [
|
||||
'attributes' => $newAttributes,
|
||||
'old' => $oldAttributes,
|
||||
];
|
||||
|
||||
if ($roleChanges) {
|
||||
$properties['attributes']['role_id'] = $roleChanges['new'];
|
||||
$properties['old']['role_id'] = $roleChanges['old'];
|
||||
}
|
||||
|
||||
activity()
|
||||
->performedOn($user)
|
||||
->causedBy(auth()->user())
|
||||
->event('updated')
|
||||
->withProperties([
|
||||
'attributes' => ['role_id' => $newRoles],
|
||||
'old' => ['role_id' => $oldRoles],
|
||||
])
|
||||
->withProperties($properties)
|
||||
->log('updated');
|
||||
}
|
||||
}
|
||||
|
||||
return redirect()->route('users.index')->with('success', '使用者更新成功');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user