Question

First of all thanks for the interest in my topic. I think that this question is easy.

I have a many-to-many relationship created in Laravel. My 'users' table represents the users who can login in my system. My 'modules' table represents the modules who the user logged can access. So, i would like to verify if the current logged user have access to the requested module.

For example, the user try to access the URL: laravel.com/blog/insert But the user no have permissions for this module, so the system verify the relationships and returns if the user can continue in this page or not. I think that this validation can be done in the __construct method of the controllers.

My tables: 'users', 'modules' and 'users_modules'

I've tried something like this on my 'Modules' Model, but no success. The $id represents the Module ID:

public static function getUser($id)
{
    $module = Modules::where('id', '=', $id)->first();

    return $module->users;
}

My users model relationship:

public function modules()
{
    return $this->belongsToMany('Modules', 'users_modules', 'user_id', 'module_id');
}

And my modules relationship:

public function users()
{
    return $this->belongsToMany('User', 'users_modules', 'module_id', 'user_id');
}

How can i do that? Thanks for everyone who helps me! :)

Was it helpful?

Solution 2

Solved the problem using Entrust for Laravel. Thanks all those who helped me!

Link for Entrust:

https://github.com/Zizaco/entrust

OTHER TIPS

The way to do this is to create a custom route filter in app/filters.php that checks to see if the user is a member of the module in question.

Have a look at the docs here particularly the section on specifying filter parameters, that should get you going in the right direction.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top