Skip to content

A free open source IT asset/license management system

Fix for legacy location_id=0 issue

snipe9a1acced5861
diff --git a/app/Http/Controllers/Assets/AssetCheckinController.php b/app/Http/Controllers/Assets/AssetCheckinController.php
index 4c0ac970a7..ec0ef1c365 100644
--- a/app/Http/Controllers/Assets/AssetCheckinController.php
+++ b/app/Http/Controllers/Assets/AssetCheckinController.php
@@ -76,9 +76,32 @@ class AssetCheckinController extends Controller
$asset->status_id = e($request->get('status_id'));
}
+ // This is just meant to correct legacy issues where some user data would have 0
+ // as a location ID, which isn't valid. Later versions of Snipe-IT have stricter validation
+ // rules, so it's necessary to fix this for long-time users. It's kinda gross, but will help
+ // people (and their data) in the long run
+
+ if ($asset->rtd_location_id=='0') {
+ \Log::debug('Manually override the RTD location IDs');
+ \Log::debug('Original RTD Location ID: '.$asset->rtd_location_id);
+ $asset->rtd_location_id = '';
+ \Log::debug('New RTD Location ID: '.$asset->rtd_location_id);
+ }
+
+ if ($asset->location_id=='0') {
+ \Log::debug('Manually override the location IDs');
+ \Log::debug('Original Location ID: '.$asset->location_id);
+ $asset->location_id = '';
+ \Log::debug('New RTD Location ID: '.$asset->location_id);
+ }
+
$asset->location_id = $asset->rtd_location_id;
+ \Log::debug('After Location ID: '.$asset->location_id);
+ \Log::debug('After RTD Location ID: '.$asset->rtd_location_id);
+
if ($request->filled('location_id')) {
+ \Log::debug('NEW Location ID: '.$request->get('location_id'));
$asset->location_id = e($request->get('location_id'));
}
@@ -97,6 +120,6 @@ class AssetCheckinController extends Controller
return redirect()->route("hardware.index")->with('success', trans('admin/hardware/message.checkin.success'));
}
// Redirect to the asset management page with error
- return redirect()->route("hardware.index")->with('error', trans('admin/hardware/message.checkin.error'));
+ return redirect()->route("hardware.index")->with('error', trans('admin/hardware/message.checkin.error').$asset->getErrors());
}
}
diff --git a/app/Http/Controllers/Assets/AssetCheckoutController.php b/app/Http/Controllers/Assets/AssetCheckoutController.php
index 5671395982..be3d9113b9 100644
--- a/app/Http/Controllers/Assets/AssetCheckoutController.php
+++ b/app/Http/Controllers/Assets/AssetCheckoutController.php
@@ -80,7 +80,7 @@ class AssetCheckoutController extends Controller
}
// Redirect to the asset management page with error
- return redirect()->to("hardware/$assetId/checkout")->with('error', trans('admin/hardware/message.checkout.error'))->withErrors($asset->getErrors());
+ return redirect()->to("hardware/$assetId/checkout")->with('error', trans('admin/hardware/message.checkout.error').$asset->getErrors());
} catch (ModelNotFoundException $e) {
return redirect()->back()->with('error', trans('admin/hardware/message.checkout.error'))->withErrors($asset->getErrors());
} catch (CheckoutNotAllowed $e) {
diff --git a/database/migrations/2020_10_23_161736_fix_zero_values_for_locations.php b/database/migrations/2020_10_23_161736_fix_zero_values_for_locations.php
new file mode 100644
index 0000000000..477587d71d
--- /dev/null
+++ b/database/migrations/2020_10_23_161736_fix_zero_values_for_locations.php
@@ -0,0 +1,53 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+use App\Models\User;
+use App\Models\Asset;
+
+class FixZeroValuesForLocations extends Migration
+{
+ /**
+ * Run the migrations.
+ *
+ * @return void
+ */
+ public function up()
+ {
+ $assets = Asset::where('location_id', '=', '0')->orWhere('rtd_location_id', '=', '0')->get();
+ $users = User::where('location_id', '=', '0')->get();
+
+ foreach ($assets as $asset) {
+
+ if ($asset->location_id == '0') {
+ $asset->location_id = '';
+ }
+
+ if ($asset->rtd_location_id == '0') {
+ $asset->rtd_location_id = '';
+ }
+
+ $asset->save();
+
+ }
+
+ foreach ($users as $user) {
+ if ($user->location_id == '0') {
+ $user->location_id = '';
+ }
+
+ $user->save();
+ }
+ }
+
+ /**
+ * Reverse the migrations.
+ *
+ * @return void
+ */
+ public function down()
+ {
+ //
+ }
+}