How to use a custom policy for a custom resource page or disable authorization entirely?

Is it possible to use a custom policy for a custom resource page? Lets say I have a page to download files from an existing resource. I want to use a custom policy download for this resource. How can I manage access to the page? It seems like the model policies take precedence over the canAccess()-method of the page. So the "normal" model policies deny access to the custom resource download page, because I can't define the download policy for this resource page.
Solution:
Why is this even a Resource when you only have one custom page? Just create a single page
Jump to solution
9 Replies
Dennis Koch
Dennis Koch5mo ago
So the "normal" model policies deny access to the custom resource download page, because I can't define the download policy for this resource page.
If you user cannot viewAny then the resource is not registering navigation. How does you policy restrict the access? https://filamentphp.com/docs/3.x/panels/resources/getting-started#authorization
Dennis Koch
Dennis Koch5mo ago
I don't think any other policies should apply to custom pages by default
Nuekrato
NuekratoOP5mo ago
I use the same resource in my "main" app panel that uses the default model policies. I setup an additional "guest" panel for unauthenticated users that can download files from a resource using some kind of magic link. For this I want to check if the URL contains a valid token instead of checking for any model policies (or use a custom policy for that check). For my custom page I don't need any navigation, breadcrumbs or whatsoever. From my Resource class:
public static function getPages(): array
{
return [
'download' => Pages\DownloadProject::route('/download/{token}'),
];
}
public static function getPages(): array
{
return [
'download' => Pages\DownloadProject::route('/download/{token}'),
];
}
I encrounter a second issue with the getPages()-method if I omit the index page for the resource. Somehow the index page seems to be required?
[2024-08-14 12:27:14] local.ERROR: Route [filament.guest.resources.projects.index] not defined. {"view":{"view":"/var/www/html/vendor/filament/filament/resources/views/components/page/index.blade.php","data":[]},"exception":"[object] (Spatie\\LaravelIgnition\\Exceptions\\ViewException(code: 0): Route [filament.guest.resources.projects.index] not defined. at /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php:477)
[stacktrace]
#0 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php(811): Illuminate\\Routing\\UrlGenerator->route()
#1 /var/www/html/vendor/filament/filament/src/Resources/Resource.php(701): route()
#2 /var/www/html/vendor/filament/filament/src/Resources/Pages/Page.php(123): Filament\\Resources\\Resource::getUrl()
#3 /var/www/html/vendor/filament/filament/resources/views/components/page/index.blade.php(36): Filament\\Resources\\Pages\\Page->getBreadcrumbs()
[2024-08-14 12:27:14] local.ERROR: Route [filament.guest.resources.projects.index] not defined. {"view":{"view":"/var/www/html/vendor/filament/filament/resources/views/components/page/index.blade.php","data":[]},"exception":"[object] (Spatie\\LaravelIgnition\\Exceptions\\ViewException(code: 0): Route [filament.guest.resources.projects.index] not defined. at /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php:477)
[stacktrace]
#0 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php(811): Illuminate\\Routing\\UrlGenerator->route()
#1 /var/www/html/vendor/filament/filament/src/Resources/Resource.php(701): route()
#2 /var/www/html/vendor/filament/filament/src/Resources/Pages/Page.php(123): Filament\\Resources\\Resource::getUrl()
#3 /var/www/html/vendor/filament/filament/resources/views/components/page/index.blade.php(36): Filament\\Resources\\Pages\\Page->getBreadcrumbs()
Solution
Dennis Koch
Dennis Koch5mo ago
Why is this even a Resource when you only have one custom page? Just create a single page
Nuekrato
NuekratoOP5mo ago
I created a custom page now. I was thinking about a resource page because I resolve a resource (eloquent model) from the magic link in the end and want to show a table with related data to that model. So I thought using a resource page for this would make sense. But it seems like resource pages do not fit my use case
Dennis Koch
Dennis Koch5mo ago
Resources are wrappers for classic CRUD: Create, Edit, List, Show It doesn't make sense when it's a single page.
Nuekrato
NuekratoOP5mo ago
OK. So I can use a custom (non resource page) like described here: https://filamentphp.com/docs/3.x/panels/pages But still use Eloquent Models and Relationships within Form or Table?
Dennis Koch
Dennis Koch5mo ago
Sure.
Nuekrato
NuekratoOP5mo ago
Alright thank you very much for your help!
Want results from more Discord servers?
Add your server