Skip to content

A free open source IT asset/license management system

Fixed #19979 - Companies and Categories search on dashboard

snipebac9abc4205e
diff --git a/app/Http/Controllers/Api/DashboardController.php b/app/Http/Controllers/Api/DashboardController.php
index e81306dff8..4801fe1fb2 100644
--- a/app/Http/Controllers/Api/DashboardController.php
+++ b/app/Http/Controllers/Api/DashboardController.php
@@ -171,6 +171,10 @@ class DashboardController extends Controller
$query->whereIn('category_type', $viewableTypeKeys);
}
+ if ($request->filled('search')) {
+ $query->TextSearch($request->input('search'));
+ }
+
$this->applyBoundedSort($query, $request, array_merge(['name', 'category_type'], $countSortColumns), 'name');
// category_type value → withCount alias. English pluralization
@@ -250,6 +254,10 @@ class DashboardController extends Controller
$countSortColumns[] = 'users_count';
}
+ if ($request->filled('search')) {
+ $query->TextSearch($request->input('search'));
+ }
+
$this->applyBoundedSort($query, $request, array_merge(['name'], $countSortColumns), 'name');
return response()->json($this->paginateRows($query, $request, function ($company) {
@@ -314,6 +322,10 @@ class DashboardController extends Controller
$countSortColumns[] = 'users_count';
}
+ if ($request->filled('search')) {
+ $query->TextSearch($request->input('search'));
+ }
+
$this->applyBoundedSort($query, $request, array_merge(['name'], $countSortColumns), 'name');
return response()->json($this->paginateRows($query, $request, function ($location) {
diff --git a/app/Models/AccessoryCheckout.php b/app/Models/AccessoryCheckout.php
index f5df682378..586d175348 100755
--- a/app/Models/AccessoryCheckout.php
+++ b/app/Models/AccessoryCheckout.php
@@ -16,6 +16,7 @@ use Illuminate\Database\Eloquent\Relations\Relation;
* Model for Accessories.
*
* @version v1.0
+ *
* @mixin IdeHelperAccessoryCheckout
*/
class AccessoryCheckout extends Model
@@ -143,11 +144,9 @@ class AccessoryCheckout extends Model
* Run additional, advanced searches.
*
* @param array $terms The search terms
- * @return Builder
*/
- public function advancedTextSearch(Builder $query, array $terms)
+ public function advancedTextSearch(Builder $outer, Builder $nested, array $terms): void
{
-
$userQuery = User::where(
function ($query) use ($terms) {
foreach ($terms as $term) {
@@ -178,30 +177,28 @@ class AccessoryCheckout extends Model
}
)->select('id');
- $query->where(
- function ($query) use ($userQuery) {
- $query->where('assigned_type', User::class)
+ $nested->orWhere(
+ function ($q) use ($userQuery) {
+ $q->where('assigned_type', User::class)
->whereIn('assigned_to', $userQuery);
}
)->orWhere(
- function ($query) use ($locationQuery) {
- $query->where('assigned_type', Location::class)
+ function ($q) use ($locationQuery) {
+ $q->where('assigned_type', Location::class)
->whereIn('assigned_to', $locationQuery);
}
)->orWhere(
- function ($query) use ($assetQuery) {
- $query->where('assigned_type', Asset::class)
+ function ($q) use ($assetQuery) {
+ $q->where('assigned_type', Asset::class)
->whereIn('assigned_to', $assetQuery);
}
)->orWhere(
- function ($query) use ($terms) {
+ function ($q) use ($terms) {
foreach ($terms as $term) {
$search_str = '%'.$term.'%';
- $query->where('note', 'like', $search_str);
+ $q->where('note', 'like', $search_str);
}
}
);
-
- return $query;
}
}
diff --git a/app/Models/Actionlog.php b/app/Models/Actionlog.php
index 3eb82437a4..fe5936f071 100755
--- a/app/Models/Actionlog.php
+++ b/app/Models/Actionlog.php
@@ -23,6 +23,7 @@ use Illuminate\Support\Str;
* checkouts, checkins, and updates).
*
* @version v1.0
+ *
* @mixin IdeHelperActionlog
*/
class Actionlog extends SnipeModel
@@ -463,7 +464,7 @@ class Actionlog extends SnipeModel
* the same three assignee morph types (User, Asset, Location)
* and LIKE across each type's identifier columns.
*/
- public function advancedTextSearch(Builder $query, array $terms)
+ public function advancedTextSearch(Builder $outer, Builder $nested, array $terms): void
{
// log_meta is a JSON diff blob that admins legitimately search
// across (who changed status to Lost, when the seat count moved),
@@ -473,7 +474,7 @@ class Actionlog extends SnipeModel
// a timing oracle on the masked serial. Scope the LIKE to
// item_type != License to keep the admin workflows while shutting
// the oracle. GHSA-68gh-qwvc-qccg.
- $query->orWhere(function (Builder $metaQuery) use ($terms): void {
+ $nested->orWhere(function (Builder $metaQuery) use ($terms): void {
$metaQuery->where('action_logs.item_type', '!=', License::class)
->where(function (Builder $likeQuery) use ($terms): void {
foreach ($terms as $term) {
@@ -482,7 +483,7 @@ class Actionlog extends SnipeModel
});
});
- return $query->orWhereHasMorph(
+ $nested->orWhereHasMorph(
'target',
[User::class, Asset::class, Location::class],
function (Builder $targetQuery, string $targetType) use ($terms) {
diff --git a/app/Models/Asset.php b/app/Models/Asset.php
index 00fd3c0e9e..7149d936be 100644
--- a/app/Models/Asset.php
+++ b/app/Models/Asset.php
@@ -961,7 +961,6 @@ class Asset extends Depreciable implements Importable
* @author [A. Gianotto] [<[email protected]>]
*
* @since [v4.0]
- *
*/
public function assetLoc($iterations = 1, $first_asset = null): ?Location
{
@@ -1599,15 +1598,20 @@ class Asset extends Depreciable implements Importable
* on the Searchable trait to also search assigned user, location,
* and asset-to-asset checkouts.
*
+ * $outer carries the leftJoins (joins cannot live inside Eloquent
+ * nested-where closures). $nested carries the orWhere clauses
+ * that reference the joined columns, confining them inside the
+ * trait's scope AND group so a multi-term search cannot escape
+ * the surrounding FMCS CompanyableScope.
+ *
* @param array<int, string> $terms The search terms
*/
- public function advancedTextSearch(Builder $query, array $terms)
+ public function advancedTextSearch(Builder $outer, Builder $nested, array $terms): void
{
-
/**
* Assigned user
*/
- $query = $query->leftJoin(
+ $outer->leftJoin(
'users as assets_users', function ($leftJoin) {
$leftJoin->on('assets_users.id', '=', 'assets.assigned_to')
->where('assets.assigned_type', '=', User::class);
@@ -1615,8 +1619,7 @@ class Asset extends Depreciable implements Importable
);
foreach ($terms as $term) {
-
- $query = $query
+ $nested
->orWhere('assets_users.first_name', 'LIKE', '%'.$term.'%')
->orWhere('assets_users.last_name', 'LIKE', '%'.$term.'%')
->orWhere('assets_users.display_name', 'LIKE', '%'.$term.'%')
@@ -1640,7 +1643,7 @@ class Asset extends Depreciable implements Importable
/**
* Assigned location
*/
- $query = $query->leftJoin(
+ $outer->leftJoin(
'locations as assets_locations', function ($leftJoin) {
$leftJoin->on('assets_locations.id', '=', 'assets.assigned_to')
->where('assets.assigned_type', '=', Location::class);
@@ -1648,14 +1651,13 @@ class Asset extends Depreciable implements Importable
);
foreach ($terms as $term) {
-
- $query = $query->orWhere('assets_locations.name', 'LIKE', '%'.$term.'%');
+ $nested->orWhere('assets_locations.name', 'LIKE', '%'.$term.'%');
}
/**
* Assigned assets
*/
- $query = $query->leftJoin(
+ $outer->leftJoin(
'assets as assigned_assets', function ($leftJoin) {
$leftJoin->on('assigned_assets.id', '=', 'assets.assigned_to')
->where('assets.assigned_type', '=', self::class);
@@ -1663,11 +1665,8 @@ class Asset extends Depreciable implements Importable
);
foreach ($terms as $term) {
- $query = $query->orWhere('assigned_assets.name', 'LIKE', '%'.$term.'%');
-
+ $nested->orWhere('assigned_assets.name', 'LIKE', '%'.$term.'%');
}
-
- return $query;
}
public function calendarEventCompanyId(): ?int
diff --git a/app/Models/CheckoutRequest.php b/app/Models/CheckoutRequest.php
index 1f6cc415a2..f2b11bdafd 100644
--- a/app/Models/CheckoutRequest.php
+++ b/app/Models/CheckoutRequest.php
@@ -16,6 +16,7 @@ use Illuminate\Support\Collection;
/**
* @property \App\Enums\CheckoutRequestState $state
+ *
* @mixin IdeHelperCheckoutRequest
*/
class CheckoutRequest extends Model
@@ -342,13 +343,11 @@ class CheckoutRequest extends Model
* other type has a direct category relation.
* - AssetModel has no company relation (asset models are
* shared across companies), so its branch skips company.
- *
- * @return Builder
*/
- public function advancedTextSearch(Builder $query, array $terms)
+ public function advancedTextSearch(Builder $outer, Builder $nested, array $terms): void
{
if (empty($terms)) {
- return $query;
+ return;
}
$morphTypes = [
@@ -360,7 +359,7 @@ class CheckoutRequest extends Model
License::class,
];
- return $query->orWhereHasMorph('requestable', $morphTypes, function (Builder $q, string $type) use ($terms): void {
+ $nested->orWhereHasMorph('requestable', $morphTypes, function (Builder $q, string $type) use ($terms): void {
$q->where(function (Builder $inner) use ($type, $terms): void {
foreach ($terms as $term) {
$like = '%'.$term.'%';
diff --git a/app/Models/Traits/Searchable.php b/app/Models/Traits/Searchable.php
index 9cba6849d4..9dd262394a 100644
--- a/app/Models/Traits/Searchable.php
+++ b/app/Models/Traits/Searchable.php
@@ -65,27 +65,45 @@ trait Searchable
return $this->applySearchFilters($query, $filters, $filterOperator);
}
- /**
- * Search the attributes of this model
- */
- $query = $this->searchAttributes($query, $terms);
+ if (empty($terms)) {
+ return $query;
+ }
- /**
- * Search through the custom fields of the model
- */
- $query = $this->searchCustomFields($query, $terms);
+ // Wrap the search methods in a nested where so the OR clauses
+ // they add stay confined inside one AND group with any
+ // surrounding scope (FMCS CompanyableScope, per-controller
+ // WHERE filters, etc). Without this nesting a multi-term
+ // search produces SQL shaped like
+ // WHERE scope AND col LIKE '%a%' OR col LIKE '%b%'
+ // which binds AND tighter than OR, letting the second term
+ // match rows outside the scope.
+ //
+ // advancedTextSearch overrides receive two builders: $outer
+ // for leftJoin calls (Eloquent nested-where closures cannot
+ // carry joins) and $nested for the orWhere clauses that
+ // reference the joined columns. The $nested parameter keeps
+ // the join-column where clauses inside the scope AND group.
+ return $query->where(function (Builder $nested) use ($query, $terms) {
+ /**
+ * Search the attributes of this model
+ */
+ $nested = $this->searchAttributes($nested, $terms);
- /**
- * Search through the relations of the model
- */
- $query = $this->searchRelations($query, $terms);
+ /**
+ * Search through the custom fields of the model
+ */
+ $nested = $this->searchCustomFields($nested, $terms);
- /**
- * Search for additional attributes defined by the model
- */
- $query = $this->advancedTextSearch($query, $terms);
+ /**
+ * Search through the relations of the model
+ */
+ $nested = $this->searchRelations($nested, $terms);
- return $query;
+ /**
+ * Search for additional attributes defined by the model
+ */
+ $this->advancedTextSearch($query, $nested, $terms);
+ });
}
/**
@@ -1066,13 +1084,20 @@ trait Searchable
* attributes or relations. No-op in this trait. Override in the
* implementing model to add relation-aware search terms.
*
+ * $outer is the top-level query builder, used for leftJoin calls
+ * (Eloquent nested-where closures cannot carry joins). $nested is
+ * the where-clause builder confined inside scopeTextSearch's
+ * nested group, used for orWhere calls that reference the joined
+ * columns. Keeping the orWhere clauses on $nested closes the
+ * multi-term oracle described on scopeTextSearch.
+ *
* @param array<int, string> $terms
*
* @SuppressWarnings("PHPMD.UnusedFormalParameter")
*/
- public function advancedTextSearch(Builder $query, array $terms): Builder
+ public function advancedTextSearch(Builder $outer, Builder $nested, array $terms): void
{
- return $query;
+ // No-op in the base trait.
}
/**
diff --git a/app/Models/User.php b/app/Models/User.php
index fcfecc2cdb..eb323bb40d 100644
--- a/app/Models/User.php
+++ b/app/Models/User.php
@@ -1785,10 +1785,10 @@ class User extends SnipeModel implements AuthenticatableContract, AuthorizableCo
*
* @param array<int, string> $terms The search terms
*/
- public function advancedTextSearch(Builder $query, array $terms)
+ public function advancedTextSearch(Builder $outer, Builder $nested, array $terms): void
{
foreach ($terms as $term) {
- $query->orWhereMultipleColumns(
+ $nested->orWhereMultipleColumns(
[
'users.first_name',
'users.last_name',
@@ -1800,8 +1800,6 @@ class User extends SnipeModel implements AuthenticatableContract, AuthorizableCo
], $term
);
}
-
- return $query;
}
/**
diff --git a/phpmd.baseline.xml b/phpmd.baseline.xml
index 978d90c5f0..2ee6cb629f 100644
--- a/phpmd.baseline.xml
+++ b/phpmd.baseline.xml
@@ -36,8 +36,11 @@
<violation rule="PHPMD\Rule\UnusedLocalVariable" file="app/Livewire/Importer.php"/>
<violation rule="PHPMD\Rule\UnusedFormalParameter" file="app/Livewire/LdapSettings.php"/>
<violation rule="PHPMD\Rule\UnusedFormalParameter" file="app/Models/Accessory.php"/>
+ <violation rule="PHPMD\Rule\UnusedFormalParameter" file="app/Models/AccessoryCheckout.php"/>
+ <violation rule="PHPMD\Rule\UnusedFormalParameter" file="app/Models/Actionlog.php"/>
<violation rule="PHPMD\Rule\UnusedFormalParameter" file="app/Models/Asset.php"/>
<violation rule="PHPMD\Rule\UnusedFormalParameter" file="app/Models/CheckoutAcceptance.php"/>
+ <violation rule="PHPMD\Rule\UnusedFormalParameter" file="app/Models/CheckoutRequest.php"/>
<violation rule="PHPMD\Rule\UnusedFormalParameter" file="app/Models/CompanyableChildScope.php"/>
<violation rule="PHPMD\Rule\UnusedFormalParameter" file="app/Models/CompanyableScope.php"/>
<violation rule="PHPMD\Rule\UnusedFormalParameter" file="app/Models/Component.php"/>
@@ -60,6 +63,7 @@
<violation rule="PHPMD\Rule\UnusedFormalParameter" file="app/Models/Traits/Searchable.php"/>
<violation rule="PHPMD\Rule\UnusedPrivateMethod" file="app/Models/User.php" method="revokeAssociatedPassportTokens"/>
<violation rule="PHPMD\Rule\UnusedPrivateMethod" file="app/Models/User.php" method="purgeAssociatedPassportTokens"/>
+ <violation rule="PHPMD\Rule\UnusedFormalParameter" file="app/Models/User.php"/>
<violation rule="PHPMD\Rule\UnusedFormalParameter" file="app/Notifications/AcceptanceItemAcceptedNotification.php"/>
<violation rule="PHPMD\Rule\UnusedFormalParameter" file="app/Notifications/AcceptanceItemDeclinedNotification.php"/>
<violation rule="PHPMD\Rule\UnusedLocalVariable" file="app/Notifications/CheckinAssetNotification.php"/>
diff --git a/tests/Feature/Dashboard/Api/CategoriesWidgetTest.php b/tests/Feature/Dashboard/Api/CategoriesWidgetTest.php
index 434f413b79..1a7d79af3f 100644
--- a/tests/Feature/Dashboard/Api/CategoriesWidgetTest.php
+++ b/tests/Feature/Dashboard/Api/CategoriesWidgetTest.php
@@ -85,8 +85,9 @@ class CategoriesWidgetTest extends TestCase
->assertOk();
}
- public function test_search_and_filter_params_are_ignored()
+ public function test_search_param_filters_rows_but_other_params_are_ignored()
{
+ // Issue #19979: `search` was ignored. Everything else still is.
Category::factory()->count(3)->create(['category_type' => 'accessory']);
Passport::actingAs(User::factory()->viewAccessories()->create());
@@ -94,16 +95,20 @@ class CategoriesWidgetTest extends TestCase
$unfilteredTotal = $this->getJson(route('api.dashboard.categories'))
->assertOk()
->json('total');
- $probeTotal = $this->getJson(route('api.dashboard.categories', [
- 'search' => 'never-matches-anything',
+
+ $searchHonoredTotal = $this->getJson(route('api.dashboard.categories', ['search' => 'never-matches-anything']))
+ ->assertOk()
+ ->json('total');
+ $this->assertSame(0, $searchHonoredTotal);
+
+ $extraParamsIgnoredTotal = $this->getJson(route('api.dashboard.categories', [
'filter' => 'never-matches-anything',
'name' => 'never-matches-anything',
'created_by' => 999_999,
]))
->assertOk()
->json('total');
-
- $this->assertSame($unfilteredTotal, $probeTotal);
+ $this->assertSame($unfilteredTotal, $extraParamsIgnoredTotal);
}
public function test_limit_caps_at_fifty()
diff --git a/tests/Feature/Dashboard/Api/CompaniesWidgetTest.php b/tests/Feature/Dashboard/Api/CompaniesWidgetTest.php
index 36103244d5..c1f5b21c0c 100644
--- a/tests/Feature/Dashboard/Api/CompaniesWidgetTest.php
+++ b/tests/Feature/Dashboard/Api/CompaniesWidgetTest.php
@@ -85,8 +85,12 @@ class CompaniesWidgetTest extends TestCase
->assertOk();
}
- public function test_search_and_filter_params_are_ignored()
+ public function test_search_param_filters_rows_but_other_params_are_ignored()
{
+ // Issue #19979: `search` was ignored, which broke the dashboard
+ // filter field. Everything else still is ignored (the index
+ // controller handles per-column filters, the dashboard widget
+ // only wires the free-text search field).
Company::factory()->count(3)->create();
Passport::actingAs(User::factory()->viewAccessories()->create());
@@ -94,8 +98,13 @@ class CompaniesWidgetTest extends TestCase
$unfilteredTotal = $this->getJson(route('api.dashboard.companies'))
->assertOk()
->json('total');
- $probeTotal = $this->getJson(route('api.dashboard.companies', [
- 'search' => 'never-matches',
+
+ $searchHonoredTotal = $this->getJson(route('api.dashboard.companies', ['search' => 'never-matches-anything']))
+ ->assertOk()
+ ->json('total');
+ $this->assertSame(0, $searchHonoredTotal, 'search param must filter the widget');
+
+ $extraParamsIgnoredTotal = $this->getJson(route('api.dashboard.companies', [
'filter' => 'never-matches',
'name' => 'never-matches',
'email' => 'never-matches',
@@ -104,7 +113,6 @@ class CompaniesWidgetTest extends TestCase
]))
->assertOk()
->json('total');
-
- $this->assertSame($unfilteredTotal, $probeTotal);
+ $this->assertSame($unfilteredTotal, $extraParamsIgnoredTotal, 'non-search filter params are not wired on the widget');
}
}
diff --git a/tests/Feature/Dashboard/Api/LocationsWidgetTest.php b/tests/Feature/Dashboard/Api/LocationsWidgetTest.php
index aa0ab8605e..7bc9dd4c46 100644
--- a/tests/Feature/Dashboard/Api/LocationsWidgetTest.php
+++ b/tests/Feature/Dashboard/Api/LocationsWidgetTest.php
@@ -83,8 +83,9 @@ class LocationsWidgetTest extends TestCase
->assertOk();
}
- public function test_search_and_filter_params_are_ignored()
+ public function test_search_param_filters_rows_but_other_params_are_ignored()
{
+ // Issue #19979: `search` was ignored. Everything else still is.
Location::factory()->count(3)->create();
Passport::actingAs(User::factory()->viewAccessories()->create());
@@ -92,8 +93,13 @@ class LocationsWidgetTest extends TestCase
$unfilteredTotal = $this->getJson(route('api.dashboard.locations'))
->assertOk()
->json('total');
- $probeTotal = $this->getJson(route('api.dashboard.locations', [
- 'search' => 'never-matches',
+
+ $searchHonoredTotal = $this->getJson(route('api.dashboard.locations', ['search' => 'never-matches']))
+ ->assertOk()
+ ->json('total');
+ $this->assertSame(0, $searchHonoredTotal);
+
+ $extraParamsIgnoredTotal = $this->getJson(route('api.dashboard.locations', [
'filter' => 'never-matches',
'name' => 'never-matches',
'city' => 'never-matches',
@@ -101,7 +107,6 @@ class LocationsWidgetTest extends TestCase
]))
->assertOk()
->json('total');
-
- $this->assertSame($unfilteredTotal, $probeTotal);
+ $this->assertSame($unfilteredTotal, $extraParamsIgnoredTotal);
}
}
diff --git a/tests/Feature/DashboardWidgetPayloadShapeTest.php b/tests/Feature/DashboardWidgetPayloadShapeTest.php
index 03ae9fbc8d..313b805944 100644
--- a/tests/Feature/DashboardWidgetPayloadShapeTest.php
+++ b/tests/Feature/DashboardWidgetPayloadShapeTest.php
@@ -146,4 +146,53 @@ class DashboardWidgetPayloadShapeTest extends TestCase
$this->assertTrue($locationRow['available_actions']['view']);
$this->assertTrue($companyRow['available_actions']['view']);
}
+
+ public function test_dashboard_categories_endpoint_filters_rows_by_search_param()
+ {
+ // Issue #19979: the bs-table filter field on the dashboard widget sends `search` to the server. Pre-fix the controller ignored it and returned every row, which the UI rendered as a stale/no-match state.
+ Category::factory()->create(['name' => 'MATCH_ME_FILTER', 'category_type' => 'asset']);
+ Category::factory()->create(['name' => 'OTHER_CATEGORY', 'category_type' => 'asset']);
+
+ Passport::actingAs(User::factory()->superuser()->create());
+
+ $rows = $this->getJson(route('api.dashboard.categories', ['search' => 'MATCH_ME_FILTER']))
+ ->assertOk()
+ ->json('rows');
+
+ $names = collect($rows)->pluck('name')->all();
+ $this->assertContains('MATCH_ME_FILTER', $names);
+ $this->assertNotContains('OTHER_CATEGORY', $names);
+ }
+
+ public function test_dashboard_companies_endpoint_filters_rows_by_search_param()
+ {
+ Company::factory()->create(['name' => 'MATCH_ME_FILTER']);
+ Company::factory()->create(['name' => 'OTHER_COMPANY']);
+
+ Passport::actingAs(User::factory()->superuser()->create());
+
+ $rows = $this->getJson(route('api.dashboard.companies', ['search' => 'MATCH_ME_FILTER']))
+ ->assertOk()
+ ->json('rows');
+
+ $names = collect($rows)->pluck('name')->all();
+ $this->assertContains('MATCH_ME_FILTER', $names);
+ $this->assertNotContains('OTHER_COMPANY', $names);
+ }
+
+ public function test_dashboard_locations_endpoint_filters_rows_by_search_param()
+ {
+ Location::factory()->create(['name' => 'MATCH_ME_FILTER']);
+ Location::factory()->create(['name' => 'OTHER_LOCATION']);
+
+ Passport::actingAs(User::factory()->superuser()->create());
+
+ $rows = $this->getJson(route('api.dashboard.locations', ['search' => 'MATCH_ME_FILTER']))
+ ->assertOk()
+ ->json('rows');
+
+ $names = collect($rows)->pluck('name')->all();
+ $this->assertContains('MATCH_ME_FILTER', $names);
+ $this->assertNotContains('OTHER_LOCATION', $names);
+ }
}
diff --git a/tests/Feature/Fmcs/HttpCrossCompanyLeakTest.php b/tests/Feature/Fmcs/HttpCrossCompanyLeakTest.php
index ffd184c3d8..495cc12648 100644
--- a/tests/Feature/Fmcs/HttpCrossCompanyLeakTest.php
+++ b/tests/Feature/Fmcs/HttpCrossCompanyLeakTest.php
@@ -170,6 +170,42 @@ class HttpCrossCompanyLeakTest extends TestCase
$this->assertCompanyBRowHidden(route('api.departments.index'), $companyBDepartment, $caller);
}
+ public function test_search_filter_does_not_bypass_scope_via_multi_term_or_clause(): void
+ {
+ // Pre-fix: the Searchable trait's scopeTextSearch stitched multi-term
+ // searches with top-level orWhere clauses. Operator precedence (AND
+ // binds tighter than OR) let the second term match rows outside the
+ // FMCS scope: `WHERE scope AND col LIKE '%a%' OR col LIKE '%b%'`
+ // meant the second LIKE escaped the scope filter. Any scoped caller
+ // using the search field could probe for the existence of cross-
+ // company rows by name. The fix wraps the free-text branch in a
+ // nested where so OR stays inside the AND group with the scope.
+ // Note: this covers searchAttributes / searchCustomFields /
+ // searchRelations. Models that override advancedTextSearch
+ // (Asset, User, Actionlog, AccessoryCheckout, CheckoutRequest)
+ // still add top-level joined-column orWhere clauses and keep a
+ // narrower residual oracle on multi-term advanced searches.
+ // Follow-up PR will refactor those overrides to split joins
+ // (outer builder) from where clauses (nested builder).
+ $companyBCompany = Company::factory()->create(['name' => 'SECRET_CROSS_COMPANY']);
+ $caller = $this->callerScopedToCompanyA(['companies']);
+
+ $this->settings->enableMultipleFullCompanySupport();
+
+ $ids = $this->idsFromResponse(
+ $this->actingAsForApi($caller)
+ ->getJson(route('api.companies.index', ['search' => 'nothing SECRET_CROSS_COMPANY']))
+ ->assertOk()
+ ->json('rows')
+ );
+
+ $this->assertNotContains(
+ $companyBCompany->id,
+ $ids,
+ 'Multi-term search must not leak cross-company rows via the OR clause',
+ );
+ }
+
public function test_api_users_index_hides_other_company_users(): void
{
$companyBUser = $this->companyB->users()->save(User::factory()->make());