From 77eb86fbacd0ab2ee5505c2c59969c04d191d8bc Mon Sep 17 00:00:00 2001
From: Tobias Regnery
Date: Mon, 27 Feb 2023 13:54:41 +0100
Subject: [PATCH] Add a backward compatibility setting for locations with
companies
Now that locations have a company_id they get restricted to the users company with FullMultipleCompanySupport.
This breaks backward compatibility, because before everyone can handle locations without restrictions.
Add a setting right below FullMultipleCompanySupport so that everyone can switch to the desired behaviour.
The default is off and the existing behaviour is preserved.
---
.../Controllers/Api/LocationsController.php | 24 +++++++++++---
app/Http/Controllers/LocationsController.php | 17 ++++++++--
app/Http/Controllers/SettingsController.php | 1 +
..._27_092130_add_scope_locations_setting.php | 32 +++++++++++++++++++
resources/lang/de/admin/settings/general.php | 2 ++
resources/lang/en/admin/settings/general.php | 2 ++
resources/views/modals/location.blade.php | 4 +--
resources/views/settings/general.blade.php | 16 +++++++++-
8 files changed, 89 insertions(+), 9 deletions(-)
create mode 100644 database/migrations/2023_02_27_092130_add_scope_locations_setting.php
diff --git a/app/Http/Controllers/Api/LocationsController.php b/app/Http/Controllers/Api/LocationsController.php
index ae9a0085e29f..7193ede585f0 100644
--- a/app/Http/Controllers/Api/LocationsController.php
+++ b/app/Http/Controllers/Api/LocationsController.php
@@ -9,6 +9,7 @@
use App\Http\Transformers\LocationsTransformer;
use App\Http\Transformers\SelectlistTransformer;
use App\Models\Location;
+use App\Models\Setting;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
@@ -66,7 +67,10 @@ public function index(Request $request)
->withCount('rtd_assets as rtd_assets_count')
->withCount('users as users_count');
- $locations = Company::scopeCompanyables($locations);
+ // Only scope locations if the setting is enabled
+ if (Setting::getSettings()->scope_locations_fmcs) {
+ $locations = Company::scopeCompanyables($locations);
+ }
if ($request->filled('search')) {
$locations = $locations->TextSearch($request->input('search'));
@@ -144,9 +148,13 @@ public function store(ImageUploadRequest $request)
$this->authorize('create', Location::class);
$location = new Location;
$location->fill($request->all());
- $location->company_id = Company::getIdForCurrentUser($request->get('company_id'));
$location = $request->handleImages($location);
+ // Only scope location if the setting is enabled
+ if (Setting::getSettings()->scope_locations_fmcs) {
+ $location->company_id = Company::getIdForCurrentUser($request->get('company_id'));
+ }
+
if ($location->save()) {
return response()->json(Helper::formatStandardApiResponse('success', (new LocationsTransformer)->transformLocation($location), trans('admin/locations/message.create.success')));
}
@@ -210,7 +218,12 @@ public function update(ImageUploadRequest $request, $id)
$location = $request->handleImages($location);
if ($request->filled('company_id')) {
- $location->company_id = Company::getIdForCurrentUser($request->get('company_id'));
+ // Only scope location if the setting is enabled
+ if (Setting::getSettings()->scope_locations_fmcs) {
+ $location->company_id = Company::getIdForCurrentUser($request->get('company_id'));
+ } else {
+ $location->company_id = $request->get('company_id');
+ }
}
if ($location->isValid()) {
@@ -290,7 +303,10 @@ public function selectlist(Request $request)
'locations.image',
]);
- $locations = Company::scopeCompanyables($locations);
+ // Only scope locations if the setting is enabled
+ if (Setting::getSettings()->scope_locations_fmcs) {
+ $locations = Company::scopeCompanyables($locations);
+ }
$page = 1;
if ($request->filled('page')) {
diff --git a/app/Http/Controllers/LocationsController.php b/app/Http/Controllers/LocationsController.php
index 1cac1464204c..a9db4ee67a33 100755
--- a/app/Http/Controllers/LocationsController.php
+++ b/app/Http/Controllers/LocationsController.php
@@ -7,6 +7,7 @@
use App\Models\Company;
use App\Models\Location;
use App\Models\User;
+use App\Models\Setting;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
@@ -79,9 +80,15 @@ public function store(ImageUploadRequest $request)
$location->zip = $request->input('zip');
$location->ldap_ou = $request->input('ldap_ou');
$location->manager_id = $request->input('manager_id');
- $location->company_id = Company::getIdForCurrentUser($request->input('company_id'));
$location->user_id = Auth::id();
+ // Only scope the location if the setting is enabled
+ if (Setting::getSettings()->scope_locations_fmcs) {
+ $location->company_id = Company::getIdForCurrentUser($request->input('company_id'));
+ } else {
+ $location->company_id = $request->input('company_id');
+ }
+
$location = $request->handleImages($location);
if ($location->save()) {
@@ -143,7 +150,13 @@ public function update(ImageUploadRequest $request, $locationId = null)
$location->zip = $request->input('zip');
$location->ldap_ou = $request->input('ldap_ou');
$location->manager_id = $request->input('manager_id');
- $location->company_id = Company::getIdForCurrentUser($request->input('company_id'));
+
+ // Only scope the location if the setting is enabled
+ if (Setting::getSettings()->scope_locations_fmcs) {
+ $location->company_id = Company::getIdForCurrentUser($request->input('company_id'));
+ } else {
+ $location->company_id = $request->input('company_id');
+ }
$location = $request->handleImages($location);
diff --git a/app/Http/Controllers/SettingsController.php b/app/Http/Controllers/SettingsController.php
index b04c692ac527..1ccd22b27cea 100755
--- a/app/Http/Controllers/SettingsController.php
+++ b/app/Http/Controllers/SettingsController.php
@@ -332,6 +332,7 @@ public function postSettings(Request $request)
}
$setting->full_multiple_companies_support = $request->input('full_multiple_companies_support', '0');
+ $setting->scope_locations_fmcs = $request->input('scope_locations_fmcs', '0');
$setting->unique_serial = $request->input('unique_serial', '0');
$setting->show_images_in_email = $request->input('show_images_in_email', '0');
$setting->show_archived_in_list = $request->input('show_archived_in_list', '0');
diff --git a/database/migrations/2023_02_27_092130_add_scope_locations_setting.php b/database/migrations/2023_02_27_092130_add_scope_locations_setting.php
new file mode 100644
index 000000000000..b73bb496b197
--- /dev/null
+++ b/database/migrations/2023_02_27_092130_add_scope_locations_setting.php
@@ -0,0 +1,32 @@
+boolean('scope_locations_fmcs')->default('0');
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ *
+ * @return void
+ */
+ public function down()
+ {
+ Schema::table('locations', function (Blueprint $table) {
+ $table->dropColumn('scope_locations_fmcs');
+ });
+ }
+}
\ No newline at end of file
diff --git a/resources/lang/de/admin/settings/general.php b/resources/lang/de/admin/settings/general.php
index acae3fee9839..682a7a199463 100644
--- a/resources/lang/de/admin/settings/general.php
+++ b/resources/lang/de/admin/settings/general.php
@@ -142,6 +142,8 @@
'logo_print_assets_help' => 'Firmenlogo anzeigen beim Drucken der Asset-Liste ',
'full_multiple_companies_support_help_text' => 'Beschränkung von Benutzern (inklusive Administratoren) die einer Firma zugewiesen sind zu den Assets der Firma.',
'full_multiple_companies_support_text' => 'Volle Mehrmandanten-Unterstützung für Firmen',
+ 'scope_locations_fmcs_support_text' => 'Beschränke Standorte mit voller Mehrmandanten-Unterstützung für Firmen',
+ 'scope_locations_fmcs_support_help_text' => 'Bis zu Version 6.x waren Standorte nicht auf die Firma des Benutzers beschränkt. Wenn diese Einstellung deaktiviert ist, wird die Kompatibilität zu älteren Versionen gewahrt und die Standorte nicht beschränkt. Wenn diese Einstellung aktiviert ist, werden Standorte ebenfalls auf die Firma des Benutzers beschränkt.',
'show_in_model_list' => 'In Modell-Dropdown-Liste anzeigen',
'optional' => 'optional',
'per_page' => 'Ergebnisse pro Seite',
diff --git a/resources/lang/en/admin/settings/general.php b/resources/lang/en/admin/settings/general.php
index 70c4932fda5b..d2e5bc4c86d9 100644
--- a/resources/lang/en/admin/settings/general.php
+++ b/resources/lang/en/admin/settings/general.php
@@ -142,6 +142,8 @@
'logo_print_assets_help' => 'Use branding on printable asset lists ',
'full_multiple_companies_support_help_text' => 'Restricting users (including admins) assigned to companies to their company\'s assets.',
'full_multiple_companies_support_text' => 'Full Multiple Companies Support',
+ 'scope_locations_fmcs_support_text' => 'Scope Locations with Full Multiple Companies Support',
+ 'scope_locations_fmcs_support_help_text' => 'Up until Version 6.x locations were not restricted to the users company. If this setting is disabled, this preserves backward compatibility with older versions and locations are not restricted. If this setting is enabled, locations are also restricted to the users company',
'show_in_model_list' => 'Show in Model Dropdowns',
'optional' => 'optional',
'per_page' => 'Results Per Page',
diff --git a/resources/views/modals/location.blade.php b/resources/views/modals/location.blade.php
index 9f30192ca0d0..25fba7fd19c0 100644
--- a/resources/views/modals/location.blade.php
+++ b/resources/views/modals/location.blade.php
@@ -10,8 +10,8 @@