Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 5 additions & 68 deletions app/Console/Commands/MergeUsersByUsername.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace App\Console\Commands;

use App\Events\UserMerged;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Console\Command;

class MergeUsersByUsername extends Command
Expand Down Expand Up @@ -59,72 +57,11 @@ public function handle()

foreach ($bad_users as $bad_user) {
$this->info($bad_user->username.' ('.$bad_user->id.') will be merged into '.$user->username.' ('.$user->id.') ');

// Walk the list of assets
foreach ($bad_user->assets as $asset) {
$this->info('Updating asset '.$asset->asset_tag.' '.$asset->id.' to user '.$user->id);
$asset->assigned_to = $user->id;
if (! $asset->save()) {
$this->error('Could not update assigned_to field on asset '.$asset->asset_tag.' '.$asset->id.' to user '.$user->id);
$this->error('Error saving: '.$asset->getErrors());
}
}

// Walk the list of licenses
foreach ($bad_user->licenses as $license) {
$this->info('Updating license '.$license->name.' '.$license->id.' to user '.$user->id);
$bad_user->licenses()->updateExistingPivot($license->id, ['assigned_to' => $user->id]);
}

// Walk the list of consumables
foreach ($bad_user->consumables as $consumable) {
$this->info('Updating consumable '.$consumable->id.' to user '.$user->id);
$bad_user->consumables()->updateExistingPivot($consumable->id, ['assigned_to' => $user->id]);
}

// Walk the list of accessories
foreach ($bad_user->accessories as $accessory) {
$this->info('Updating accessory '.$accessory->id.' to user '.$user->id);
$bad_user->accessories()->updateExistingPivot($accessory->id, ['assigned_to' => $user->id]);
}

// Walk the list of logs
foreach ($bad_user->userlog as $log) {
$this->info('Updating action log record '.$log->id.' to user '.$user->id);
$log->target_id = $user->id;
$log->save();
}

// Update any manager IDs
$this->info('Updating managed user records to user '.$user->id);
User::where('manager_id', '=', $bad_user->id)->update(['manager_id' => $user->id]);

// Update location manager IDs
foreach ($bad_user->managedLocations as $managedLocation) {
$this->info('Updating managed location record '.$managedLocation->name.' to manager '.$user->id);
$managedLocation->manager_id = $user->id;
$managedLocation->save();
}

foreach ($bad_user->uploads as $upload) {
$this->info('Updating upload log record '.$upload->id.' to user '.$user->id);
$upload->item_id = $user->id;
$upload->save();
}

foreach ($bad_user->acceptances as $acceptance) {
$this->info('Updating acceptance log record '.$acceptance->id.' to user '.$user->id);
$acceptance->item_id = $user->id;
$acceptance->save();
}

// Mark the user as deleted
$this->info('Marking the user as deleted');
$bad_user->deleted_at = Carbon::now()->timestamp;
$bad_user->save();

event(new UserMerged($bad_user, $user, null));

$user->merge($bad_user, function ($msg) {
$this->info($msg);
}, function ($error) {
$this->error($error);
});

}
}
Expand Down
1 change: 1 addition & 0 deletions app/Enums/ActionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ enum ActionType: string {
case NoteAdded = 'note added';
case Audit = 'audit';
case Checkout = 'checkout';
case Merged = 'merged';
}
24 changes: 0 additions & 24 deletions app/Events/UserMerged.php

This file was deleted.

50 changes: 1 addition & 49 deletions app/Http/Controllers/Users/BulkUsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
namespace App\Http\Controllers\Users;

use App\Enums\ActionType;
use App\Events\UserMerged;
use App\Helpers\Helper;
use App\Http\Controllers\Controller;
use App\Models\Accessory;
use App\Models\License;
use App\Models\Actionlog;
use App\Models\Asset;
use App\Models\Group;
use App\Models\LicenseSeat;
Expand All @@ -17,13 +15,11 @@
use App\Models\Setting;
use App\Models\User;
use App\Notifications\CurrentInventory;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Password;
use Illuminate\Support\Facades\Log;

