A free open source IT asset/license management system
Fixed company quirk with multi-company users creating assets, etc
snipec44f3319e374
diff --git a/app/Models/Company.php b/app/Models/Company.phpindex 66e13a50cc..3dae954365 100644--- a/app/Models/Company.php+++ b/app/Models/Company.php@@ -155,11 +155,20 @@ final class Company extends SnipeModelif ($current_user->isSuperUser()) {return self::getIdFromInput($unescaped_input);} else {- if ($current_user->company_id != null) {- return $current_user->company_id;- } else {- return null;+ $userCompanyIds = self::getCurrentUserCompanyIds();+ $submittedId = (int) self::getIdFromInput($unescaped_input);++ // Company membership is now determined entirely by the pivot (company_user table).+ // If the submitted value is a company the user actually belongs to, honour it —+ // this is the normal case and also fixes pivot-only users (scalar company_id = null).+ if ($submittedId && in_array($submittedId, $userCompanyIds)) {+ return $submittedId;}++ // The submitted value was not in the user's pivot. When the user belongs to exactly+ // one company the choice is unambiguous: fall back to that company. When they belong+ // to multiple companies we cannot safely pick one, so return null.+ return count($userCompanyIds) === 1 ? $userCompanyIds[0] : null;}}}diff --git a/tests/Feature/Assets/Ui/StoreAssetWithFullMultipleCompanySupportTest.php b/tests/Feature/Assets/Ui/StoreAssetWithFullMultipleCompanySupportTest.phpindex c9a2cc8e39..c2677eab60 100644--- a/tests/Feature/Assets/Ui/StoreAssetWithFullMultipleCompanySupportTest.php+++ b/tests/Feature/Assets/Ui/StoreAssetWithFullMultipleCompanySupportTest.php@@ -36,6 +36,40 @@ class StoreAssetWithFullMultipleCompanySupportTest extends TestCase$assertions($asset);}+ /**+ * Non-admin whose scalar company_id is null but has a pivot membership should be able to create+ * an asset in their company. Previously getIdForCurrentUser() returned null (scalar), causing+ * the asset to be saved with company_id = null and then failing FMCS scoping on the redirect.+ */+ public function test_non_admin_with_pivot_only_company_membership_can_create_asset_in_their_company()+ {+ $this->settings->enableScopedLocationsWithFullMultipleCompanySupport();++ $company = Company::factory()->create();+ $location = Location::factory()->for($company)->create();++ // Create a user with NULL scalar company_id but a pivot membership in $company.+ // This mirrors users created/updated via the web UI, which writes only to the pivot.+ $user = User::factory()->createAssets()->create(['company_id' => null]);+ $user->companies()->sync([$company->id]);++ $this->actingAs($user)+ ->post(route('hardware.store'), [+ 'asset_tags' => ['1' => '5678'],+ 'serials' => ['1' => null],+ 'model_id' => AssetModel::factory()->create()->id,+ 'status_id' => Statuslabel::factory()->readyToDeploy()->create()->id,+ 'company_id' => $company->id,+ 'rtd_location_id' => $location->id,+ 'redirect_option' => 'back',+ ])->assertSessionHasNoErrors();++ $asset = Asset::withoutGlobalScopes()->where('asset_tag', '5678')->first();++ $this->assertNotNull($asset, 'Asset was not created.');+ $this->assertEquals($company->id, $asset->company_id, 'Asset should be assigned to the submitted company, not null.');+ }+/*** @link https://github.com/grokability/snipe-it/issues/18798*/diff --git a/tests/Support/ProvidesDataForFullMultipleCompanySupportTesting.php b/tests/Support/ProvidesDataForFullMultipleCompanySupportTesting.phpindex 75e8a5bc92..e76204a72c 100644--- a/tests/Support/ProvidesDataForFullMultipleCompanySupportTesting.php+++ b/tests/Support/ProvidesDataForFullMultipleCompanySupportTesting.php@@ -26,6 +26,8 @@ trait ProvidesDataForFullMultipleCompanySupportTesting'actor' => $luke,'company_attempting_to_associate' => $sith,'assertions' => function ($model) use ($jedi) {+ // Sith is not in Luke's pivot (he belongs only to Jedi), so the+ // submitted company is rejected and his single pivot company is used.self::assertEquals($jedi->id, $model->company_id);},];