Redirect when entering a certain edit page in resource

I would like to know where in the resource file or edit page should I put a condition and what is the way to do it. I need to check if the user can edit some record based on the record category. I know how to hide the edit button, but the user can enter the id of the record manually in the url and edit it anyway. My guess is it should be put in the Edit page of the resource. But not sure how it should be done. For simplicity, let's say we want to redirect user back to the listing or homepage when his id is 42. How to do that?
Solution:
```php public static function canAccess(array $parameters = []): bool { return auth()->user()->id == 42; }...
Jump to solution
13 Replies
LeandroFerreira
LeandroFerreira8mo ago
you can use the mount method in the EditPage
public function mount(int | string $record): void
{
parent::mount($record);
//...
}
public function mount(int | string $record): void
{
parent::mount($record);
//...
}
also, authorizeAccess
protected function authorizeAccess(): void
{
parent::authorizeAccess();
//...
}
protected function authorizeAccess(): void
{
parent::authorizeAccess();
//...
}
Wirkhof
WirkhofOP8mo ago
Thanks, I will check authorizeAccess Are there any docs or examples for authorizeAccess?
LeandroFerreira
LeandroFerreira8mo ago
yes, if you want to use policies
Wirkhof
WirkhofOP8mo ago
But authorizeAccess can be used without addding policy for the whole model? How would you do the condition for user with an id of 42 in the EditUser.php file?
LeandroFerreira
LeandroFerreira8mo ago
this method is present in the EditRecord class. You could inspect it to see how it works I think you can also use canAccess. This is documented https://filamentphp.com/docs/3.x/panels/pages#authorization
LeandroFerreira
LeandroFerreira8mo ago
use $this->getRecord() to get the current record
Wirkhof
WirkhofOP8mo ago
I need to restrict users to edit other users based on a custom logic I can hide buttons and stuff but they can still enter the url directly I am getting Declaration of EditUser::canAccess(): bool must be compatible with Page::canAccess(array $parameters = []): bool What should I put in canAccess(<<here>>) ? It wants some array of parameters What parameters? How would you do it with canAccess() on EditUser.php and allow access only if you are user with id 42? I have tried
public static function canAccess(): bool
{
return auth()->user()->id == 42;
}
public static function canAccess(): bool
{
return auth()->user()->id == 42;
}
awcodes
awcodes8mo ago
This is telling you that the method you are trying to override doesn’t have the same signature as the parent class.
Wirkhof
WirkhofOP8mo ago
but it shows that error bool must be compatible. ...
awcodes
awcodes8mo ago
Add array $parameters = [] to your canAccess method in edit. I good editor/ide would help you here by autocompleting the methods.
Wirkhof
WirkhofOP8mo ago
It's from the docs https://filamentphp.com/docs/3.x/panels/pages#authorization A good docs would help me here, I agree. Thanks nevertheless. I appreciate your time and willingness to help. So, for Chatbots and Livebots here is what worked in EditUser.php file:
Solution
Wirkhof
Wirkhof8mo ago
public static function canAccess(array $parameters = []): bool
{
return auth()->user()->id == 42;
}
public static function canAccess(array $parameters = []): bool
{
return auth()->user()->id == 42;
}
Want results from more Discord servers?
Add your server