Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kreierson's avatar

Separate roles/permissions for users belonging to multiple organizations

I have an application where a user can belong to multiple organizations. I want to set it up in a way that a user can have different roles/permissions for each organization. I am using Laravel and plan on implementing Spatie/laravel-permission. What is the best way to implement this?

I have tried setting up two guards, one for the main user account and another for the pivot model between the user and the organization they log into. So basically when they log into the app using the main user model, I ask them which organization they would like to log into, when they choose the organization I will then also set up an auth session on the pivot model that links the user to the organization and access the roles off that model. This works, but having to manage the auth sessions is kind of a pain.

// User Model
class User extends Authenticatable
{
    public function organizationUsers()
    {
        return $this->hasMany(OrganizationUser::class);
    }
}
// OrganizationUser Model

class Organziationuser extends Authenticatable
{
    use HasRoles;

    public $guard_name = 'organization_user';

    public function organization()
    {
        return $this->belongsTo(Organization::class);
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

I would expect a user to be able to log into the application using a single login, but also be able to have different permissions for different organizations.

I wonder how it would look if i added an organization_id to the model_has_roles and model_has_permissions tables??

0 likes
6 replies
Snapey's avatar

As @tray2 suggests you can add an organization column to the roles and then implement a global scope to automatically hide roles for different organizations

fasriyaa's avatar

For anyone who is still trying to resolve this issue. I had the same issue and resolved it by adding organisation column to the role model (as suggested by @snapey) and change the role() function in hasRoles trait (vendors/spatie/laravel-permission/src/traits) to the following.

public function roles(): MorphToMany { return $this->morphToMany( config('permission.models.role'), 'model', config('permission.table_names.model_has_roles'), config('permission.column_names.model_morph_key'), 'role_id' )->where('organization_id', $organization_id); }

1 like
Bvk's avatar

Hi, You can provide us with the code in more detail.

Please or to participate in this conversation.