A free open source IT asset/license management system
FMCS: throw an error if companies don’t match, updated tests
snipeb0aa21bee701
diff --git a/app/Models/Company.php b/app/Models/Company.phpindex 3dae954365..9c7a48878b 100644--- a/app/Models/Company.php+++ b/app/Models/Company.php@@ -15,6 +15,7 @@ use Illuminate\Support\Facades\DB;use Illuminate\Support\Facades\Gate;use Illuminate\Support\Facades\Log;use Illuminate\Support\Facades\Schema;+use Illuminate\Validation\ValidationException;use Watson\Validating\ValidatingTrait;/**@@ -159,15 +160,22 @@ final class Company extends SnipeModel$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 the submitted value is a company the user actually belongs to, honour it.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.+ // A user with pivot memberships who submits a company they don't belong to is+ // attempting cross-tenant assignment — reject outright rather than silently+ // overriding or storing null.+ if ($submittedId && ! empty($userCompanyIds)) {+ throw ValidationException::withMessages([+ 'company_id' => [trans('validation.in', ['attribute' => 'company_id'])],+ ]);+ }++ // No company submitted (or user has no pivot memberships) — fall back to the+ // user's single company if unambiguous, otherwise null.return count($userCompanyIds) === 1 ? $userCompanyIds[0] : null;}}diff --git a/tests/Feature/Accessories/Api/StoreAccessoryTest.php b/tests/Feature/Accessories/Api/StoreAccessoryTest.phpindex ac36b9e9a9..0345325f3b 100644--- a/tests/Feature/Accessories/Api/StoreAccessoryTest.php+++ b/tests/Feature/Accessories/Api/StoreAccessoryTest.php@@ -29,22 +29,16 @@ class StoreAccessoryTest extends TestCase implements TestsFullMultipleCompaniesS$this->settings->enableMultipleFullCompanySupport();- // attempt to store an accessory for company B- $response = $this->actingAsForApi($userInCompanyA)+ // A user in company A cannot create an accessory assigned to company B — request is rejected.+ $this->actingAsForApi($userInCompanyA)->postJson(route('api.accessories.store'), ['category_id' => Category::factory()->forAccessories()->create()->id,'name' => 'My Awesome Accessory','qty' => 1,'company_id' => $companyB->id,- ])->assertStatusMessageIs('success');-- $accessory = Accessory::withoutGlobalScopes()->findOrFail($response['payload']['id']);+ ])->assertStatusMessageIs('error');- $this->assertSame($companyA->id, $accessory->company_id);- $this->assertDatabaseMissing('accessories', [- 'name' => 'My Awesome Accessory',- 'company_id' => $companyB->id,- ]);+ $this->assertDatabaseMissing('accessories', ['name' => 'My Awesome Accessory']);}public function test_can_store_accessory()diff --git a/tests/Feature/Accessories/Api/UpdateAccessoryTest.php b/tests/Feature/Accessories/Api/UpdateAccessoryTest.phpindex 1e1a13b337..615f7e5218 100644--- a/tests/Feature/Accessories/Api/UpdateAccessoryTest.php+++ b/tests/Feature/Accessories/Api/UpdateAccessoryTest.php@@ -68,7 +68,7 @@ class UpdateAccessoryTest extends TestCase implements TestsFullMultipleCompanies->patchJson(route('api.accessories.update', $accessory), ['company_id' => $companyB->id,])- ->assertStatusMessageIs('success');+ ->assertStatusMessageIs('error');$this->assertSame($companyA->id, $accessory->fresh()->company_id);}diff --git a/tests/Support/ProvidesDataForFullMultipleCompanySupportTesting.php b/tests/Support/ProvidesDataForFullMultipleCompanySupportTesting.phpindex e76204a72c..1a4ffd05a9 100644--- a/tests/Support/ProvidesDataForFullMultipleCompanySupportTesting.php+++ b/tests/Support/ProvidesDataForFullMultipleCompanySupportTesting.php@@ -13,7 +13,6 @@ trait ProvidesDataForFullMultipleCompanySupportTestingyield "User in a company should result in user's company_id being used" => [function () {$jedi = Company::factory()->create();- $sith = Company::factory()->create();$luke = User::factory()->for($jedi)->createAccessories()->createAssets()@@ -24,10 +23,9 @@ trait ProvidesDataForFullMultipleCompanySupportTestingreturn ['actor' => $luke,- 'company_attempting_to_associate' => $sith,+ 'company_attempting_to_associate' => $jedi,'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.+ // Luke submits his own company (Jedi) — it is in his pivot so it is honoured.self::assertEquals($jedi->id, $model->company_id);},];diff --git a/tests/Unit/Models/Company/GetIdForCurrentUserTest.php b/tests/Unit/Models/Company/GetIdForCurrentUserTest.phpindex 36acd04287..d3efcf6813 100644--- a/tests/Unit/Models/Company/GetIdForCurrentUserTest.php+++ b/tests/Unit/Models/Company/GetIdForCurrentUserTest.php@@ -4,6 +4,7 @@ namespace Tests\Unit\Models\Company;use App\Models\Company;use App\Models\User;+use Illuminate\Validation\ValidationException;use Tests\TestCase;class GetIdForCurrentUserTest extends TestCase@@ -24,12 +25,13 @@ class GetIdForCurrentUserTest extends TestCase$this->assertEquals(2000, Company::getIdForCurrentUser(2000));}- public function test_returns_non_super_users_company_id_when_full_company_support_enabled()+ public function test_throws_when_non_super_user_submits_company_they_do_not_belong_to(){$this->settings->enableMultipleFullCompanySupport();$this->actingAs(User::factory()->forCompany(['id' => 2000])->create());- $this->assertEquals(2000, Company::getIdForCurrentUser(1000));+ $this->expectException(ValidationException::class);+ Company::getIdForCurrentUser(1000);}public function test_returns_null_for_non_super_user_without_company_id_when_full_company_support_enabled()