Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v6] Sync v5 fix: Avoid triggering eloquent.retrieved event #2498

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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