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

FIX: PHP8 fix for removed ldap functions #95

Open
wants to merge 1 commit into
base: 2.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 34 additions & 10 deletions src/Iterators/LDAPIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,29 @@ private function fetchPagedResult(): bool
$baseDn = $ldap->getBaseDn();
}

ldap_control_paged_result($resource, $this->getPageSize(), true, $this->cookie);
if ($this->getReturnAttributes() !== null) {
$resultResource = ldap_search($resource, $baseDn ?? '', $this->getFilter() ?? '', $this->getReturnAttributes() ?? []);
if (version_compare(PHP_VERSION, '8.0.0') < 0) {
ldap_control_paged_result($resource, $this->getPageSize(), true, $this->cookie);
if ($this->getReturnAttributes() !== null) {
$resultResource = ldap_search($resource, $baseDn ?? '', $this->getFilter() ?? '', $this->getReturnAttributes() ?? []);
} else {
$resultResource = ldap_search($resource, $baseDn ?? '', $this->getFilter() ?? '');
}
if (! is_resource($resultResource)) {
throw new \Exception('ldap_search returned a non-resource type value' . ldap_error($resource));
}
} else {
$resultResource = ldap_search($resource, $baseDn ?? '', $this->getFilter() ?? '');
}
if (! is_resource($resultResource)) {
throw new \Exception('ldap_search returned a non-resource type value' . ldap_error($resource));
if ($this->getReturnAttributes() !== null) {
$resultResource = ldap_search($resource, $baseDn ?? '', $this->getFilter() ?? '', $this->getReturnAttributes() ?? [],
0, 0, 0, LDAP_DEREF_NEVER,
[['oid' => LDAP_CONTROL_PAGEDRESULTS, 'value' => ['size' => $this->getPageSize(), 'cookie' => $this->cookie]]]
);
} else {
$resultResource = ldap_search($resource, $baseDn ?? '', $this->getFilter() ?? '', [],
0, 0, 0, LDAP_DEREF_NEVER,
[['oid' => LDAP_CONTROL_PAGEDRESULTS, 'value' => ['size' => $this->getPageSize(), 'cookie' => $this->cookie]]]
);
}
$response = ldap_parse_result($resource, $resultResource, $errcode , $matcheddn , $errmsg , $referrals, $controls);
}

$entries = ldap_get_entries($resource, $resultResource);
Expand All @@ -130,9 +145,18 @@ private function fetchPagedResult(): bool
}
$entries = $this->getConvertedEntries($entries);

ErrorHandler::start();
$response = ldap_control_paged_result_response($resource, $resultResource, $this->cookie);
ErrorHandler::stop();
if (version_compare(PHP_VERSION, '8.0.0') < 0) {
ErrorHandler::start();
$response = ldap_control_paged_result_response($resource, $resultResource, $this->cookie);
ErrorHandler::stop();
} else {
if (isset($controls[LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'])) {
// You need to pass the cookie from the last call to the next one
$this->cookie = $controls[LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'];
} else {
$this->cookie = '';
}
}

if ($response !== true) {
throw new LdapException($ldap, 'Paged result was empty');
Expand Down
18 changes: 14 additions & 4 deletions src/Model/LDAPGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ class LDAPGateway
*/
private static $options = [];

/**
* If configured, only user objects matching this LDAP filter will be considered to this service instead of the default.
* @var string
*
* @config
*/
private static $user_filter = [];

/**
* @var Laminas\Ldap\Ldap
*/
Expand Down Expand Up @@ -62,8 +70,10 @@ protected function searchWithIterator($filter, $baseDn = null, $attributes = [])
$records = new LDAPIterator($this->getLdap(), $filter, $baseDn, $attributes, $pageSize);
$results = $this->processSearchResults($records);

// Reset the LDAP pagination control back to the original, otherwise all further LDAP read queries fail
ldap_control_paged_result($this->getLdap()->getResource(), 1000);
if (version_compare(PHP_VERSION, '8.0.0') < 0) {
// Reset the LDAP pagination control back to the original, otherwise all further LDAP read queries fail
ldap_control_paged_result($this->getLdap()->getResource(), 1000);
}

return $results;
}
Expand Down Expand Up @@ -254,7 +264,7 @@ public function getGroupByDN($dn, $baseDn = null, $scope = Ldap::SEARCH_SCOPE_SU
*/
public function getUsers($baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = [], $sort = '')
{
$filter = '(&(objectClass=user)(!(objectClass=computer))(!(samaccountname=Guest))(!(samaccountname=Administrator))(!(samaccountname=krbtgt)))';
$filter = $this->config()->user_filter ?: '(&(objectClass=user)(!(objectClass=computer))(!(samaccountname=Guest))(!(samaccountname=Administrator))(!(samaccountname=krbtgt)))';

$this->extend('updateUsersFilter', $filter);

Expand All @@ -277,7 +287,7 @@ public function getUsers($baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attri
*/
public function getUsersWithIterator($baseDn = null, $attributes = [])
{
$filter = '(&(objectClass=user)(!(objectClass=computer))(!(samaccountname=Guest))(!(samaccountname=Administrator))(!(samaccountname=krbtgt)))';
$filter = $this->config()->user_filter ?: '(&(objectClass=user)(!(objectClass=computer))(!(samaccountname=Guest))(!(samaccountname=Administrator))(!(samaccountname=krbtgt)))';

$this->extend('updateUsersWithIteratorFilter', $filter);

Expand Down
10 changes: 9 additions & 1 deletion src/Services/LDAPService.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ class LDAPService implements Flushable
*/
private static $groups_search_locations = [];

/**
* If configured, only group objects within these locations will be searched for nexted groups to this service.
* @var array
*
* @config
*/
private static $nested_groups_search_locations = [];

/**
* Location to create new users in (distinguished name).
* @var string
Expand Down Expand Up @@ -301,7 +309,7 @@ public function getNestedGroups($dn, $attributes = [])
return LDAPService::$_cache_nested_groups[$dn];
}

$searchLocations = $this->config()->groups_search_locations ?: [null];
$searchLocations = $this->config()->nested_groups_search_locations ?: $this->config()->groups_search_locations ?: [null];
$results = [];
foreach ($searchLocations as $searchLocation) {
$records = $this->getGateway()->getNestedGroups($dn, $searchLocation, Ldap::SEARCH_SCOPE_SUB, $attributes);
Expand Down