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:Jump to solution
```php
public static function canAccess(array $parameters = []): bool
{
return auth()->user()->id == 42;
}...
13 Replies
you can use the mount method in the EditPage
also, authorizeAccess
Thanks, I will check authorizeAccess
Are there any docs or examples for authorizeAccess?
yes, if you want to use policies
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?
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#authorizationuse
$this->getRecord()
to get the current recordI 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
This is telling you that the method you are trying to override doesn’t have the same signature as the parent class.
but it shows that error bool must be compatible. ...
Add
array $parameters = []
to your canAccess method in edit.
I good editor/ide would help you here by autocompleting the methods.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