From ab1fe8be0c72522273c468dfd5551553d9f92665 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 25 Jun 2020 17:42:39 -0700 Subject: [PATCH 1/2] Added merge utility --- app/Console/Commands/MergeUsersByUsername.php | 113 ++++++++++++++++++ app/Models/User.php | 2 +- 2 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 app/Console/Commands/MergeUsersByUsername.php diff --git a/app/Console/Commands/MergeUsersByUsername.php b/app/Console/Commands/MergeUsersByUsername.php new file mode 100644 index 000000000000..00b428dc7b7e --- /dev/null +++ b/app/Console/Commands/MergeUsersByUsername.php @@ -0,0 +1,113 @@ +whereNull('deleted_at')->get(); + + foreach ($users as $user) { + $parts = explode("@", $user->username); + $bad_users = User::where('username', '=', $parts[0])->whereNull('deleted_at')->with('assets', 'manager', 'userlog', 'licenses', 'consumables', 'accessories', 'managedLocations')->get(); + + 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; + $asset->save(); + } + + // 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(); + } + + // Mark the user as deleted + $this->info( 'Marking the user as deleted'); + $bad_user->deleted_at = Carbon::now()->timestamp; + $bad_user->save(); + + + } + + } + + + } +} diff --git a/app/Models/User.php b/app/Models/User.php index 81a1fd69459c..413ed98a5f8f 100755 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -237,7 +237,7 @@ public function licenses() */ public function userlog() { - return $this->hasMany('\App\Models\Actionlog', 'target_id')->orderBy('created_at', 'DESC')->withTrashed(); + return $this->hasMany('\App\Models\Actionlog', 'target_id')->where('target_type', '=', 'App\Models\User')->orderBy('created_at', 'DESC')->withTrashed(); } /** From cc9f1577a47708a6e11dffeab4797982be243cfa Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 25 Jun 2020 17:43:53 -0700 Subject: [PATCH 2/2] Removed unused use directives --- app/Console/Commands/MergeUsersByUsername.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/app/Console/Commands/MergeUsersByUsername.php b/app/Console/Commands/MergeUsersByUsername.php index 00b428dc7b7e..ded5d1986fca 100644 --- a/app/Console/Commands/MergeUsersByUsername.php +++ b/app/Console/Commands/MergeUsersByUsername.php @@ -4,10 +4,6 @@ use Illuminate\Console\Command; use App\Models\User; -use App\Models\Actionlog; -use App\Models\Asset; -use App\Models\LicenseSeat; -use App\Models\Consumable; use Carbon\Carbon;