Replies: 4 comments 3 replies
-
In short, I want to set the My few super-admin users do not need to be a member of a team to be able to select that team to do some admin in there. |
Beta Was this translation helpful? Give feedback.
-
Ah, just read the role check again, and it doesn't do quite what I had hoped: return $relation->wherePivot(PermissionRegistrar::$teamsKey, getPermissionsTeamId())
->where(function ($q) {
$teamField = config('permission.table_names.roles').'.'.PermissionRegistrar::$teamsKey;
$q->whereNull($teamField)->orWhere($teamField, getPermissionsTeamId());
}); The return $relation->wherePivot(PermissionRegistrar::$teamsKey, getPermissionsTeamId())
->orWherePivotNull(PermissionRegistrar::$teamsKey) // <<<=== new condition
->where(function ($q) {
$teamField = config('permission.table_names.roles').'.'.PermissionRegistrar::$teamsKey;
$q->whereNull($teamField)->orWhere($teamField, getPermissionsTeamId());
}); That one line - I think - would open up roles that can span across all teams, making super-admins much easier to manage. If you didn't want that feature, then leave the team ID field as not-nullable. If this works for me, would it be a feature worth submitting? |
Beta Was this translation helpful? Give feedback.
-
The pivot methods in eloquent are a bit of a mess. They all have an "or" version, but you cannot chain them because eloquent does not wrap them up parenthesis. Not sure what the point of them are. So instead, I have moved the pivot conditions to a where clause, which works because the pivot table is joined to the authenticated table. My /**
* A model may have multiple roles.
*/
public function roles(): BelongsToMany
{
$relation = $this->morphToMany(
config('permission.models.role'),
'model',
config('permission.table_names.model_has_roles'),
config('permission.column_names.model_morph_key'),
PermissionRegistrar::$pivotRole
);
if (! PermissionRegistrar::$teams) {
return $relation;
}
return $relation //->wherePivot(PermissionRegistrar::$teamsKey, getPermissionsTeamId())
->where(
fn ($query) => $query->where(config('permission.table_names.model_has_roles').'.'.PermissionRegistrar::$teamsKey, getPermissionsTeamId())
->orWhereNull(config('permission.table_names.model_has_roles').'.'.PermissionRegistrar::$teamsKey)
)
->where(function ($q) {
$teamField = config('permission.table_names.roles').'.'.PermissionRegistrar::$teamsKey;
$q->whereNull($teamField)->orWhere($teamField, getPermissionsTeamId());
});
} That's for PHP 8.1+, so replace the arrow function for earlier versions. So this allows me to make the The pivot conditions moved to the It is necessary to notify the table name in the pivot condition, as the same column appears in both joined tables - the |
Beta Was this translation helpful? Give feedback.
-
This is the approach I'm trying now:
Then team-based roles can be added when in a team context, and administration roles can be added when not in a team context. Note: My team is set in the URL path, so any administration path that does not have a team in there, does not have a team selected, and so the user is not in a team context. I also realised that a user does not have to be a member of team to have roles on that team, which is great. |
Beta Was this translation helpful? Give feedback.
-
We have been using v4 for some time, but with teams applied. Out implementation looks very similar to the teams introduced in v5, with one major exception: all model roles and model permissions must have a team set.
Our team IDs are nullable, which means they work across teams. So a super-admin user can have a role that applies to ALL teams, just by setting their
team_id
tonull
for an admin role.The way this operates in this package is to make the team ID mandatory for every user role. So on creating a team, users requiring access to that team with an elevated privilage must be explicitely added to that team.
Now, it looks to me like this package already handles roles in this way:
https://github.com/spatie/laravel-permission/blob/main/src/Traits/HasRoles.php#L65
Here a user role can be for the team currently set or can be
null
, for "any team".The database migrations to convert the package to team-based permissions makes those columns mandatory, they cannot be
null
.So - if I made the
team_id
columns optional in the migrations, would this just work as I want? Could I add a role to a user with anull
team_id
in the user/role pivot table, and have a role that the user will then have in every team they go into? Would be awsome if it worked this way, then I can drop a bunch of custom permissions code we have.Beta Was this translation helpful? Give feedback.
All reactions