class BulkUsersController extends Controller
{
Expand Down Expand Up @@ -402,51 +398,7 @@ public function merge(Request $request)
// Walk users
foreach ($users_to_merge as $user_to_merge) {

foreach ($user_to_merge->assets as $asset) {
Log::debug('Updating asset: '.$asset->asset_tag . ' to '.$merge_into_user->id);
$asset->assigned_to = $request->input('merge_into_id');
$asset->save();
}

foreach ($user_to_merge->licenses as $license) {
Log::debug('Updating license pivot: '.$license->id . ' to '.$merge_into_user->id);
$user_to_merge->licenses()->updateExistingPivot($license->id, ['assigned_to' => $merge_into_user->id]);
}

foreach ($user_to_merge->consumables as $consumable) {
Log::debug('Updating consumable pivot: '.$consumable->id . ' to '.$merge_into_user->id);
$user_to_merge->consumables()->updateExistingPivot($consumable->id, ['assigned_to' => $merge_into_user->id]);
}

foreach ($user_to_merge->accessories as $accessory) {
$user_to_merge->accessories()->updateExistingPivot($accessory->id, ['assigned_to' => $merge_into_user->id]);
}

foreach ($user_to_merge->userlog as $log) {
$log->target_id = $merge_into_user->id;
$log->save();
}

foreach ($user_to_merge->uploads as $upload) {
$upload->item_id = $merge_into_user->id;
$upload->save();
}

foreach ($user_to_merge->acceptances as $acceptance) {
$acceptance->item_id = $merge_into_user->id;
$acceptance->save();
}

User::where('manager_id', '=', $user_to_merge->id)->update(['manager_id' => $merge_into_user->id]);

foreach ($user_to_merge->managedLocations as $managedLocation) {
$managedLocation->manager_id = $merge_into_user->id;
$managedLocation->save();
}

$user_to_merge->delete();

event(new UserMerged($user_to_merge, $merge_into_user, $admin));
$merge_into_user->merge($user_to_merge);

}

Expand Down
41 changes: 0 additions & 41 deletions app/Listeners/LogListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
use App\Events\AccessoryCheckedOut;
use App\Events\AssetCheckedIn;
use App\Events\AssetCheckedOut;
use App\Events\CheckoutableCheckedIn;
use App\Events\CheckoutableCheckedOut;
use App\Events\CheckoutAccepted;
use App\Events\CheckoutDeclined;
use App\Events\ComponentCheckedIn;
Expand All @@ -19,9 +17,7 @@
use App\Events\LicenseCheckedIn;
use App\Events\LicenseCheckedOut;
use App\Models\Actionlog;
use App\Models\User;
use App\Models\LicenseSeat;
use App\Events\UserMerged;
use Illuminate\Support\Facades\Log;

class LogListener
Expand Down Expand Up @@ -70,41 +66,6 @@ public function onCheckoutDeclined(CheckoutDeclined $event)
}


public function onUserMerged(UserMerged $event)
{

$to_from_array = [
'to_id' => $event->merged_to->id,
'to_username' => $event->merged_to->username,
'from_id' => $event->merged_from->id,
'from_username' => $event->merged_from->username,
];

// Add a record to the users being merged FROM
Log::debug('Users merged: '.$event->merged_from->id .' ('.$event->merged_from->username.') merged into '. $event->merged_to->id. ' ('.$event->merged_to->username.')');
$logaction = new Actionlog();
$logaction->item_id = $event->merged_from->id;
$logaction->item_type = User::class;
$logaction->target_id = $event->merged_to->id;
$logaction->target_type = User::class;
$logaction->action_type = 'merged';
$logaction->note = trans('general.merged_log_this_user_from', $to_from_array);
$logaction->created_by = $event->admin->id ?? null;
$logaction->save();

// Add a record to the users being merged TO
$logaction = new Actionlog();
$logaction->target_id = $event->merged_from->id;
$logaction->target_type = User::class;
$logaction->item_id = $event->merged_to->id;
$logaction->item_type = User::class;
$logaction->action_type = 'merged';
$logaction->note = trans('general.merged_log_this_user_into', $to_from_array);
$logaction->created_by = $event->admin->id ?? null;
$logaction->save();


}

