Using Spatie's Laravel-permission, is there a way to assign roles to a pivot table?

For example, if I have a User that is associated with a Tenant, but also Groups within Tenants, can I assign a role so that it gets saved in the model_has_roles table so that it has something like:
role_id = 549839
model_type = App\Models\Tenant_User
model_id = 53093

role_id = 549840
model_type = App\Models\Group_User
model_id = 83553
role_id = 549839
model_type = App\Models\Tenant_User
model_id = 53093

role_id = 549840
model_type = App\Models\Group_User
model_id = 83553
It continuously wants to just associate the role with the User model, and not based specifically on the specific relationship. And how would I try to define that relationship in the Resource?
4 Replies
cheesegrits
cheesegrits14mo ago
Can you give a little more information about where and how you are trying to create this relationship?
Clay Bitner
Clay BitnerOP14mo ago
I've tried it in a few ways. First is within the UsersRelationManager within the GroupResource RelationManagers. Another place is within the UserResource when creating a user within the Tenant Admin panel. Inside the form, it looks like this:
return $form->schema([
Forms\Components\Section::make('User Details')
->schema([
Forms\Components\TextInput::make('first_name')
->required(),
Forms\Components\TextInput::make('last_name')
->required(),
Forms\Components\TextInput::make('username')
->required(),
Forms\Components\TextInput::make('email')
->required()
->email(),
Forms\Components\TextInput::make('password')
->required()
->password()
->maxLength(255)
->rule(Password::default())
->dehydrateStateUsing(fn ($state) => \Hash::make($state))
->visible(fn ($livewire) => $livewire instanceof Pages\CreateUser),
Forms\Components\CheckboxList::make('tenant_user.roles')
->relationship('roles', 'name'),
// ->preload(true)
]),
]);
return $form->schema([
Forms\Components\Section::make('User Details')
->schema([
Forms\Components\TextInput::make('first_name')
->required(),
Forms\Components\TextInput::make('last_name')
->required(),
Forms\Components\TextInput::make('username')
->required(),
Forms\Components\TextInput::make('email')
->required()
->email(),
Forms\Components\TextInput::make('password')
->required()
->password()
->maxLength(255)
->rule(Password::default())
->dehydrateStateUsing(fn ($state) => \Hash::make($state))
->visible(fn ($livewire) => $livewire instanceof Pages\CreateUser),
Forms\Components\CheckboxList::make('tenant_user.roles')
->relationship('roles', 'name'),
// ->preload(true)
]),
]);
Would you have any advice on how to approach it, though? Is there something I should be changing in the user model? I have these functions defining the groups and tenants within the User model:
public function groups(): BelongsToMany
{
return $this->belongsToMany(Group::class)->using(GroupUser::class)->withTimestamps();
}

public function tenants(): BelongsToMany
{
return $this->belongsToMany(Tenant::class)->using(TenantUser::class)->withTimestamps();
}
public function groups(): BelongsToMany
{
return $this->belongsToMany(Group::class)->using(GroupUser::class)->withTimestamps();
}

public function tenants(): BelongsToMany
{
return $this->belongsToMany(Tenant::class)->using(TenantUser::class)->withTimestamps();
}
cheesegrits
cheesegrits14mo ago
I'm a little confused as to what you are trying to do here. Surely if you are assigning your users to groups, then you need to be assigning them to groups on that form, not roles. Then you assign the roles for that group elsewhere. So you would have as more or less static set of permissions for a given group (like 'Editors' might have 'can-edit-thing' and 'can-delete-thing'), and in the User form you would add them to that group (the user-group pivot), you wouldn't be assigning roles?
Clay Bitner
Clay BitnerOP14mo ago
Within the app, there are these groups where people can have chats and discussions and share pictures and the like. A user might have the role of "moderator" in one group (which allows them to add pictures, add new posts, remove posts, edit comments), but not another group, even if they are in both groups. In another group they may just have the role of "user" which allows them to view posts and pictures, for example. But a group exists within an organization (tenant), so there are multiple layers. From a UX perspective, when you add a user to a group, you'd want to define their role at the same time - because that's where the association lies.
Want results from more Discord servers?
Add your server