Skip to content

A free open source IT asset/license management system

Fixed custom field handling

snipe2ea64ad96b55
diff --git a/app/Http/Requests/Traits/MayContainCustomFields.php b/app/Http/Requests/Traits/MayContainCustomFields.php
index 2a781df29d..61d623be68 100644
--- a/app/Http/Requests/Traits/MayContainCustomFields.php
+++ b/app/Http/Requests/Traits/MayContainCustomFields.php
@@ -37,17 +37,36 @@ trait MayContainCustomFields
return str_starts_with($attributes, '_snipeit_');
});
- // if there are custom fields, find the ones that don't exist on the model's fieldset and add an error to the validator's error bag
- if (count($request_fields) > 0 && $validator->errors()->isEmpty()) {
- $request_fields->diff($asset_model?->fieldset?->fields?->pluck('db_column'))
- ->each(function ($request_field_name) use ($validator) {
- if (CustomField::where('db_column', $request_field_name)->exists()) {
- $validator->errors()->add($request_field_name, trans('validation.custom.custom_field_not_found_on_model'));
- } else {
- $validator->errors()->add($request_field_name, trans('validation.custom.custom_field_not_found'));
- }
- });
+ if ($request_fields->isEmpty() || ! $validator->errors()->isEmpty()) {
+ return;
}
+
+ // Bulk update path: no single target model to check membership
+ // against, since different assets in the batch may carry
+ // different fieldsets. We still flag truly nonexistent
+ // custom-field columns (typo catch); per-model membership is
+ // deferred to save-time, where applyAssetUpdate silently
+ // ignores fields the asset's own model doesn't carry.
+ if ($asset_model === null) {
+ $request_fields->each(function ($request_field_name) use ($validator) {
+ if (! CustomField::where('db_column', $request_field_name)->exists()) {
+ $validator->errors()->add($request_field_name, trans('validation.custom.custom_field_not_found'));
+ }
+ });
+
+ return;
+ }
+
+ // Single-target-model path: fields present in the request but
+ // not on this asset's fieldset are errors.
+ $request_fields->diff($asset_model->fieldset?->fields?->pluck('db_column'))
+ ->each(function ($request_field_name) use ($validator) {
+ if (CustomField::where('db_column', $request_field_name)->exists()) {
+ $validator->errors()->add($request_field_name, trans('validation.custom.custom_field_not_found_on_model'));
+ } else {
+ $validator->errors()->add($request_field_name, trans('validation.custom.custom_field_not_found'));
+ }
+ });
});
}
}
diff --git a/tests/Feature/Assets/Api/BulkAuditAssetsTest.php b/tests/Feature/Assets/Api/BulkAuditAssetsTest.php
index 07bc9c6384..6e29d559ef 100644
--- a/tests/Feature/Assets/Api/BulkAuditAssetsTest.php
+++ b/tests/Feature/Assets/Api/BulkAuditAssetsTest.php
@@ -170,6 +170,35 @@ class BulkAuditAssetsTest extends TestCase
}
}
+ public function test_superuser_bypasses_fmcs_and_audits_assets_in_any_company()
+ {
+ // FMCS on, but a superuser is exempt from CompanyableScope — they can
+ // see and audit assets in every company. A bulk request touching
+ // assets in two different companies should succeed on both rows.
+ $this->settings->enableMultipleFullCompanySupport();
+
+ $companyA = Company::factory()->create();
+ $companyB = Company::factory()->create();
+
+ $assetA = Asset::factory()->create(['company_id' => $companyA->id]);
+ $assetB = Asset::factory()->create(['company_id' => $companyB->id]);
+
+ $response = $this->actingAsForApi(User::factory()->superuser()->create())
+ ->postJson($this->bulkUrl(), [
+ 'ids' => [$assetA->id, $assetB->id],
+ 'note' => 'superuser bulk audit',
+ ])
+ ->assertOk()
+ ->assertJsonPath('status', 'success');
+
+ $rows = collect($response->json('results'))->keyBy('id');
+ $this->assertSame('success', $rows[$assetA->id]['status']);
+ $this->assertSame('success', $rows[$assetB->id]['status']);
+
+ $this->assertNotNull($assetA->fresh()->last_audit_date);
+ $this->assertNotNull($assetB->fresh()->last_audit_date);
+ }
+
public function test_respects_fmcs_scoping_for_non_superuser()
{
// FMCS on, caller in company A tries to audit both an A-owned asset
diff --git a/tests/Feature/Assets/Api/BulkUpdateAssetsTest.php b/tests/Feature/Assets/Api/BulkUpdateAssetsTest.php
index f7c242afd7..45e5aa2e5f 100644
--- a/tests/Feature/Assets/Api/BulkUpdateAssetsTest.php
+++ b/tests/Feature/Assets/Api/BulkUpdateAssetsTest.php
@@ -4,6 +4,7 @@ namespace Tests\Feature\Assets\Api;
use App\Models\Asset;
use App\Models\Company;
+use App\Models\CustomField;
use App\Models\Statuslabel;
use App\Models\User;
use Tests\TestCase;
@@ -172,6 +173,113 @@ class BulkUpdateAssetsTest extends TestCase
$this->assertSame([], $response->json('results'));
}
+ public function test_custom_field_can_be_updated_across_assets_sharing_a_model()
+ {
+ // Regression: pre-fix, MayContainCustomFields on the bulk path set
+ // $asset_model = null and flagged EVERY _snipeit_* field as not-on-model,
+ // failing request-level validation. Fix: on bulk, skip the per-model
+ // membership check and defer to per-row save behavior.
+ $this->markIncompleteIfMySQL('Custom Field Tests do not work in MySQL');
+
+ $customField = CustomField::factory()->create();
+ [$a, $b] = Asset::factory()->hasMultipleCustomFields([$customField])->count(2)->create();
+
+ $response = $this->actingAsForApi(User::factory()->editAssets()->create())
+ ->patchJson($this->bulkUrl(), [
+ 'ids' => [$a->id, $b->id],
+ $customField->db_column_name() => 'bulk custom',
+ ])
+ ->assertOk()
+ ->assertJsonPath('status', 'success');
+
+ foreach ([$a, $b] as $asset) {
+ $this->assertSame('bulk custom', $asset->fresh()->{$customField->db_column_name()});
+ }
+
+ $rows = collect($response->json('results'))->keyBy('id');
+ $this->assertSame('success', $rows[$a->id]['status']);
+ $this->assertSame('success', $rows[$b->id]['status']);
+ }
+
+ public function test_custom_field_absent_from_some_models_is_silently_ignored_on_those_rows()
+ {
+ // Half the batch carries the field, half doesn't. Rows whose model
+ // doesn't carry the field still succeed (their row is a no-op for
+ // that field); rows whose model does carry it get the write.
+ // Matches the per-row apply loop's "iterate the asset's own fieldset"
+ // behavior — a field the model doesn't know is never touched.
+ $this->markIncompleteIfMySQL('Custom Field Tests do not work in MySQL');
+
+ $customField = CustomField::factory()->create();
+ $withField = Asset::factory()->hasMultipleCustomFields([$customField])->create();
+ $withoutField = Asset::factory()->create();
+
+ $this->actingAsForApi(User::factory()->editAssets()->create())
+ ->patchJson($this->bulkUrl(), [
+ 'ids' => [$withField->id, $withoutField->id],
+ $customField->db_column_name() => 'partial write',
+ 'notes' => 'both should update notes',
+ ])
+ ->assertOk()
+ ->assertJsonPath('status', 'success');
+
+ $this->assertSame('partial write', $withField->fresh()->{$customField->db_column_name()});
+ // Notes still applied on the row that didn't carry the custom field.
+ $this->assertSame('both should update notes', $withoutField->fresh()->notes);
+ }
+
+ public function test_unknown_custom_field_column_is_still_a_request_level_validation_error()
+ {
+ // We drop the per-model membership check on bulk, but we still catch
+ // truly nonexistent custom-field columns (typo protection). Every
+ // targeted row gets the same error via the failedValidation reshape.
+ $this->markIncompleteIfMySQL('Custom Field Tests do not work in MySQL');
+
+ [$a, $b] = Asset::factory()->count(2)->create();
+
+ $response = $this->actingAsForApi(User::factory()->editAssets()->create())
+ ->patchJson($this->bulkUrl(), [
+ 'ids' => [$a->id, $b->id],
+ '_snipeit_totally_bogus_9999' => 'nope',
+ ])
+ ->assertJsonPath('status', 'error');
+
+ $rows = collect($response->json('results'))->keyBy('id');
+ foreach ([$a->id, $b->id] as $id) {
+ $this->assertSame('error', $rows[$id]['status']);
+ $this->assertArrayHasKey('_snipeit_totally_bogus_9999', $rows[$id]['messages']);
+ }
+ }
+
+ public function test_superuser_bypasses_fmcs_and_updates_assets_in_any_company()
+ {
+ // FMCS on, but a superuser is exempt from CompanyableScope — they can
+ // see and update assets in every company. A bulk request touching
+ // assets in two different companies should succeed on both rows.
+ $this->settings->enableMultipleFullCompanySupport();
+
+ $companyA = Company::factory()->create();
+ $companyB = Company::factory()->create();
+
+ $assetA = Asset::factory()->create(['company_id' => $companyA->id]);
+ $assetB = Asset::factory()->create(['company_id' => $companyB->id]);
+
+ $response = $this->actingAsForApi(User::factory()->superuser()->create())
+ ->patchJson($this->bulkUrl(), [
+ 'ids' => [$assetA->id, $assetB->id],
+ 'notes' => 'superuser bulk touch',
+ ])
+ ->assertOk()
+ ->assertJsonPath('status', 'success');
+
+ $rows = collect($response->json('results'))->keyBy('id');
+ $this->assertSame('success', $rows[$assetA->id]['status']);
+ $this->assertSame('success', $rows[$assetB->id]['status']);
+
+ $this->assertSame('superuser bulk touch', $assetA->fresh()->notes);
+ $this->assertSame('superuser bulk touch', $assetB->fresh()->notes);
+ }
+
public function test_respects_fmcs_scoping_for_non_superuser()
{
// Caller scoped to company A asking to update assets [A-owned, B-owned]