Skip to content

A free open source IT asset/license management system

Small fixes

snipe2ee97645e40b
diff --git a/app/Helpers/Helper.php b/app/Helpers/Helper.php
index 933f76e6b0..cb6374d633 100644
--- a/app/Helpers/Helper.php
+++ b/app/Helpers/Helper.php
@@ -1755,9 +1755,11 @@ class Helper
// Users belong to companies via the many-to-many pivot (company_user).
// canReceiveFromCompany() returns true only when the user's pivot
// contains the location's company, so !canReceiveFromCompany() is
- // the correct mismatch signal.
+ // the correct mismatch signal. Pass $location_company through as
+ // ?int — casting null to (int) would coerce it to 0 and miss the
+ // null-company branch inside the method.
if ($item instanceof User) {
- $isMismatch = ! $item->canReceiveFromCompany((int) $location_company);
+ $isMismatch = ! $item->canReceiveFromCompany($location_company === null ? null : (int) $location_company);
} elseif ($item->company_id == $location_company) {
$isMismatch = false;
} elseif (is_null($item->company_id) || is_null($location_company)) {
diff --git a/app/Models/User.php b/app/Models/User.php
index 8d75e733bc..9fa7c8b39f 100644
--- a/app/Models/User.php
+++ b/app/Models/User.php
@@ -656,24 +656,33 @@ class User extends SnipeModel implements AuthenticatableContract, AuthorizableCo
/**
* Returns whether an FMCS company check should allow this user to receive
- * an asset that belongs to the given company.
+ * an item that belongs to the given company (null = uncompanied item).
*
- * - If the user has no company associations at all: returns true (no restriction).
- * - If the user has associations: returns true only when $companyId is among them.
+ * Mirrors the rules in CompanyableTrait::canCheckoutTo so the User-target
+ * branch behaves the same as every other companyable target:
+ * - Both sides uncompanied → allowed (null↔null).
+ * - Item uncompanied, user companied → only when null_company_is_floater
+ * is on (floater mode treats null-company items as system-wide).
+ * - Item companied, user uncompanied → only when null_company_is_floater
+ * is on (floater users see everything).
+ * - Both sides companied → user's pivot must contain the item's company.
*/
- public function canReceiveFromCompany(int $companyId): bool
+ 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 (is_null($companyId)) {
+ if ((bool) Setting::getSettings()->null_company_is_floater) {
+ return true;
+ }
+
+ return $userCompanyIds->isEmpty();
+ }
+
if ($userCompanyIds->isEmpty()) {
return (bool) Setting::getSettings()->null_company_is_floater;
}
diff --git a/tests/Unit/Models/UserCanReceiveFromCompanyTest.php b/tests/Unit/Models/UserCanReceiveFromCompanyTest.php
new file mode 100644
index 0000000000..a2006174ef
--- /dev/null
+++ b/tests/Unit/Models/UserCanReceiveFromCompanyTest.php
@@ -0,0 +1,83 @@
+<?php
+
+namespace Tests\Unit\Models;
+
+use App\Models\Company;
+use App\Models\User;
+use Tests\TestCase;
+
+/**
+ * Pins User::canReceiveFromCompany() — the User-target branch of the FMCS
+ * checkout rules in CompanyableTrait::canCheckoutTo(). Originally this method
+ * returned true unconditionally for null-company items, which broke parity with
+ * the asset/location branch: non-floater mode should restrict null-company
+ * items to null-company users (flagged in PR review by @uberbrady on #19200).
+ */
+class UserCanReceiveFromCompanyTest extends TestCase
+{
+ public function test_uncompanied_item_to_uncompanied_user_is_always_allowed(): void
+ {
+ $this->settings->disableFloaterMode();
+ $user = User::factory()->create();
+ $user->companies()->sync([]);
+
+ $this->assertTrue($user->canReceiveFromCompany(null));
+ }
+
+ public function test_uncompanied_item_to_companied_user_is_blocked_without_floater_mode(): void
+ {
+ $this->settings->disableFloaterMode();
+ $company = Company::factory()->create();
+ $user = $company->users()->save(User::factory()->create());
+
+ $this->assertFalse($user->canReceiveFromCompany(null));
+ }
+
+ public function test_uncompanied_item_to_companied_user_is_allowed_with_floater_mode(): void
+ {
+ $this->settings->enableFloaterMode();
+ $company = Company::factory()->create();
+ $user = $company->users()->save(User::factory()->create());
+
+ $this->assertTrue($user->canReceiveFromCompany(null));
+ }
+
+ public function test_companied_item_to_uncompanied_user_is_blocked_without_floater_mode(): void
+ {
+ $this->settings->disableFloaterMode();
+ $company = Company::factory()->create();
+ $user = User::factory()->create();
+ $user->companies()->sync([]);
+
+ $this->assertFalse($user->canReceiveFromCompany($company->id));
+ }
+
+ public function test_companied_item_to_uncompanied_user_is_allowed_with_floater_mode(): void
+ {
+ $this->settings->enableFloaterMode();
+ $company = Company::factory()->create();
+ $user = User::factory()->create();
+ $user->companies()->sync([]);
+
+ $this->assertTrue($user->canReceiveFromCompany($company->id));
+ }
+
+ public function test_companied_item_to_matching_companied_user_is_allowed(): void
+ {
+ $this->settings->disableFloaterMode();
+ $company = Company::factory()->create();
+ $user = $company->users()->save(User::factory()->create());
+
+ $this->assertTrue($user->canReceiveFromCompany($company->id));
+ }
+
+ public function test_companied_item_to_non_matching_companied_user_is_blocked(): void
+ {
+ $this->settings->enableFloaterMode(); // even with floater mode on, a companied user is restricted to their own pivot
+ $companyA = Company::factory()->create();
+ $companyB = Company::factory()->create();
+ $user = $companyA->users()->save(User::factory()->create());
+
+ $this->assertFalse($user->canReceiveFromCompany($companyB->id));
+ }
+}