filamentphp / filament

A collection of beautiful full-stack components for Laravel. The perfect starting point for your next app. Using Livewire, Alpine.js and Tailwind CSS.
https://filamentphp.com
MIT License
18.97k stars 2.93k forks source link

Policy view(User $user, Type $type) not being called #12472

Closed towerthepower89 closed 6 months ago

towerthepower89 commented 6 months ago

Package

filament/filament

Package Version

v3.2.71

Laravel Version

v10.48.9

Livewire Version

v3.4.10

PHP Version

PHP 8.1.0

Problem description

I generated a new policy file using php artisan make:policy TypePolicy --model=Type

and a new file TypePolicy.php has been generated. During the configuration, the public function view(User $user, Type $type): bool is not being called. Does it have a new name?

Expected behavior

Normally, it should apply a filter in the rows shown in the table, in this case it shows all the records regardless the settings true/false. By checking the logs, it seems the function is never called.

Steps to reproduce

php artisan make:model Type -m

php artisan make:filament-resource Type

and configure the resource as shown:

public static function table(Table $table): Table
{
    return $table
        ->columns([
            Tables\Columns\TextColumn::make('name')
                    ->searchable()
                    ->sortable(),
        ])
        ->filters([
            //
        ])
        ->actions([
            Tables\Actions\EditAction::make(),
        ])
        ->bulkActions([
            Tables\Actions\BulkActionGroup::make([
                Tables\Actions\DeleteBulkAction::make(),
            ]),
        ]);
}

php artisan make:policy TypePolicy --model=Type

public function view(User $user, Type $type): bool
{
    return false;
}

Reproduction repository

https://github.com/towerthepower89/filament-issue

Relevant log output

No response

ryangjchandler commented 6 months ago

Have you registered the policy? Laravel 10.x doesn't auto-register policies, that's a new feature in Laravel 11.x

dmitry-udod commented 6 months ago

Seems no https://github.com/towerthepower89/filament-issue/blob/main/app/Providers/AuthServiceProvider.php#L15-L17

towerthepower89 commented 6 months ago

hi, thank you, indeed it was missing. I updated the version with the policy registration, still the same issue. All the other policies are/were already working, so viewAny, create, delete, update: all of them are correctly applied, but view seems to be skipped.

dmitry-udod commented 6 months ago

@towerthepower89 If I understand you correctly you need to add update function to TypePolicy. Because you are trying to edit not view Type entity

https://github.com/filamentphp/filament/assets/4639175/8948b4cc-6867-44a7-aa7e-c8a342dd123f

towerthepower89 commented 6 months ago

My end goal would be to hide some rows based on the user group, for example removing RDX and ILX, or all. In the example attached I removed some methods, like delete and update, those have been tested and worked perfectly. From my understanding, by returning false from the view function, no rows should be displayed. Probably I am missing something, thank you for your help.

dmitry-udod commented 6 months ago

@towerthepower89 According to the Laravel documentation, the view function determines whether a user can view the model or not. It seems you are trying to filter the row list. I suggest you change your query. Try checking the Customizing the Table Eloquent Query'section and adjust your filter according to the user group.

As for me it's not a filament issue at all.

towerthepower89 commented 6 months ago

thank you @dmitry-udod , then we can close the thread :)