Skip to content

A free open source IT asset/license management system

FMCS: check for floater mode in user and company

snipeab1a5c02417b
diff --git a/app/Models/Company.php b/app/Models/Company.php
index 9c7a48878b..2b2c01da2e 100644
--- a/app/Models/Company.php
+++ b/app/Models/Company.php
@@ -398,10 +398,17 @@ final class Company extends SnipeModel
return $query->whereIn('companies.id', $companyIds);
}
+ $floater = Setting::getSettings()->null_company_is_floater;
+
// Users are scoped by pivot membership (company_user), not by company_id column,
// since a user may belong to multiple companies and company_id alone is insufficient.
if ($query->getModel()->getTable() == 'users') {
if (empty($companyIds)) {
+ // Floater: actor has no company and is unrestricted — see everyone.
+ if ($floater) {
+ return $query;
+ }
+
// No pivot memberships: mirror old null-company behavior — show only users
// who are also not in any company via the pivot.
return $query->whereNotIn('users.id', function ($sub) {
@@ -409,6 +416,17 @@ final class Company extends SnipeModel
});
}
+ // Floater: also include users with no company associations (they float).
+ 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');
+ });
+ });
+ }
+
return $query->whereIn('users.id', function ($sub) use ($companyIds) {
$sub->select('user_id')->from('company_user')->whereIn('company_id', $companyIds);
});
@@ -419,6 +437,11 @@ final class Company extends SnipeModel
$table = ($table_name) ? $table_name.'.' : $query->getModel()->getTable().'.';
if (empty($companyIds)) {
+ // Floater: actor has no company and is unrestricted — see everything.
+ if ($floater) {
+ return $query;
+ }
+
return $query->whereNull($table.$column);
}
@@ -432,6 +455,14 @@ final class Company extends SnipeModel
});
}
+ // Floater: null-company items are visible to users from any company.
+ if ($floater) {
+ return $query->where(function ($q) use ($table, $column, $companyIds) {
+ $q->whereIn($table.$column, $companyIds)
+ ->orWhereNull($table.$column);
+ });
+ }
+
return $query->whereIn($table.$column, $companyIds);
}
}
diff --git a/app/Models/User.php b/app/Models/User.php
index 61a4714369..e43f45cc23 100644
--- a/app/Models/User.php
+++ b/app/Models/User.php
@@ -634,22 +634,22 @@ class User extends SnipeModel implements AuthenticatableContract, AuthorizableCo
*/
public function canReceiveFromCompany(int $companyId): bool
{
+ // Items with no company association are unrestricted — anyone can receive them.
+ if (! $companyId) {
+ return true;
+ }
+
// Query the pivot directly to avoid the Company model's FMCS global scope,
// which would restrict results to the current actor's visible companies.
$userCompanyIds = DB::table('company_user')
->where('user_id', $this->id)
->pluck('company_id');
- if ($userCompanyIds->contains($companyId)) {
- return true;
- }
-
- // User has no company associations — don't enforce.
if ($userCompanyIds->isEmpty()) {
- return true;
+ return (bool) Setting::getSettings()->null_company_is_floater;
}
- return false;
+ return $userCompanyIds->contains($companyId);
}
/**