Skip to content

Commit 79f7dbf

Browse files
authored
[V5] WherePivot instead of only Where on team relation pivot, better readability (#1944)
* WherePivot instead of only Where on team relation pivot * Fix detach, sync after wherePivot on relations Co-authored-by: Erik Niebla <[email protected]>
1 parent 3f3d874 commit 79f7dbf

File tree

3 files changed

+53
-90
lines changed

3 files changed

+53
-90
lines changed

src/Traits/HasPermissions.php

Lines changed: 24 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,19 @@ public function getPermissionClass()
4444
*/
4545
public function permissions(): BelongsToMany
4646
{
47-
return $this->morphToMany(
47+
$relation = $this->morphToMany(
4848
config('permission.models.permission'),
4949
'model',
5050
config('permission.table_names.model_has_permissions'),
5151
config('permission.column_names.model_morph_key'),
5252
PermissionRegistrar::$pivotPermission
53-
)
54-
->where(function ($q) {
55-
$q->when(PermissionRegistrar::$teams, function ($q) {
56-
$q->where(PermissionRegistrar::$teamsKey, app(PermissionRegistrar::class)->getPermissionsTeamId());
57-
});
58-
});
53+
);
54+
55+
if (! PermissionRegistrar::$teams) {
56+
return $relation;
57+
}
58+
59+
return $relation->wherePivot(PermissionRegistrar::$teamsKey, getPermissionsTeamId());
5960
}
6061

6162
/**
@@ -314,21 +315,6 @@ public function getAllPermissions(): Collection
314315
return $permissions->sort()->values();
315316
}
316317

317-
/**
318-
* Add teams pivot if teams are enabled
319-
*
320-
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
321-
*/
322-
protected function getPermissionsRelation()
323-
{
324-
$relation = $this->permissions();
325-
if (PermissionRegistrar::$teams && ! is_a($this, Role::class)) {
326-
$relation->wherePivot(PermissionRegistrar::$teamsKey, app(PermissionRegistrar::class)->getPermissionsTeamId());
327-
}
328-
329-
return $relation;
330-
}
331-
332318
/**
333319
* Grant the given permission(s) to a role.
334320
*
@@ -338,33 +324,30 @@ protected function getPermissionsRelation()
338324
*/
339325
public function givePermissionTo(...$permissions)
340326
{
341-
$permissionClass = $this->getPermissionClass();
342327
$permissions = collect($permissions)
343328
->flatten()
344-
->map(function ($permission) {
329+
->reduce(function ($array, $permission) {
345330
if (empty($permission)) {
346-
return false;
331+
return $array;
332+
}
333+
334+
$permission = $this->getStoredPermission($permission);
335+
if (! $permission instanceof Permission) {
336+
return $array;
347337
}
348338

349-
return $this->getStoredPermission($permission);
350-
})
351-
->filter(function ($permission) {
352-
return $permission instanceof Permission;
353-
})
354-
->each(function ($permission) {
355339
$this->ensureModelSharesGuard($permission);
356-
})
357-
->map(function ($permission) {
358-
return [$permission->getKeyName() => $permission->getKey(), 'values' => PermissionRegistrar::$teams && ! is_a($this, Role::class) ?
359-
[PermissionRegistrar::$teamsKey => app(PermissionRegistrar::class)->getPermissionsTeamId()] : [],
360-
];
361-
})
362-
->pluck('values', (new $permissionClass())->getKeyName())->toArray();
340+
341+
$array[$permission->getKey()] = PermissionRegistrar::$teams && ! is_a($this, Role::class) ?
342+
[PermissionRegistrar::$teamsKey => getPermissionsTeamId()] : [];
343+
344+
return $array;
345+
}, []);
363346

364347
$model = $this->getModel();
365348

366349
if ($model->exists) {
367-
$this->getPermissionsRelation()->sync($permissions, false);
350+
$this->permissions()->sync($permissions, false);
368351
$model->load('permissions');
369352
} else {
370353
$class = \get_class($model);
@@ -396,7 +379,7 @@ function ($object) use ($permissions, $model) {
396379
*/
397380
public function syncPermissions(...$permissions)
398381
{
399-
$this->getPermissionsRelation()->detach();
382+
$this->permissions()->detach();
400383

401384
return $this->givePermissionTo($permissions);
402385
}
@@ -410,7 +393,7 @@ public function syncPermissions(...$permissions)
410393
*/
411394
public function revokePermissionTo($permission)
412395
{
413-
$this->getPermissionsRelation()->detach($this->getStoredPermission($permission));
396+
$this->permissions()->detach($this->getStoredPermission($permission));
414397

415398
if (is_a($this, get_class(app(PermissionRegistrar::class)->getRoleClass()))) {
416399
$this->forgetCachedPermissions();

src/Traits/HasRoles.php

Lines changed: 28 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,23 @@ public function getRoleClass()
4141
*/
4242
public function roles(): BelongsToMany
4343
{
44-
$model_has_roles = config('permission.table_names.model_has_roles');
45-
46-
return $this->morphToMany(
44+
$relation = $this->morphToMany(
4745
config('permission.models.role'),
4846
'model',
49-
$model_has_roles,
47+
config('permission.table_names.model_has_roles'),
5048
config('permission.column_names.model_morph_key'),
5149
PermissionRegistrar::$pivotRole
52-
)
53-
->where(function ($q) use ($model_has_roles) {
54-
$q->when(PermissionRegistrar::$teams, function ($q) use ($model_has_roles) {
55-
$teamId = app(PermissionRegistrar::class)->getPermissionsTeamId();
56-
$q->where($model_has_roles.'.'.PermissionRegistrar::$teamsKey, $teamId)
57-
->where(function ($q) use ($teamId) {
58-
$teamField = config('permission.table_names.roles').'.'.PermissionRegistrar::$teamsKey;
59-
$q->whereNull($teamField)->orWhere($teamField, $teamId);
60-
});
50+
);
51+
52+
if (! PermissionRegistrar::$teams) {
53+
return $relation;
54+
}
55+
56+
return $relation->wherePivot(PermissionRegistrar::$teamsKey, getPermissionsTeamId())
57+
->where(function ($q) {
58+
$teamField = config('permission.table_names.roles').'.'.PermissionRegistrar::$teamsKey;
59+
$q->whereNull($teamField)->orWhere($teamField, getPermissionsTeamId());
6160
});
62-
});
6361
}
6462

6563
/**
@@ -98,21 +96,6 @@ public function scopeRole(Builder $query, $roles, $guard = null): Builder
9896
});
9997
}
10098

101-
/**
102-
* Add teams pivot if teams are enabled
103-
*
104-
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
105-
*/
106-
protected function getRolesRelation()
107-
{
108-
$relation = $this->roles();
109-
if (PermissionRegistrar::$teams && ! is_a($this, Permission::class)) {
110-
$relation->wherePivot(PermissionRegistrar::$teamsKey, app(PermissionRegistrar::class)->getPermissionsTeamId());
111-
}
112-
113-
return $relation;
114-
}
115-
11699
/**
117100
* Assign the given role to the model.
118101
*
@@ -122,33 +105,30 @@ protected function getRolesRelation()
122105
*/
123106
public function assignRole(...$roles)
124107
{
125-
$roleClass = $this->getRoleClass();
126108
$roles = collect($roles)
127109
->flatten()
128-
->map(function ($role) {
110+
->reduce(function ($array, $role) {
129111
if (empty($role)) {
130-
return false;
112+
return $array;
113+
}
114+
115+
$role = $this->getStoredRole($role);
116+
if (! $role instanceof Role) {
117+
return $array;
131118
}
132119

133-
return $this->getStoredRole($role);
134-
})
135-
->filter(function ($role) {
136-
return $role instanceof Role;
137-
})
138-
->each(function ($role) {
139120
$this->ensureModelSharesGuard($role);
140-
})
141-
->map(function ($role) {
142-
return [$role->getKeyName() => $role->getKey(), 'values' => PermissionRegistrar::$teams && ! is_a($this, Permission::class) ?
143-
[PermissionRegistrar::$teamsKey => app(PermissionRegistrar::class)->getPermissionsTeamId()] : [],
144-
];
145-
})
146-
->pluck('values', (new $roleClass())->getKeyName())->toArray();
121+
122+
$array[$role->getKey()] = PermissionRegistrar::$teams && ! is_a($this, Permission::class) ?
123+
[PermissionRegistrar::$teamsKey => getPermissionsTeamId()] : [];
124+
125+
return $array;
126+
}, []);
147127

148128
$model = $this->getModel();
149129

150130
if ($model->exists) {
151-
$this->getRolesRelation()->sync($roles, false);
131+
$this->roles()->sync($roles, false);
152132
$model->load('roles');
153133
} else {
154134
$class = \get_class($model);
@@ -178,7 +158,7 @@ function ($object) use ($roles, $model) {
178158
*/
179159
public function removeRole($role)
180160
{
181-
$this->getRolesRelation()->detach($this->getStoredRole($role));
161+
$this->roles()->detach($this->getStoredRole($role));
182162

183163
$this->load('roles');
184164

@@ -198,7 +178,7 @@ public function removeRole($role)
198178
*/
199179
public function syncRoles(...$roles)
200180
{
201-
$this->getRolesRelation()->detach();
181+
$this->roles()->detach();
202182

203183
return $this->assignRole($roles);
204184
}

src/helpers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ function setPermissionsTeamId($id)
3636
*/
3737
function getPermissionsTeamId()
3838
{
39-
app(\Spatie\Permission\PermissionRegistrar::class)->getPermissionsTeamId();
39+
return app(\Spatie\Permission\PermissionRegistrar::class)->getPermissionsTeamId();
4040
}
4141
}

0 commit comments

Comments
 (0)