-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check for inconsistencies before activating scoped locations
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
Showing
3 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters