I am trying to come up with an efficient and flexible RBAC solution for my app. I have done a little research and think I have created the following.
In my User model I have:
...
public function role() {
return $this->belongsToMany('App\Models\Role', 'user_roles');
}
public function hasRole($role) {
if($this->role->where('name', $role)->first())
return true;
}
...
And an example of usage:
Route::group(['middleware' => 'auth'], function () {
Route::get('/dashboard', function () {
if (Auth::user()->hasRole('Sales')) {
return view('dashboards/sales');
} else {
return 'Don\'t know where to send you :(';
}
});
});
Permissions are assigned to roles, but permissions are not checked in the example above. Roles are then assigned to users and a user can have many roles.
Is the way I have done things scaleable and an effective RBAC solution?