samedi 10 octobre 2015

Laravel 5.1 protecting routes with ownership using middleware

I'm trying to learn Laravel, but don't quite understand how i can accomplish my task the best way.

Scenario,

A user (user_id 5) may create a todo list, with the id of 3 (todo_id 3). Each of them live in their own model, so users model, and todo model. I'm currently checking if they created the todo by passing this into the method,

public function show($id)
{
    TodoList::where('id',$id)->where('user_id', auth::id())->firstOrFail();
    return 'ok';
}

So, i was thinking it would be possible to filter all this, using middleware. I have more models that requires the same action, so user 2, can't change/edit the todo_list for user 5 etc. And this needs to be attached to all methods, to block their access. Show, View, Delete etc.

Is there any way of doing this, so i don't have to create a new middleware for each model?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire