-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added #2353: Add ability to tie locations to companies - 2023 edition #12577
base: develop
Are you sure you want to change the base?
Changes from all commits
1ccbf89
1318dc6
651d1c7
b6f05bf
6921df9
4e0bcac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?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 {--location_id=}'; | ||
|
||
/** | ||
* 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'); | ||
|
||
// if parameter location_id is set, only test this location | ||
$location_id = null; | ||
if ($this->option('location_id')) { | ||
$location_id = $this->option('location_id'); | ||
} | ||
$ret = Helper::test_locations_fmcs(true, $location_id); | ||
|
||
foreach($ret as $output) { | ||
$this->info($output); | ||
} | ||
} | ||
} |
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,68 @@ 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 | ||
* @param $location_id when set, only test this specific location | ||
* @param $new_company_id in case of updating a location, this is the newly requested company_id | ||
* @return string [] | ||
*/ | ||
static public function test_locations_fmcs($artisan, $location_id = null, $new_company_id = null) { | ||
$ret = []; | ||
|
||
if ($location_id) { | ||
$location = Location::find($location_id); | ||
if ($location) { | ||
$locations = collect([])->push(Location::find($location_id)); | ||
} | ||
} else { | ||
$locations = Location::all(); | ||
} | ||
|
||
foreach($locations as $location) { | ||
// in case of an update of a single location use the newly requested company_id | ||
if ($new_company_id) { | ||
$location_company = $new_company_id; | ||
} else { | ||
$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']]; | ||
|
||
// In case of a single location the children must be checked either, becuase we don't walk every location | ||
if ($location_id) { | ||
$keywords_relation['many'][] = 'children'; | ||
} | ||
|
||
foreach ($keywords_relation as $relation => $keywords) { | ||
foreach($keywords as $keyword) { | ||
if ($relation == 'many') { | ||
$items = $location->$keyword->all(); | ||
} else { | ||
$items = collect([])->push($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 not called from artisan command we bail out on the first error | ||
if (!$artisan) { | ||
return $ret; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return $ret; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -314,7 +314,23 @@ 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'); | ||
|
||
// Backward compatibility for locations makes no sense without FullMultipleCompanySupport | ||
if (!$setting->full_multiple_companies_support) { | ||
$setting->scope_locations_fmcs = '0'; | ||
} | ||
Comment on lines
+321
to
+324
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This makes sense but might confuse some users. I wonder if there is a way to concisely express this on the settings page? 🤔 // @snipe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this can be handled in the view with some javascript. If this is a problem I can think about it some more, but my experience with this is quite limited. |
||
|
||
// 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'); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be wrapped in the following like we have in other places?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is in line with other scoped controllers and not about the company_id or am I misunderstanding something?