From 78c342eefd4b566bc248c95405e82821a874fcdd Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 19 Nov 2026 18:25:33 +0000 Subject: [PATCH 1/2] Search permission users through the user relation. The Permissions tab applied first_name, last_name, and email filters to library_item_permissions. Scope that search with whereHas('user') so a folder permissions search matches the user and no longer errors. Co-authored-by: Steve Williamson --- .../LibraryItemPermissionsRelationManager.php | 24 +-- ...emPermissionsRelationManagerSearchTest.php | 165 ++++++++++++++++++ tests/TestCase.php | 2 + 3 files changed, 181 insertions(+), 10 deletions(-) create mode 100644 tests/Feature/LibraryItemPermissionsRelationManagerSearchTest.php diff --git a/src/Resources/RelationManagers/LibraryItemPermissionsRelationManager.php b/src/Resources/RelationManagers/LibraryItemPermissionsRelationManager.php index afdc499..ac014b8 100644 --- a/src/Resources/RelationManagers/LibraryItemPermissionsRelationManager.php +++ b/src/Resources/RelationManagers/LibraryItemPermissionsRelationManager.php @@ -129,16 +129,20 @@ public function table(Table $table): Table Tables\Columns\TextColumn::make('user') ->label('User') ->formatStateUsing(fn ($record) => $this->getUserDisplayName($record->user)) - ->searchable(query: function ($query, $search) { - return $query->where(function ($query) use ($search): void { - if (SchemaFacade::hasColumn('users', 'first_name') && SchemaFacade::hasColumn('users', 'last_name')) { - $query->orWhere('first_name', 'like', "%{$search}%") - ->orWhere('last_name', 'like', "%{$search}%"); - } elseif (SchemaFacade::hasColumn('users', 'name')) { - $query->orWhere('name', 'like', "%{$search}%"); - } - - $query->orWhere('email', 'like', "%{$search}%"); + ->searchable(query: function ($query, string $search) { + // The callback receives the permissions query. Name and email + // columns live on the related user, not on library_item_permissions. + return $query->whereHas('user', function ($userQuery) use ($search): void { + $userQuery->where(function ($userQuery) use ($search): void { + if (SchemaFacade::hasColumn('users', 'first_name') && SchemaFacade::hasColumn('users', 'last_name')) { + $userQuery->orWhere('first_name', 'like', "%{$search}%") + ->orWhere('last_name', 'like', "%{$search}%"); + } elseif (SchemaFacade::hasColumn('users', 'name')) { + $userQuery->orWhere('name', 'like', "%{$search}%"); + } + + $userQuery->orWhere('email', 'like', "%{$search}%"); + }); }); }), diff --git a/tests/Feature/LibraryItemPermissionsRelationManagerSearchTest.php b/tests/Feature/LibraryItemPermissionsRelationManagerSearchTest.php new file mode 100644 index 0000000..ed2da70 --- /dev/null +++ b/tests/Feature/LibraryItemPermissionsRelationManagerSearchTest.php @@ -0,0 +1,165 @@ +string('first_name')->nullable(); + $table->string('last_name')->nullable(); + }); + + $owner = permissionsSearchUser('Owner', 'owner@example.com'); + $rachel = permissionsSearchUser('rnuser', 'rn@example.com', 'Rachel', 'Nguyen'); + $chris = permissionsSearchUser('causer', 'ca@example.com', 'Chris', 'Alvarez'); + $pat = permissionsSearchUser('pruser', 'pr@example.com', 'Pat', 'Rachelson'); + + $folder = permissionsSearchFolder($owner, 'Shared folder'); + $otherFolder = permissionsSearchFolder($owner, 'Other folder'); + + $rachelPermission = permissionsSearchGrant($folder, $rachel, 'viewer'); + $chrisPermission = permissionsSearchGrant($folder, $chris, 'editor'); + $patPermission = permissionsSearchGrant($folder, $pat, 'viewer'); + $otherFolderPermission = permissionsSearchGrant($otherFolder, $rachel, 'owner'); + + $component = permissionsSearchTable($owner, $folder); + + $component->searchTable('rachel'); + + $sql = str_replace('`', '"', $component->instance()->getFilteredTableQuery()->toSql()); + $usersSubquery = strpos($sql, 'from "users"'); + $firstNameConstraint = strpos($sql, '"first_name"'); + + expect($usersSubquery)->toBeInt() + ->and($firstNameConstraint)->toBeInt() + ->and($firstNameConstraint)->toBeGreaterThan($usersSubquery); + + $component + ->searchTable('rachel') + ->assertCanSeeTableRecords([$rachelPermission, $patPermission]) + ->assertCanNotSeeTableRecords([$chrisPermission, $otherFolderPermission]); + + $component + ->searchTable('Alvarez') + ->assertCanSeeTableRecords([$chrisPermission]) + ->assertCanNotSeeTableRecords([$rachelPermission, $patPermission, $otherFolderPermission]); + + $component + ->searchTable('rn@example.com') + ->assertCanSeeTableRecords([$rachelPermission]) + ->assertCanNotSeeTableRecords([$chrisPermission, $patPermission, $otherFolderPermission]); +}); + +test('permissions table search uses name and email when users have no first or last name columns', function (): void { + expect(Schema::hasColumn('users', 'first_name'))->toBeFalse() + ->and(Schema::hasColumn('users', 'last_name'))->toBeFalse() + ->and(Schema::hasColumn('users', 'name'))->toBeTrue(); + + $owner = permissionsSearchUser('Owner', 'owner-name-only@example.com'); + $rachel = permissionsSearchUser('Rachel Nguyen', 'rn-name@example.com'); + $chris = permissionsSearchUser('Chris Alvarez', 'ca-name@example.com'); + + $folder = permissionsSearchFolder($owner, 'Name only folder'); + $rachelPermission = permissionsSearchGrant($folder, $rachel, 'viewer'); + $chrisPermission = permissionsSearchGrant($folder, $chris, 'editor'); + + $component = permissionsSearchTable($owner, $folder); + + $component->searchTable('Rachel'); + + $sql = str_replace('`', '"', $component->instance()->getFilteredTableQuery()->toSql()); + $usersSubquery = strpos($sql, 'from "users"'); + $nameConstraint = strpos($sql, '"name"'); + + expect($usersSubquery)->toBeInt() + ->and($nameConstraint)->toBeInt() + ->and($nameConstraint)->toBeGreaterThan($usersSubquery); + + $component + ->searchTable('Rachel') + ->assertCanSeeTableRecords([$rachelPermission]) + ->assertCanNotSeeTableRecords([$chrisPermission]); + + $component + ->searchTable('rn-name@example.com') + ->assertCanSeeTableRecords([$rachelPermission]) + ->assertCanNotSeeTableRecords([$chrisPermission]); +}); + +function permissionsSearchTable(PermissionsRelationSearchUser $owner, LibraryItem $folder): Testable +{ + config()->set('filament-library.user_model', PermissionsRelationSearchUser::class); + config()->set('auth.providers.users.model', PermissionsRelationSearchUser::class); + FilamentLibraryPlugin::setLibraryAdminCallback(fn (): bool => true); + + $panel = Panel::make() + ->id('admin') + ->default(); + Filament::registerPanel($panel); + Filament::setCurrentPanel($panel); + + Auth::login($owner); + + return Livewire::actingAs($owner) + ->test(LibraryItemPermissionsRelationManager::class, [ + 'ownerRecord' => $folder, + 'pageClass' => EditFolder::class, + ]); +} + +function permissionsSearchFolder(PermissionsRelationSearchUser $owner, string $name): LibraryItem +{ + return LibraryItem::query()->create([ + 'name' => $name, + 'type' => 'folder', + 'created_by' => $owner->id, + 'updated_by' => $owner->id, + 'general_access' => 'private', + ]); +} + +function permissionsSearchGrant(LibraryItem $folder, PermissionsRelationSearchUser $user, string $role): LibraryItemPermission +{ + return $folder->permissions()->create([ + 'user_id' => $user->id, + 'role' => $role, + ]); +} + +function permissionsSearchUser(string $name, string $email, ?string $firstName = null, ?string $lastName = null): PermissionsRelationSearchUser +{ + $attributes = [ + 'name' => $name, + 'email' => $email, + ]; + + if ($firstName !== null) { + $attributes['first_name'] = $firstName; + $attributes['last_name'] = $lastName; + } + + return PermissionsRelationSearchUser::query()->create($attributes); +} + +class PermissionsRelationSearchUser extends Authenticatable +{ + protected $table = 'users'; + + protected $guarded = []; +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 691830b..8092ba3 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -67,6 +67,8 @@ protected function getPackageProviders($app) public function getEnvironmentSetUp($app) { + $app['config']->set('app.key', 'base64:' . base64_encode(str_repeat('a', 32))); + config()->set('database.default', 'testing'); // Create users table first (required by library migrations) From 131cf4d11ca1941cf090e34df5933a348518cdbc Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 19 Nov 2026 18:27:50 +0000 Subject: [PATCH 2/2] Run the style fixer on PHP 8.3 with mbstring. The Pint Docker action uses composer:latest, which is now PHP 8.5 without mbstring, so the job dies in Termwind after the files have already passed. Co-authored-by: Steve Williamson --- .github/workflows/fix-php-code-style-issues.yml | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/.github/workflows/fix-php-code-style-issues.yml b/.github/workflows/fix-php-code-style-issues.yml index 0645413..dc62ec9 100644 --- a/.github/workflows/fix-php-code-style-issues.yml +++ b/.github/workflows/fix-php-code-style-issues.yml @@ -4,6 +4,7 @@ on: push: paths: - "**.php" + - ".github/workflows/fix-php-code-style-issues.yml" permissions: contents: write @@ -18,8 +19,20 @@ jobs: with: ref: ${{ github.head_ref }} + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: "8.3" + extensions: mbstring + coverage: none + + # The Pint Docker action uses composer:latest. That image is PHP 8.5 + # without mbstring, and Pint's summary then dies on mb_strimwidth(). + - name: Install Pint + run: composer global require laravel/pint --no-interaction --no-progress + - name: Fix PHP code style issues - uses: aglipanci/laravel-pint-action@2.6 + run: "$(composer global config home)/vendor/bin/pint" - name: Commit changes uses: stefanzweifel/git-auto-commit-action@v7