Skip to content

A free open source IT asset/license management system

Updated user request for PATCH

snipe37238625e506
diff --git a/app/Http/Requests/UpdateAssetRequest.php b/app/Http/Requests/UpdateAssetRequest.php
index 5f5e98a199..19587ac4f5 100644
--- a/app/Http/Requests/UpdateAssetRequest.php
+++ b/app/Http/Requests/UpdateAssetRequest.php
@@ -48,6 +48,23 @@ class UpdateAssetRequest extends ImageUploadRequest
// route through checkOut() and produce the required audit-log entry).
unset($assetRules['assigned_to'], $assetRules['assigned_type']);
+ // Strip fmcs_company from the request-level company_id rule. It's an
+ // implicit rule (fires on null/absent) which is right for the
+ // model-level ValidatingTrait check on save, but wrong at the
+ // request-level PATCH context — omitting company_id means "don't
+ // change it," not "set to null." The model-level rule still
+ // catches a genuine null-value save. StoreAssetRequest and
+ // BulkUpdateAssetsRequest inherit from this indirectly via
+ // parent classes; the store path keeps the rule (create should
+ // require Company), the bulk update path is already covered by
+ // this strip since it extends UpdateAssetRequest.
+ if (isset($assetRules['company_id']) && is_array($assetRules['company_id'])) {
+ $assetRules['company_id'] = array_values(array_filter(
+ $assetRules['company_id'],
+ fn ($rule) => $rule !== 'fmcs_company',
+ ));
+ }
+
// On the singular endpoint we can tell Rule::unique to ignore the
// asset being updated. Bulk (BulkUpdateAssetsRequest) overrides this
// to null because it would have to ignore N different ids at once,
diff --git a/app/Providers/ValidationServiceProvider.php b/app/Providers/ValidationServiceProvider.php
index 7d7b5abc9e..3926e68a0e 100644
--- a/app/Providers/ValidationServiceProvider.php
+++ b/app/Providers/ValidationServiceProvider.php
@@ -472,7 +472,12 @@ class ValidationServiceProvider extends ServiceProvider
// for them. Forcing them to pick a non-null company would
// both lock them out of their normal workflow and produce a
// row they wouldn't be able to see afterward.
- Validator::extend('fmcs_company', function ($attribute, $value, $parameters, $validator) {
+ // extendImplicit (not extend) — Laravel skips "explicit" rules
+ // when the field is null/absent. Since the whole point of
+ // fmcs_company is to fire ON blank submissions, it must run
+ // implicitly. Same reason built-in rules like `required`,
+ // `filled`, `present`, `accepted` are all registered implicit.
+ Validator::extendImplicit('fmcs_company', function ($attribute, $value, $parameters, $validator) {
$settings = Setting::getSettings();
if (! $settings->full_multiple_companies_support) {
return true;
diff --git a/tests/Feature/Fmcs/StrictModeRequiresCompanyOnCreateTest.php b/tests/Feature/Fmcs/StrictModeRequiresCompanyOnCreateTest.php
index 65574c26e0..8efb5fe5ec 100644
--- a/tests/Feature/Fmcs/StrictModeRequiresCompanyOnCreateTest.php
+++ b/tests/Feature/Fmcs/StrictModeRequiresCompanyOnCreateTest.php
@@ -204,6 +204,84 @@ class StrictModeRequiresCompanyOnCreateTest extends TestCase
$this->assertDatabaseMissing('users', ['username' => $username]);
}
+ // ------------------------------------------------------------------
+ // Bulk asset edit: same gate reaches through ValidatingTrait
+ // ------------------------------------------------------------------
+
+ public function test_bulk_asset_edit_clear_company_is_rejected_for_companied_non_superuser()
+ {
+ // BulkAssetsController::update() calls $asset->update($updateArray)
+ // per row; the model-level fmcs_company rule fires when
+ // company_id gets filled to null via the 'clear' bulk option.
+ // Locked in here so a future refactor of the bulk controller
+ // can't quietly bypass the gate.
+ $this->settings->enableMultipleFullCompanySupport();
+ $this->settings->disableFloaterMode();
+
+ $company = \App\Models\Company::factory()->create();
+ $actor = $company->users()->save(User::factory()->editAssets()->create());
+ $target = \App\Models\Asset::factory()->create(['company_id' => $company->id]);
+
+ $this->actingAs($actor)
+ ->post(route('hardware/bulksave'), [
+ 'ids' => [$target->id => '1'],
+ 'company_id' => 'clear',
+ 'bulk_actions' => 'edit',
+ ]);
+
+ // Row's company should NOT have been cleared.
+ $this->assertEquals($company->id, $target->fresh()->company_id);
+ }
+
+ public function test_bulk_asset_edit_clear_company_is_allowed_for_uncompanied_non_superuser()
+ {
+ // Uncompanied non-superusers work in the null pseudo-company
+ // namespace under strict mode. Bulk-clearing Company on rows
+ // they own is a legitimate operation for them and the gate
+ // steps aside.
+ $this->settings->enableMultipleFullCompanySupport();
+ $this->settings->disableFloaterMode();
+
+ $actor = User::factory()->withoutCompany()->editAssets()->create();
+ $target = \App\Models\Asset::factory()->create(['company_id' => null]);
+
+ $this->actingAs($actor)
+ ->post(route('hardware/bulksave'), [
+ 'ids' => [$target->id => '1'],
+ 'company_id' => 'clear',
+ 'bulk_actions' => 'edit',
+ ]);
+
+ $this->assertNull($target->fresh()->company_id);
+ }
+
+ public function test_bulk_asset_edit_unrelated_field_still_works_for_companied_non_superuser()
+ {
+ // If the bulk edit doesn't touch Company at all, ValidatingTrait
+ // sees the existing non-null company_id on each row and passes
+ // — this is the "make sure we haven't broken ordinary bulk
+ // edits" sanity guard.
+ $this->settings->enableMultipleFullCompanySupport();
+ $this->settings->disableFloaterMode();
+
+ $company = \App\Models\Company::factory()->create();
+ $actor = $company->users()->save(User::factory()->editAssets()->create());
+ $target = \App\Models\Asset::factory()->create([
+ 'company_id' => $company->id,
+ 'notes' => 'before',
+ ]);
+
+ $this->actingAs($actor)
+ ->post(route('hardware/bulksave'), [
+ 'ids' => [$target->id => '1'],
+ 'notes' => 'after',
+ 'bulk_actions' => 'edit',
+ ]);
+
+ $this->assertEquals('after', $target->fresh()->notes);
+ $this->assertEquals($company->id, $target->fresh()->company_id);
+ }
+
public function test_users_strict_fmcs_allows_empty_company_ids_for_superuser()
{
$this->settings->enableMultipleFullCompanySupport();