Skip to content

A free open source IT asset/license management system

Bulk User Edit Logging: Fixed #19193 - log parity in bulk as for regular user edit

snipe1efd470a77fe
diff --git a/app/Http/Controllers/Users/BulkUsersController.php b/app/Http/Controllers/Users/BulkUsersController.php
index 138288a4d5..cb837028c9 100644
--- a/app/Http/Controllers/Users/BulkUsersController.php
+++ b/app/Http/Controllers/Users/BulkUsersController.php
@@ -271,63 +271,62 @@ class BulkUsersController extends Controller
if (! $manager_conflict) {
$this->conditionallyAddItem('manager_id');
}
- // Save the updated info
- User::whereIn('id', $user_raw_array)
- ->where('id', '!=', auth()->id())->update($this->update_array);
-
- if (array_key_exists('location_id', $this->update_array)) {
- Asset::where('assigned_type', User::class)
- ->whereIn('assigned_to', $user_raw_array)
- ->update(['location_id' => $this->update_array['location_id']]);
- }
-
- // Handle company pivot sync separately from the mass update.
- // company_ids[] comes from the multi-select; null_company_ids clears all memberships.
+ // Prepare company pivot changes upfront so we can include company_id in the per-user save.
$bulkCompanyIds = array_filter(array_map('intval', (array) $request->input('company_ids', [])));
$clearCompanies = $request->input('null_company_ids') == '1';
+ $allowedIds = [];
if ($bulkCompanyIds || $clearCompanies) {
$allowedIds = Company::getIdsForCurrentUser($bulkCompanyIds);
- // Also update the scalar company_id column for display/backward compat.
- $scalarCompanyId = $allowedIds[0] ?? null;
- User::whereIn('id', $user_raw_array)->where('id', '!=', auth()->id())
- ->update(['company_id' => $scalarCompanyId]);
- foreach ($users as $user) {
- if ($clearCompanies && ! auth()->user()->isSuperUser() && Company::isFullMultipleCompanySupportEnabled()) {
- // Non-superusers can only detach companies they belong to; sync([]) would
- // also wipe memberships for companies outside their scope.
- $user->companies()->detach(Company::getIdsForCurrentUser(
- $user->companies()->pluck('companies.id')->toArray()
- ));
- } else {
- $user->companies()->sync($allowedIds);
- }
- }
+ // Include scalar company_id in the update array for display/backward compat.
+ $this->update_array['company_id'] = $allowedIds[0] ?? null;
}
- // Fields that require canEditAuthFields (non-admins cannot touch admins/superusers,
- // admins cannot touch superusers) must be applied per-user, not via mass update.
+ // Save per-user so UserObserver::updating() fires and all field changes are written
+ // to the Activity Report. A mass update() bypasses model observers entirely.
foreach ($users as $user) {
- if (auth()->user()->can('canEditAuthFields', $user) && auth()->user()->can('editableOnDemo')) {
- $authFieldUpdate = [];
+ $canEditAuth = auth()->user()->can('canEditAuthFields', $user) && auth()->user()->can('editableOnDemo');
+ $userUpdate = $this->update_array;
+
+ if ($canEditAuth) {
if ($request->filled('activated')) {
- $authFieldUpdate['activated'] = $request->input('activated');
+ $userUpdate['activated'] = $request->input('activated');
}
if ($request->filled('ldap_import')) {
- $authFieldUpdate['ldap_import'] = $request->input('ldap_import');
+ $userUpdate['ldap_import'] = $request->input('ldap_import');
}
if ($request->filled('email')) {
- $authFieldUpdate['email'] = $request->input('email');
+ $userUpdate['email'] = $request->input('email');
} elseif ($request->input('null_email') == '1') {
- $authFieldUpdate['email'] = null;
- }
- if (! empty($authFieldUpdate)) {
- $user->update($authFieldUpdate);
+ $userUpdate['email'] = null;
}
- if ($request->filled('groups') && auth()->user()->isSuperUser()) {
- $user->groups()->sync($request->input('groups'));
+ }
+
+ if (! empty($userUpdate)) {
+ $user->fill($userUpdate)->save();
+ }
+
+ if (array_key_exists('location_id', $this->update_array)) {
+ Asset::where('assigned_type', User::class)
+ ->where('assigned_to', $user->id)
+ ->update(['location_id' => $this->update_array['location_id']]);
+ }
+
+ if ($bulkCompanyIds || $clearCompanies) {
+ if ($clearCompanies && ! auth()->user()->isSuperUser() && Company::isFullMultipleCompanySupportEnabled()) {
+ // Non-superusers can only detach companies they belong to; sync([]) would
+ // also wipe memberships for companies outside their scope.
+ $user->companies()->detach(Company::getIdsForCurrentUser(
+ $user->companies()->pluck('companies.id')->toArray()
+ ));
+ } else {
+ $user->syncCompaniesWithLogging($allowedIds);
}
}
+
+ if ($canEditAuth && $request->filled('groups') && auth()->user()->isSuperUser()) {
+ $user->groups()->sync($request->input('groups'));
+ }
}
return redirect()->route('users.index')
diff --git a/app/Observers/UserObserver.php b/app/Observers/UserObserver.php
index 1967ff5eb1..d7ec6bb2ba 100644
--- a/app/Observers/UserObserver.php
+++ b/app/Observers/UserObserver.php
@@ -37,6 +37,7 @@ class UserObserver
'locale',
'two_factor_enrolled',
'two_factor_optin',
+ 'display_name',
'department_id',
'address',
'address2',
diff --git a/tests/Feature/Users/Ui/BulkActions/BulkEditUsersTest.php b/tests/Feature/Users/Ui/BulkActions/BulkEditUsersTest.php
index 5c9ab3cb98..3fa447d736 100644
--- a/tests/Feature/Users/Ui/BulkActions/BulkEditUsersTest.php
+++ b/tests/Feature/Users/Ui/BulkActions/BulkEditUsersTest.php
@@ -2,6 +2,7 @@
namespace Tests\Feature\Users\Ui\BulkActions;
+use App\Models\Actionlog;
use App\Models\Group;
use App\Models\User;
use Tests\TestCase;
@@ -151,4 +152,118 @@ class BulkEditUsersTest extends TestCase
$this->assertFalse($target->fresh()->groups->contains($group));
}
+
+ public function test_bulk_edit_logs_general_field_changes_to_activity_report()
+ {
+ $target = User::factory()->create(['city' => 'Springfield', 'jobtitle' => 'Engineer']);
+
+ $existingLogIds = Actionlog::where('item_type', User::class)
+ ->where('item_id', $target->id)
+ ->pluck('id');
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('users/bulkeditsave'), [
+ 'ids' => [$target->id],
+ 'city' => 'Shelbyville',
+ 'jobtitle' => 'Senior Engineer',
+ ])
+ ->assertRedirect(route('users.index'));
+
+ $log = Actionlog::where('item_type', User::class)
+ ->where('item_id', $target->id)
+ ->where('action_type', 'update')
+ ->whereNotIn('id', $existingLogIds)
+ ->first();
+
+ $this->assertNotNull($log, 'Bulk edit should produce an activity log entry');
+
+ $meta = json_decode($log->log_meta, true);
+ $this->assertArrayHasKey('city', $meta);
+ $this->assertEquals('Springfield', $meta['city']['old']);
+ $this->assertEquals('Shelbyville', $meta['city']['new']);
+ $this->assertArrayHasKey('jobtitle', $meta);
+ $this->assertEquals('Engineer', $meta['jobtitle']['old']);
+ $this->assertEquals('Senior Engineer', $meta['jobtitle']['new']);
+ }
+
+ public function test_bulk_edit_logs_null_clear_operations_to_activity_report()
+ {
+ $target = User::factory()->create(['notes' => 'Some notes', 'phone' => '555-1234']);
+
+ $existingLogIds = Actionlog::where('item_type', User::class)
+ ->where('item_id', $target->id)
+ ->pluck('id');
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('users/bulkeditsave'), [
+ 'ids' => [$target->id],
+ 'null_notes' => '1',
+ 'null_phone' => '1',
+ ])
+ ->assertRedirect(route('users.index'));
+
+ $log = Actionlog::where('item_type', User::class)
+ ->where('item_id', $target->id)
+ ->where('action_type', 'update')
+ ->whereNotIn('id', $existingLogIds)
+ ->first();
+
+ $this->assertNotNull($log, 'Bulk clear should produce an activity log entry');
+
+ $meta = json_decode($log->log_meta, true);
+ $this->assertArrayHasKey('notes', $meta);
+ $this->assertNull($meta['notes']['new']);
+ $this->assertArrayHasKey('phone', $meta);
+ $this->assertNull($meta['phone']['new']);
+ }
+
+ public function test_bulk_edit_logs_auth_field_changes_for_eligible_users()
+ {
+ $target = User::factory()->create(['activated' => 1]);
+
+ $existingLogIds = Actionlog::where('item_type', User::class)
+ ->where('item_id', $target->id)
+ ->pluck('id');
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('users/bulkeditsave'), [
+ 'ids' => [$target->id],
+ 'activated' => '0',
+ 'city' => 'Portland',
+ ])
+ ->assertRedirect(route('users.index'));
+
+ $logs = Actionlog::where('item_type', User::class)
+ ->where('item_id', $target->id)
+ ->where('action_type', 'update')
+ ->whereNotIn('id', $existingLogIds)
+ ->get();
+
+ $this->assertCount(1, $logs, 'All changes should appear in a single log entry');
+
+ $meta = json_decode($logs->first()->log_meta, true);
+ $this->assertArrayHasKey('activated', $meta, 'activated change should be logged');
+ $this->assertArrayHasKey('city', $meta, 'city change should be logged');
+ }
+
+ public function test_bulk_edit_creates_one_log_entry_per_user()
+ {
+ $targets = User::factory()->count(3)->create(['city' => 'Old City']);
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('users/bulkeditsave'), [
+ 'ids' => $targets->pluck('id')->all(),
+ 'city' => 'New City',
+ ])
+ ->assertRedirect(route('users.index'));
+
+ foreach ($targets as $target) {
+ $count = Actionlog::where('item_type', User::class)
+ ->where('item_id', $target->id)
+ ->where('action_type', 'update')
+ ->count();
+
+ $this->assertEquals(1, $count, "User {$target->id} should have exactly one update log entry");
+ }
+ }
}