A good read on reality: http://laravel.io/forum/05-12-2015-has-many-through-relationship-depth
Users in Orgs with org dependent Roles. Many to Many to Many.
I've got multiple users in multiple organizations with roles dependent on organization.
Ok, so initially we had a model that was fairly straightforward. One user belongs to one org with many roles.
// Tables:
users: {id, username, etc..}
orgs: { id, name, etc..}
roles: {id, name, org_id} //Note: roles can be created by the org.
org_roles {id, org_id, role_id}
role_user: {id, role_id, user_id}
And all was good in the world. I have models for all three of these and relationships and everything is wonderful.
Then it is decided that some users need to be able to be part of more than one organization. It's just how it is. That means our tables need to change, and that's where things start to get a little less... good.
// Tables:
// Basically everything is the same as above, but now role_user becomes
role_user: {id, role_id, user_id, org_id}
// Which would really be a three way pivot.
org_role_user:{id, role_id, user_id, org_id}
// Laravel does not seem to allow this. Or if it does, I'm not finding anything.
Now I have role_user pivot with a org_id in there as well, so I know which role applies to what org.
Where this starts to break down for me is in the how to implement this nicely with laravel.
Everything in laravel centers around a single many to many. Not many to many to many. I've looked and I'm stuck. I need to be able to do two things.
- get all roles across all org's for a user so I can return the users capabilities in each of the organizations they are part off.
- For authentication I need to get JUST the roles that the user has from the org they are accessing. So that I can compare the proper permissions from those roles to those implemented in my policies. After all, logging into an org with another org's permissions is bad.
- adding and removing roles from users based on the org they are currently interacting with.
Now I've been scouring the web and my books for anything on how to do these type of relationships properly and I'm not finding anything helpful. This is the first time for me that using the basic laravel eloquent models doesn't just work and I'm stuck.
Regards, O.
Please or to participate in this conversation.