Skip to content

Commit

Permalink
code reformatting
Browse files Browse the repository at this point in the history
  • Loading branch information
angeljqv committed Apr 14, 2023
1 parent 38f2049 commit fee1ff9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
33 changes: 17 additions & 16 deletions src/Traits/HasPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,23 @@ public function scopePermission(Builder $query, $permissions): Builder
{
$permissions = $this->convertToPermissionModels($permissions);

$permissionClass = $this->getPermissionClass();
$permissionKey = (new $permissionClass())->getKeyName();
$roleClass = $this->getRoleClass();
$roleKey = (new $roleClass())->getKeyName();

$rolesWithPermissions = is_a($this, Role::class) ? [] : array_unique(
array_reduce($permissions, fn ($result, $permission) => array_merge($result, $permission->roles->all()), [])
);

return $query->where(fn (Builder $query) => $query
->whereHas('permissions', function (Builder $subQuery) use ($permissions) {
$permissionClass = $this->getPermissionClass();
$key = (new $permissionClass())->getKeyName();
$subQuery->whereIn(config('permission.table_names.permissions').".$key", \array_column($permissions, $key));
})
->when(count($rolesWithPermissions) > 0, fn ($subQuery) => $subQuery
->orWhereHas('roles', function (Builder $subQuery) use ($rolesWithPermissions) {
$roleClass = $this->getRoleClass();
$key = (new $roleClass())->getKeyName();
$subQuery->whereIn(config('permission.table_names.roles').".$key", \array_column($rolesWithPermissions, $key));
})
->whereHas('permissions', fn (Builder $subQuery) => $subQuery
->whereIn(config('permission.table_names.permissions').".$permissionKey", \array_column($permissions, $permissionKey))
)
->when(count($rolesWithPermissions), fn ($whenQuery) => $whenQuery
->orWhereHas('roles', fn (Builder $subQuery) => $subQuery
->whereIn(config('permission.table_names.roles').".$roleKey", \array_column($rolesWithPermissions, $roleKey))
)
)
);
}
Expand Down Expand Up @@ -209,6 +210,10 @@ protected function hasWildcardPermission($permission, $guardName = null): bool
{
$guardName = $guardName ?? $this->getDefaultGuardName();

if ($permission instanceof \BackedEnum) {
$permission = $permission->value;
}

if (is_int($permission) || PermissionRegistrar::isUid($permission)) {
$permission = $this->getPermissionClass()::findById($permission, $guardName);
}
Expand All @@ -217,10 +222,6 @@ protected function hasWildcardPermission($permission, $guardName = null): bool
$permission = $permission->name;
}

if ($permission instanceof \BackedEnum) {
$permission = $permission->value;
}

if (! is_string($permission)) {
throw WildcardPermissionInvalidArgument::create();
}
Expand Down Expand Up @@ -461,7 +462,7 @@ protected function getStoredPermission($permissions)
$permissions = $permissions->value;
}

if (is_numeric($permissions) || PermissionRegistrar::isUid($permissions)) {
if (is_int($permissions) || PermissionRegistrar::isUid($permissions)) {
return $this->getPermissionClass()::findById($permissions, $this->getDefaultGuardName());
}

Expand Down
4 changes: 2 additions & 2 deletions src/Traits/HasRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function scopeRole(Builder $query, $roles, $guard = null): Builder
return $role;
}

$method = is_numeric($role) || PermissionRegistrar::isUid($role) ? 'findById' : 'findByName';
$method = is_int($role) || PermissionRegistrar::isUid($role) ? 'findById' : 'findByName';

return $this->getRoleClass()::{$method}($role, $guard ?: $this->getDefaultGuardName());
}, Arr::wrap($roles));
Expand Down Expand Up @@ -347,7 +347,7 @@ protected function getStoredRole($role): Role
$role = $role->value;
}

if (is_numeric($role) || PermissionRegistrar::isUid($role)) {
if (is_int($role) || PermissionRegistrar::isUid($role)) {
return $this->getRoleClass()::findById($role, $this->getDefaultGuardName());
}

Expand Down

0 comments on commit fee1ff9

Please sign in to comment.