Skip to content

A free open source IT asset/license management system

Style changes requested

snipea27c551f64a3
diff --git a/app/Http/Controllers/Accessories/AccessoryCheckoutController.php b/app/Http/Controllers/Accessories/AccessoryCheckoutController.php
index 331325d2e1..3f0bc8054a 100644
--- a/app/Http/Controllers/Accessories/AccessoryCheckoutController.php
+++ b/app/Http/Controllers/Accessories/AccessoryCheckoutController.php
@@ -69,11 +69,14 @@ class AccessoryCheckoutController extends Controller
if (Setting::getSettings()->full_multiple_companies_support == '1' && $accessory->company_id) {
if ($target instanceof User) {
+ // Users may belong to multiple companies; canReceiveFromCompany() checks the pivot.
$mismatch = ! $target->canReceiveFromCompany($accessory->company_id);
+ } elseif (is_null($target->company_id)) {
+ // Target has no company — only a mismatch when floater mode is off.
+ $mismatch = ! Setting::getSettings()->null_company_is_floater;
} else {
- $mismatch = is_null($target->company_id)
- ? ! Setting::getSettings()->null_company_is_floater
- : (int) $target->company_id !== (int) $accessory->company_id;
+ // Both sides have a company; require an exact match.
+ $mismatch = (int) $target->company_id !== (int) $accessory->company_id;
}
if ($mismatch) {
diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php
index 550bd29074..8930f376a1 100644
--- a/app/Http/Controllers/Api/AssetsController.php
+++ b/app/Http/Controllers/Api/AssetsController.php
@@ -927,9 +927,13 @@ class AssetsController extends Controller
return null;
}
- $nonUserMismatch = is_null($target->company_id)
- ? ! Setting::getSettings()->null_company_is_floater
- : (int) $asset->company_id !== (int) $target->company_id;
+ if (is_null($target->company_id)) {
+ // Target has no company — only a mismatch when floater mode is off.
+ $nonUserMismatch = ! Setting::getSettings()->null_company_is_floater;
+ } else {
+ // Both sides have a company; require an exact match.
+ $nonUserMismatch = (int) $asset->company_id !== (int) $target->company_id;
+ }
if ($nonUserMismatch) {
return response()->json(Helper::formatStandardApiResponse('error', null, trans('general.error_user_company')));
diff --git a/app/Http/Controllers/Api/LocationsController.php b/app/Http/Controllers/Api/LocationsController.php
index fe1448b3c2..bfa27a5a28 100644
--- a/app/Http/Controllers/Api/LocationsController.php
+++ b/app/Http/Controllers/Api/LocationsController.php
@@ -215,7 +215,8 @@ class LocationsController extends Controller
if (Setting::getSettings()->scope_locations_fmcs) {
$location->company_id = Company::getIdForCurrentUser($request->input('company_id'));
// check if parent is set and has a different company
- if ($location->parent_id && ($parent = Location::find($location->parent_id)) && $parent->company_id != $location->company_id) {
+ $parent = $location->parent_id ? Location::find($location->parent_id) : null;
+ if ($parent && $parent->company_id != $location->company_id) {
return response()->json(Helper::formatStandardApiResponse('error', null, trans('general.error_location_parent_company', [
'parent' => $parent->name,
'parent_company' => $parent->company?->name ?? trans('general.unassigned'),
@@ -321,7 +322,8 @@ class LocationsController extends Controller
])));
}
// check if parent is set and has a different company
- if ($location->parent_id && ($parent = Location::find($location->parent_id)) && $parent->company_id != $location->company_id) {
+ $parent = $location->parent_id ? Location::find($location->parent_id) : null;
+ if ($parent && $parent->company_id != $location->company_id) {
return response()->json(Helper::formatStandardApiResponse('error', null, trans('general.error_location_parent_company', [
'parent' => $parent->name,
'parent_company' => $parent->company?->name ?? trans('general.unassigned'),
diff --git a/app/Http/Controllers/Api/UsersController.php b/app/Http/Controllers/Api/UsersController.php
index 6fb22ef6ea..49c3960055 100644
--- a/app/Http/Controllers/Api/UsersController.php
+++ b/app/Http/Controllers/Api/UsersController.php
@@ -404,16 +404,7 @@ class UsersController extends Controller
if ((Setting::getSettings()->full_multiple_companies_support == '1') && $request->filled('companyId')) {
$companyIds = array_values(array_filter(array_map('intval', explode(',', $request->input('companyId')))));
if (! empty($companyIds)) {
- if (Setting::getSettings()->null_company_is_floater) {
- // Floater mode: users with no company associations can receive items from
- // any company, so include them alongside exact-company matches.
- $users->where(function ($q) use ($companyIds) {
- $q->whereHas('companies', fn ($q2) => $q2->whereIn('companies.id', $companyIds))
- ->orWhereDoesntHave('companies');
- });
- } else {
- $users->whereHas('companies', fn ($q) => $q->whereIn('companies.id', $companyIds));
- }
+ $users = Company::scopeUsersByCompanyIds($users, $companyIds);
}
}
diff --git a/app/Http/Controllers/Assets/AssetCheckoutController.php b/app/Http/Controllers/Assets/AssetCheckoutController.php
index 0e6e7cda0d..86b9b9897e 100644
--- a/app/Http/Controllers/Assets/AssetCheckoutController.php
+++ b/app/Http/Controllers/Assets/AssetCheckoutController.php
@@ -124,11 +124,16 @@ class AssetCheckoutController extends Controller
// Locations have no company, so we only enforce FMCS when both sides have a company_id.
// For users with multiple companies, check all their associated companies via the pivot.
if ($settings->full_multiple_companies_support && ! is_null($asset->company_id)) {
- $mismatch = $target instanceof User
- ? ! $target->canReceiveFromCompany((int) $asset->company_id)
- : (is_null($target->company_id)
- ? ! $settings->null_company_is_floater
- : (int) $target->company_id !== (int) $asset->company_id);
+ if ($target instanceof User) {
+ // Users may belong to multiple companies; canReceiveFromCompany() checks the pivot.
+ $mismatch = ! $target->canReceiveFromCompany((int) $asset->company_id);
+ } elseif (is_null($target->company_id)) {
+ // Target has no company — only a mismatch when floater mode is off.
+ $mismatch = ! $settings->null_company_is_floater;
+ } else {
+ // Both sides have a company; require an exact match.
+ $mismatch = (int) $target->company_id !== (int) $asset->company_id;
+ }
if ($mismatch) {
$targetType = match (class_basename($target)) {
@@ -138,7 +143,7 @@ class AssetCheckoutController extends Controller
};
return redirect()->route('hardware.checkout.create', $asset)->with('error', trans('general.error_checkout_company_mismatch', [
- 'item' => trans('general.asset').' "'.($asset->name ?? $asset->asset_tag).'"',
+ 'item' => trans('general.asset').' "'.$asset->display_name.'"',
'item_company' => $asset->company?->name ?? trans('general.unassigned'),
'target' => $targetType.' "'.($target->name ?? $target->username ?? $target->id).'"',
]));
diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php
index 889ed68891..7c6e161da2 100644
--- a/app/Http/Controllers/Assets/BulkAssetsController.php
+++ b/app/Http/Controllers/Assets/BulkAssetsController.php
@@ -696,12 +696,19 @@ class BulkAssetsController extends Controller
if ($company_ids->isNotEmpty()) {
$assetCompanyId = (int) $company_ids->first();
- $mismatch = $company_ids->count() > 1
- || ($target instanceof User
- ? ! $target->canReceiveFromCompany($assetCompanyId)
- : (is_null($target->company_id)
- ? ! Setting::getSettings()->null_company_is_floater
- : (int) $target->company_id !== $assetCompanyId));
+ if ($company_ids->count() > 1) {
+ // Selected assets span multiple companies; bulk checkout can't satisfy all of them.
+ $mismatch = true;
+ } elseif ($target instanceof User) {
+ // Users may belong to multiple companies; canReceiveFromCompany() checks the pivot.
+ $mismatch = ! $target->canReceiveFromCompany($assetCompanyId);
+ } elseif (is_null($target->company_id)) {
+ // Target has no company — only a mismatch when floater mode is off.
+ $mismatch = ! Setting::getSettings()->null_company_is_floater;
+ } else {
+ // Both sides have a company; require an exact match.
+ $mismatch = (int) $target->company_id !== $assetCompanyId;
+ }
if ($mismatch) {
$request->session()->flashInput(['selected_assets' => $asset_ids]);
diff --git a/app/Http/Controllers/Licenses/LicenseCheckoutController.php b/app/Http/Controllers/Licenses/LicenseCheckoutController.php
index fc96b9f74b..edcbde1744 100644
--- a/app/Http/Controllers/Licenses/LicenseCheckoutController.php
+++ b/app/Http/Controllers/Licenses/LicenseCheckoutController.php
@@ -100,9 +100,13 @@ class LicenseCheckoutController extends Controller
if ($request->filled('asset_id')) {
$fmcsTarget = Asset::find($request->input('asset_id'));
if ($fmcsTarget && $license->company_id) {
- $mismatch = is_null($fmcsTarget->company_id)
- ? ! Setting::getSettings()->null_company_is_floater
- : ($license->company_id !== $fmcsTarget->company_id);
+ if (is_null($fmcsTarget->company_id)) {
+ // Target asset has no company — only a mismatch when floater mode is off.
+ $mismatch = ! Setting::getSettings()->null_company_is_floater;
+ } else {
+ // Both sides have a company; require an exact match.
+ $mismatch = $license->company_id !== $fmcsTarget->company_id;
+ }
if ($mismatch) {
return redirect()->route('licenses.index')->with('error', trans('general.error_checkout_company_mismatch', [
'item' => trans('general.license').' "'.$license->name.'"',
diff --git a/app/Http/Controllers/LocationsController.php b/app/Http/Controllers/LocationsController.php
index da149d91ca..8b25784f38 100755
--- a/app/Http/Controllers/LocationsController.php
+++ b/app/Http/Controllers/LocationsController.php
@@ -95,7 +95,8 @@ class LocationsController extends Controller
if (Setting::getSettings()->scope_locations_fmcs) {
$location->company_id = Company::getIdForCurrentUser($request->input('company_id'));
// check if parent is set and has a different company
- if ($location->parent_id && ($parent = Location::find($location->parent_id)) && $parent->company_id != $location->company_id) {
+ $parent = $location->parent_id ? Location::find($location->parent_id) : null;
+ if ($parent && $parent->company_id != $location->company_id) {
return redirect()->back()->withInput()->with('error', trans('general.error_location_parent_company', [
'parent' => $parent->name,
'parent_company' => $parent->company?->name ?? trans('general.unassigned'),
@@ -189,7 +190,8 @@ class LocationsController extends Controller
]));
}
// check if parent is set and has a different company
- if ($location->parent_id && ($parent = Location::find($location->parent_id)) && $parent->company_id != $location->company_id) {
+ $parent = $location->parent_id ? Location::find($location->parent_id) : null;
+ if ($parent && $parent->company_id != $location->company_id) {
return redirect()->back()->withInput()->with('error', trans('general.error_location_parent_company', [
'parent' => $parent->name,
'parent_company' => $parent->company?->name ?? trans('general.unassigned'),
diff --git a/app/Models/Company.php b/app/Models/Company.php
index 2b2c01da2e..1466571a81 100644
--- a/app/Models/Company.php
+++ b/app/Models/Company.php
@@ -416,14 +416,12 @@ final class Company extends SnipeModel
});
}
- // Floater: also include users with no company associations (they float).
+ // Floater: also include users with no company associations (they float). They all float down here, Georgie.).
if ($floater) {
return $query->where(function ($q) use ($companyIds) {
$q->whereIn('users.id', function ($sub) use ($companyIds) {
$sub->select('user_id')->from('company_user')->whereIn('company_id', $companyIds);
- })->orWhereNotIn('users.id', function ($sub) {
- $sub->select('user_id')->from('company_user');
- });
+ })->orWhereDoesntHave('companies');
});
}
@@ -467,6 +465,24 @@ final class Company extends SnipeModel
}
}
+ /**
+ * Scope a users query to those belonging to the given company IDs, respecting floater mode.
+ *
+ * Extracted from controller-level inline logic so the same rule is enforced consistently
+ * everywhere users are filtered by a specific set of company IDs (e.g. select2 dropdowns).
+ */
+ public static function scopeUsersByCompanyIds($query, array $companyIds): mixed
+ {
+ if (Setting::getSettings()->null_company_is_floater) {
+ return $query->where(function ($q) use ($companyIds) {
+ $q->whereHas('companies', fn($q2) => $q2->whereIn('companies.id', $companyIds))
+ ->orWhereDoesntHave('companies');
+ });
+ }
+
+ return $query->whereHas('companies', fn($q) => $q->whereIn('companies.id', $companyIds));
+ }
+
/**
* I legit do not know what this method does, but we can't remove it (yet).
*