Skip to content

A free open source IT asset/license management system

Exclude encrypted custom fields in search

snipe2c2de8719b5d
diff --git a/app/Models/Traits/Searchable.php b/app/Models/Traits/Searchable.php
index 0c43393dad..94f93b3956 100644
--- a/app/Models/Traits/Searchable.php
+++ b/app/Models/Traits/Searchable.php
@@ -269,7 +269,7 @@ trait Searchable
return $filterKey;
}
- // 2. Model-defined aliases — e.g. 'status_label' => 'assetstatus'.
+ // 2. Model-defined aliases — e.g. 'status_label' => 'status'.
$aliases = $this->getSearchableRelationAliases();
if (array_key_exists($filterKey, $aliases)) {
@@ -455,7 +455,8 @@ trait Searchable
return $query;
}
- $customFields = CustomField::all();
+ // Only pull unencrypted fields, since encrypted fields cannot be searched on
+ $customFields = CustomField::where('field_encrypted', 0)->get();
$firstConditionAdded = false;
foreach ($customFields as $field) {
@@ -650,7 +651,7 @@ trait Searchable
* Eloquent relation names used in $searchableRelations. For example:
*
* protected $searchableRelationAliases = [
- * 'status_label' => 'assetstatus',
+ * 'status_label' => 'status',
* ];
*
* Override this method in a model if you need dynamic alias resolution.
@@ -799,6 +800,7 @@ trait Searchable
try {
CustomField::query()
->whereNotNull('db_column')
+ ->where('field_encrypted', 0)
->get(['name', 'db_column'])
->each(function (CustomField $field) use (&$map): void {
$dbColumn = $field->db_column;
diff --git a/tests/Feature/Search/SearchableTraitTest.php b/tests/Feature/Search/SearchableTraitTest.php
index 1c6daf6cce..d861998658 100644
--- a/tests/Feature/Search/SearchableTraitTest.php
+++ b/tests/Feature/Search/SearchableTraitTest.php
@@ -23,7 +23,7 @@ use Tests\TestCase;
* Tests verify that:
* 1. Attributes are searchable via both modes
* 2. Relations are searchable via both modes
- * 3. Relation aliases (e.g., status_label → assetstatus) work correctly
+ * 3. Relation aliases (e.g., status_label → status) work correctly
* 4. Multi-word searches work as expected
*/
class SearchableTraitTest extends TestCase
@@ -147,7 +147,7 @@ class SearchableTraitTest extends TestCase
}
/**
- * Test Asset structured filter using relation alias (status_label → assetstatus)
+ * Test Asset structured filter using relation alias (status_label → status)
*/
public function test_asset_structured_filter_using_relation_alias()
{
@@ -157,7 +157,7 @@ class SearchableTraitTest extends TestCase
Asset::factory()->create(['status_id' => $status->id]);
Asset::factory()->create();
- // Filter using the API key 'status_label' should map to 'assetstatus' relation
+ // Filter using the API key 'status_label' should map to 'status' relation
$this->actingAsForApi(User::factory()->viewAssets()->create())
->getJson(route('api.assets.index', [
'filter' => json_encode(['status_label' => 'TestStatus']),
@@ -589,6 +589,28 @@ class SearchableTraitTest extends TestCase
->assertJson(fn (AssertableJson $json) => $json->has('rows', 1)->etc());
}
+ /**
+ * Test filtering on a custom field using the raw db_column slug.
+ */
+ public function test_custom_field_gets_skipped_if_encrypted()
+ {
+ $field = CustomField::factory()->testEncrypted()->create();
+ $dbColumn = $field->db_column_name();
+
+ Asset::factory()->create([$dbColumn => '3.2GHz i9']);
+ Asset::factory()->create([$dbColumn => '2.4GHz i5']);
+
+ Asset::flushCustomFieldFilterMap();
+
+ $this->actingAsForApi(User::factory()->viewAssets()->create())
+ ->getJson(route('api.assets.index', [
+ 'filter' => json_encode([$dbColumn => 'i9']),
+ ]))
+ ->assertOk()
+ // Encrypted fields are not searchable, so this filter key is ignored.
+ ->assertJson(fn (AssertableJson $json) => $json->has('rows', 2)->etc());
+ }
+
/**
* Test that custom field filter returns no results when value doesn't match.
*/
@@ -603,7 +625,7 @@ class SearchableTraitTest extends TestCase
$this->actingAsForApi(User::factory()->viewAssets()->create())
->getJson(route('api.assets.index', [
- 'filter' => json_encode(['CPU' => 'NonExistentCPU']),
+ 'filter' => json_encode([$dbColumn => 'NonExistentCPU']),
]))
->assertOk()
->assertJson(fn (AssertableJson $json) => $json->has('rows', 0)->etc());