A free open source IT asset/license management system
Check for inconsistencies before activating scoped locations
Tobias Regnery6921df933479
diff --git a/app/Console/Commands/TestLocationsFMCS.php b/app/Console/Commands/TestLocationsFMCS.phpnew file mode 100644index 0000000000..e7562e23cf--- /dev/null+++ b/app/Console/Commands/TestLocationsFMCS.php@@ -0,0 +1,37 @@+<?php++namespace App\Console\Commands;++use App\Helpers\Helper;+use Illuminate\Console\Command;++class TestLocationsFMCS extends Command+{+ /**+ * The name and signature of the console command.+ *+ * @var string+ */+ protected $signature = 'snipeit:test-locations-fmcs';++ /**+ * The console command description.+ *+ * @var string+ */+ protected $description = 'Test for inconsistencies if FullMultipleCompanySupport with scoped locations will be used';++ /**+ * Execute the console command.+ */+ public function handle()+ {+ $this->info('Test for inconsistencies if FullMultipleCompanySupport with scoped locations will be used');+ $this->info('Depending on the database size this will take a while, output will be displayed after the complete test is over');+ $ret = Helper::test_locations_fmcs(true);++ foreach($ret as $output) {+ $this->info($output);+ }+ }+}diff --git a/app/Helpers/Helper.php b/app/Helpers/Helper.phpindex 95a344dce9..dcfb5e995d 100644--- a/app/Helpers/Helper.php+++ b/app/Helpers/Helper.php@@ -12,6 +12,7 @@ use App\Models\Depreciation;use App\Models\Setting;use App\Models\Statuslabel;use App\Models\License;+use App\Models\Location;use Illuminate\Support\Facades\Crypt;use Illuminate\Contracts\Encryption\DecryptException;use Carbon\Carbon;@@ -1529,4 +1530,48 @@ class Helper}return redirect()->back()->with('error', trans('admin/hardware/message.checkout.error'));}++ /**+ * Check for inconsistencies before activating scoped locations with FullMultipleCompanySupport+ * If there are locations with different companies than related objects unforseen problems could arise+ *+ * @author T. Regnery <[email protected]>+ * @since 7.0+ *+ * @param $artisan when false, bail out on first inconsistent entry+ * @return string []+ */+ static public function test_locations_fmcs($artisan) {+ $ret = [];+ $locations = Location::all();++ foreach($locations as $location) {+ $location_company = $location->company_id;++ // depending on the relationship we must use different operations to retrieve the objects+ $keywords_relation = ['many' => ['users', 'assets', 'rtd_assets', 'consumables', 'components', 'accessories', 'assignedAssets', 'assignedAccessories'],+ 'one' => ['parent', 'manager']];++ foreach ($keywords_relation as $relation => $keywords) {+ foreach($keywords as $keyword) {+ if ($relation == 'many') {+ $items = $location->$keyword->all();+ } else {+ $items = array($location->$keyword);+ }++ foreach ($items as $item) {+ if ($item && $item->company_id != $location_company) {+ $ret[] = 'type: ' . get_class($item) . ', id: ' . $item->id . ', company_id: ' . $item->company_id . ', location company_id: ' . $location_company;+ // when called from SettingsController we bail out on the first error+ if (!$artisan) {+ return $ret;+ }+ }+ }+ }+ }+ }+ return $ret;+ }}diff --git a/app/Http/Controllers/SettingsController.php b/app/Http/Controllers/SettingsController.phpindex 2784a0bf18..6f69de8bda 100755--- a/app/Http/Controllers/SettingsController.php+++ b/app/Http/Controllers/SettingsController.php@@ -314,6 +314,7 @@ class SettingsController extends Controller$setting->modellist_displays = implode(',', $request->input('show_in_model_list'));}+ $old_locations_fmcs = $setting->scope_locations_fmcs;$setting->full_multiple_companies_support = $request->input('full_multiple_companies_support', '0');$setting->scope_locations_fmcs = $request->input('scope_locations_fmcs', '0');@@ -322,6 +323,14 @@ class SettingsController extends Controller$setting->scope_locations_fmcs = '0';}+ // check for inconsistencies when activating scoped locations+ if ($old_locations_fmcs == '0' && $setting->scope_locations_fmcs == '1') {+ $ret = Helper::test_locations_fmcs(false);+ if (count($ret) != 0) {+ return redirect()->back()->withInput()->with('error', 'Inconsistencies with scoped locations found, please use php artisan snipeit:test-locations-fmcs for details');+ }+ }+$setting->unique_serial = $request->input('unique_serial', '0');$setting->shortcuts_enabled = $request->input('shortcuts_enabled', '0');$setting->show_images_in_email = $request->input('show_images_in_email', '0');