I made a link using
Route::get('/admin/edit/user/{user_id}', [UserController::class, 'editUser'])->name('edit.user');
That is why when a user click on edit he see https://0608sns.org/admin/edit/user/1789
and he can edit his profile but if that user put another id like 1790 he can use that user profile.
So, How can i prevent it?
2
Answers
You have to use
Policies
to allow an entity to do something or not.In your example, you should create a Policy called
UserPolicy
withphp artisan make:policy UserPolicy
, and the contents of it should be:Then, in your controller, you should have something like this:
I may be missing something, this is from the top of my head, so try it out and let me know if it worked or not.
Actually you just need a simple
Gate
[Policies also great]But for simple authorizations like this, we prefer Gates.
Go over here https://laravel.com/docs/10.x/authorization#writing-gates