Skip to content

Commit

Permalink
[v6] Sync v5 fix: Avoid triggering eloquent.retrieved event #2498
Browse files Browse the repository at this point in the history
  • Loading branch information
erikn69 authored Sep 13, 2023
1 parent 982d4d4 commit 8f15d18
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 17 deletions.
13 changes: 6 additions & 7 deletions src/PermissionRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,12 +354,11 @@ private function getSerializedRoleRelation($permission): array

private function getHydratedPermissionCollection(): Collection
{
$permissionClass = $this->getPermissionClass();
$permissionInstance = new $permissionClass();
$permissionInstance = new ($this->getPermissionClass())();

return Collection::make(array_map(
fn ($item) => $permissionInstance
->newFromBuilder($this->aliasedArray(array_diff_key($item, ['r' => 0])))
fn ($item) => $permissionInstance->newInstance([], true)
->setRawAttributes($this->aliasedArray(array_diff_key($item, ['r' => 0])), true)
->setRelation('roles', $this->getHydratedRoleCollection($item['r'] ?? [])),
$this->permissions['permissions']
));
Expand All @@ -374,11 +373,11 @@ private function getHydratedRoleCollection(array $roles): Collection

private function hydrateRolesCache(): void
{
$roleClass = $this->getRoleClass();
$roleInstance = new $roleClass();
$roleInstance = new ($this->getRoleClass())();

array_map(function ($item) use ($roleInstance) {
$role = $roleInstance->newFromBuilder($this->aliasedArray($item));
$role = $roleInstance->newInstance([], true)
->setRawAttributes($this->aliasedArray($item), true);
$this->cachedRoles[$role->getKey()] = $role;
}, $this->permissions['roles']);

Expand Down
12 changes: 4 additions & 8 deletions src/Traits/HasPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,8 @@ public function scopePermission(Builder $query, $permissions): Builder
{
$permissions = $this->convertToPermissionModels($permissions);

$permissionClass = $this->getPermissionClass();
$permissionKey = (new $permissionClass())->getKeyName();
$roleClass = is_a($this, Role::class) ? static::class : $this->getRoleClass();
$roleKey = (new $roleClass())->getKeyName();
$permissionKey = (new ($this->getPermissionClass())())->getKeyName();
$roleKey = (new (is_a($this, Role::class) ? static::class : $this->getRoleClass())())->getKeyName();

$rolesWithPermissions = is_a($this, Role::class) ? [] : array_unique(
array_reduce($permissions, fn ($result, $permission) => array_merge($result, $permission->roles->all()), [])
Expand Down Expand Up @@ -132,10 +130,8 @@ public function scopeWithoutPermission(Builder $query, $permissions, $debug = fa
{
$permissions = $this->convertToPermissionModels($permissions);

$permissionClass = $this->getPermissionClass();
$permissionKey = (new $permissionClass())->getKeyName();
$roleClass = is_a($this, Role::class) ? static::class : $this->getRoleClass();
$roleKey = (new $roleClass())->getKeyName();
$permissionKey = (new ($this->getPermissionClass())())->getKeyName();
$roleKey = (new (is_a($this, Role::class) ? static::class : $this->getRoleClass())())->getKeyName();

$rolesWithPermissions = is_a($this, Role::class) ? [] : array_unique(
array_reduce($permissions, fn ($result, $permission) => array_merge($result, $permission->roles->all()), [])
Expand Down
3 changes: 1 addition & 2 deletions src/Traits/HasRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,7 @@ public function hasRole($roles, string $guard = null): bool
}

if (is_int($roles) || PermissionRegistrar::isUid($roles)) {
$roleClass = $this->getRoleClass();
$key = (new $roleClass())->getKeyName();
$key = (new ($this->getRoleClass())())->getKeyName();

return $guard
? $this->roles->where('guard_name', $guard)->contains($key, $roles)
Expand Down

0 comments on commit 8f15d18

Please sign in to comment.