Skip to content

Commit

Permalink
Check for inconsistencies before activating scoped locations
Browse files Browse the repository at this point in the history
Before activating scoped location all locations and their related objects will be checked.
If there are locations with different companies than the related objects error out.

Because this operation is quite slow, bail out on the first inconsistent entry.
There is a new artisan command introduced that checks every location.

Depending on the size of the database, this will take very long.

Signed-off-by: Tobias Regnery <[email protected]>
  • Loading branch information
Toreg87 committed Jan 23, 2025
1 parent b6f05bf commit 6921df9
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
37 changes: 37 additions & 0 deletions app/Console/Commands/TestLocationsFMCS.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
45 changes: 45 additions & 0 deletions app/Helpers/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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;
Expand Down Expand Up @@ -1529,4 +1530,48 @@ static public function getRedirectOption($request, $id, $table, $item_id = null)
}
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;
}
}
9 changes: 9 additions & 0 deletions app/Http/Controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ public function postSettings(Request $request) : RedirectResponse
$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');

Expand All @@ -322,6 +323,14 @@ public function postSettings(Request $request) : RedirectResponse
$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');
Expand Down

0 comments on commit 6921df9

Please sign in to comment.