/**
* Register the listeners for the subscriber.
Expand All @@ -116,8 +77,6 @@ public function subscribe($events)
$list = [
'CheckoutAccepted',
'CheckoutDeclined',
'UserMerged',
'NoteAdded',
];

foreach ($list as $event) {
Expand Down
98 changes: 98 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace App\Models;

use App\Enums\ActionType;
use App\Http\Traits\UniqueUndeletedTrait;
use App\Models\Traits\Loggable;
use App\Models\Traits\Searchable;
use App\Models\Traits\HasUploads;
use App\Presenters\Presentable;
use Carbon\Carbon;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
Expand All @@ -21,6 +23,7 @@
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Log;
use Laravel\Passport\HasApiTokens;
use Watson\Validating\ValidatingTrait;
use Illuminate\Database\Eloquent\Casts\Attribute;
Expand Down Expand Up @@ -1163,4 +1166,99 @@ public function isManagerOf(User $userToCheck): bool
}
return false;
}

public function merge(User $bad_user, ?callable $status_callback = null, ?callable $error_callback = null)
{
if (!$status_callback) {
$status_callback = function ($msg) {
Log::debug("User Merge: " . $msg);
};
}
if (!$error_callback) {
$error_callback = function ($msg) {
Log::error("User Merge ERROR: " . $msg);
};
}
// Walk the list of assets
foreach ($bad_user->assets as $asset) {
$status_callback('Updating asset ' . $asset->asset_tag . ' ' . $asset->id . ' to user ' . $this->id);
$asset->assigned_to = $this->id;
if (!$asset->save()) {
$error_callback('Could not update assigned_to field on asset ' . $asset->asset_tag . ' ' . $asset->id . ' to user ' . $this->id);
$error_callback('Error saving: ' . $asset->getErrors());
}
}

// Walk the list of licenses
foreach ($bad_user->licenses as $license) {
$status_callback('Updating license ' . $license->name . ' ' . $license->id . ' to user ' . $this->id);
$bad_user->licenses()->updateExistingPivot($license->id, ['assigned_to' => $this->id]);
}

// Walk the list of consumables
foreach ($bad_user->consumables as $consumable) {
$status_callback('Updating consumable ' . $consumable->id . ' to user ' . $this->id);
$bad_user->consumables()->updateExistingPivot($consumable->id, ['assigned_to' => $this->id]);
}

// Walk the list of accessories
foreach ($bad_user->accessories as $accessory) {
$status_callback('Updating accessory ' . $accessory->id . ' to user ' . $this->id);
$bad_user->accessories()->updateExistingPivot($accessory->id, ['assigned_to' => $this->id]);
}

// Walk the list of logs
foreach ($bad_user->userlog as $log) {
$status_callback('Updating action log record ' . $log->id . ' to user ' . $this->id);
$log->target_id = $this->id;
$log->save();
}

// Update any manager IDs
$status_callback('Updating managed user records to user ' . $this->id);
User::where('manager_id', '=', $bad_user->id)->update(['manager_id' => $this->id]);

// Update location manager IDs
foreach ($bad_user->managedLocations as $managedLocation) {
$status_callback('Updating managed location record ' . $managedLocation->name . ' to manager ' . $this->id);
$managedLocation->manager_id = $this->id;
$managedLocation->save();
}

foreach ($bad_user->uploads as $upload) {
$status_callback('Updating upload log record ' . $upload->id . ' to user ' . $this->id);
$upload->item_id = $this->id;
$upload->save();
}

foreach ($bad_user->acceptances as $acceptance) {
$status_callback('Updating acceptance log record ' . $acceptance->id . ' to user ' . $this->id);
$acceptance->item_id = $this->id;
$acceptance->save();
}

// Mark the user as deleted
$status_callback('Marking the user as deleted');
$bad_user->delete();

// this was in the LogListener before
$to_from_array = [
'to_id' => $this->id,
'to_username' => $this->username,
'from_id' => $bad_user->id,
'from_username' => $bad_user->username,
];

// Add a record to the users being merged FROM
Log::debug('Users merged: ' . $bad_user->id . ' (' . $bad_user->username . ') merged into ' . $this->id . ' (' . $this->username . ')');
$bad_user->setLogTarget($this);
$bad_user->setLogNote(trans('general.merged_log_this_user_from', $to_from_array));
$bad_user->saveWithActionType(ActionType::Merged);

// Add a record to the users being merged TO
$this->setLogTarget($bad_user);
$this->setLogNote(trans('general.merged_log_this_user_into', $to_from_array));
$this->saveWithActionType(ActionType::Merged);

}
}
Loading