I hope this package gonna help you : https://github.com/Pharaonic/laravel-users
Aug 28, 2021
2
Level 2
Looking for a better way of doing this
Hi all,
I've got a project which has users, teams and roles. Users have a system-wide role and a role within each team. I have a controller which returns a list of models from the DB, let's say 'clients'. I'd like to return different things based on the users role. At the moment I'm doing this with a big if statement in the same controller but I know there is a nicer way, I just don't know what it is. I know this is Laravel 101 but it's something I've never quite figured a good solution to. Any ideas?
if(Auth::user()->currentTeam){
$team_role = Auth::user()->teamRole(Auth::user()->currentTeam);
//if user is within a team and user is 'admin' role
//then send them all clients in team
if($team_role == 'admin'){
//...
//if user is within a team and user is 'user' role
//then send them only the clients assigned to them
}else if($team_role == 'user'){
//...
}
}else{
//if user is not within team and has 'admin' system role
//then send them all clients in system
if(Auth::user()->role == 'admin'){
//...
}
}
Please or to participate in this conversation.