Ldap\Connection: Some coding style fixes

refs #8954
This commit is contained in:
Johannes Meyer 2015-06-23 17:27:30 +02:00
parent 7640284564
commit 372ca8859e

View File

@ -213,6 +213,7 @@ class Connection implements Selectable
if ($this->root === null) { if ($this->root === null) {
$this->root = Root::forConnection($this); $this->root = Root::forConnection($this);
} }
return $this->root; return $this->root;
} }
@ -359,20 +360,19 @@ class Connection implements Selectable
if (ldap_errno($this->ds) === self::LDAP_NO_SUCH_OBJECT) { if (ldap_errno($this->ds) === self::LDAP_NO_SUCH_OBJECT) {
return false; return false;
} }
throw new LdapException(
'LDAP list for "%s" failed: %s', throw new LdapException('LDAP list for "%s" failed: %s', $dn, ldap_error($this->ds));
$dn,
ldap_error($this->ds)
);
} }
$children = ldap_get_entries($this->ds, $result); $children = ldap_get_entries($this->ds, $result);
for ($i = 0; $i < $children['count']; $i++) { for ($i = 0; $i < $children['count']; $i++) {
$result = $this->deleteRecursively($children[$i]['dn']); $result = $this->deleteRecursively($children[$i]['dn']);
if (!$result) { if (! $result) {
//return result code, if delete fails // TODO: return result code, if delete fails
throw new LdapException('Recursively deleting "%s" failed', $dn); throw new LdapException('Recursively deleting "%s" failed', $dn);
} }
} }
return $this->deleteDn($dn); return $this->deleteDn($dn);
} }
@ -393,13 +393,10 @@ class Connection implements Selectable
$result = @ldap_delete($this->ds, $dn); $result = @ldap_delete($this->ds, $dn);
if ($result === false) { if ($result === false) {
if (ldap_errno($this->ds) === self::LDAP_NO_SUCH_OBJECT) { if (ldap_errno($this->ds) === self::LDAP_NO_SUCH_OBJECT) {
return false; return false; // TODO: Isn't it a success if something i'd like to remove is not existing at all???
} }
throw new LdapException(
'LDAP delete for "%s" failed: %s', throw new LdapException('LDAP delete for "%s" failed: %s', $dn, ldap_error($this->ds));
$dn,
ldap_error($this->ds)
);
} }
return true; return true;
@ -418,10 +415,7 @@ class Connection implements Selectable
{ {
$rows = $this->fetchAll($query, array()); $rows = $this->fetchAll($query, array());
if (count($rows) > 1) { if (count($rows) > 1) {
throw new LdapException( throw new LdapException('Cannot fetch single DN for %s', $query);
'Cannot fetch single DN for %s',
$query
);
} }
return key($rows); return key($rows);
} }
@ -482,7 +476,7 @@ class Connection implements Selectable
$limit = $query->getLimit(); $limit = $query->getLimit();
$offset = $query->hasOffset() ? $query->getOffset() - 1 : 0; $offset = $query->hasOffset() ? $query->getOffset() - 1 : 0;
if (empty($fields)) { if ($fields === null) {
$fields = $query->getColumns(); $fields = $query->getColumns();
} }
@ -490,8 +484,8 @@ class Connection implements Selectable
if ($serverSorting && $query->hasOrder()) { if ($serverSorting && $query->hasOrder()) {
ldap_set_option($this->ds, LDAP_OPT_SERVER_CONTROLS, array( ldap_set_option($this->ds, LDAP_OPT_SERVER_CONTROLS, array(
array( array(
'oid' => Capability::LDAP_SERVER_SORT_OID, 'oid' => Capability::LDAP_SERVER_SORT_OID,
'value' => $this->encodeSortRules($query->getOrder()) 'value' => $this->encodeSortRules($query->getOrder())
) )
)); ));
} }
@ -570,7 +564,7 @@ class Connection implements Selectable
*/ */
protected function runPagedQuery(Query $query, array $fields = null, $pageSize = null) protected function runPagedQuery(Query $query, array $fields = null, $pageSize = null)
{ {
if (! isset($pageSize)) { if ($pageSize === null) {
$pageSize = static::PAGE_SIZE; $pageSize = static::PAGE_SIZE;
} }
@ -579,7 +573,7 @@ class Connection implements Selectable
$queryString = (string) $query; $queryString = (string) $query;
$base = $query->getBase() ?: $this->rootDn; $base = $query->getBase() ?: $this->rootDn;
if (empty($fields)) { if ($fields === null) {
$fields = $query->getColumns(); $fields = $query->getColumns();
} }
@ -587,8 +581,8 @@ class Connection implements Selectable
if ($serverSorting && $query->hasOrder()) { if ($serverSorting && $query->hasOrder()) {
ldap_set_option($this->ds, LDAP_OPT_SERVER_CONTROLS, array( ldap_set_option($this->ds, LDAP_OPT_SERVER_CONTROLS, array(
array( array(
'oid' => Capability::LDAP_SERVER_SORT_OID, 'oid' => Capability::LDAP_SERVER_SORT_OID,
'value' => $this->encodeSortRules($query->getOrder()) 'value' => $this->encodeSortRules($query->getOrder())
) )
)); ));
} }
@ -597,8 +591,8 @@ class Connection implements Selectable
$cookie = ''; $cookie = '';
$entries = array(); $entries = array();
do { do {
// do not set controlPageResult as a critical extension, since we still want the // Do not request the pagination control as a critical extension, as we want the
// possibillity server to return an answer in case the pagination extension is missing. // server to return results even if the paged search request cannot be satisfied
ldap_control_paged_result($this->ds, $pageSize, false, $cookie); ldap_control_paged_result($this->ds, $pageSize, false, $cookie);
$results = @ldap_search( $results = @ldap_search(
@ -654,10 +648,11 @@ class Connection implements Selectable
// This applies no matter whether paged search requests are permitted or not. You're done once you // This applies no matter whether paged search requests are permitted or not. You're done once you
// got everything you were out for. // got everything you were out for.
if ($serverSorting && count($entries) !== $limit) { if ($serverSorting && count($entries) !== $limit) {
// The server does not support pagination, but still returned a response by ignoring the // The server does not support pagination, but still returned a response by ignoring the
// pagedResultsControl. We output a warning to indicate that the pagination control was ignored. // pagedResultsControl. We output a warning to indicate that the pagination control was ignored.
Logger::warning('Unable to request paged LDAP results. Does the server allow paged search requests?'); Logger::warning(
'Unable to request paged LDAP results. Does the server allow paged search requests?'
);
} }
} }
@ -821,14 +816,17 @@ class Connection implements Selectable
} }
$ds = ldap_connect($hostname, $this->port); $ds = ldap_connect($hostname, $this->port);
try { try {
$this->capabilities = $this->discoverCapabilities($ds); $this->capabilities = $this->discoverCapabilities($ds);
$this->discoverySuccess = true; $this->discoverySuccess = true;
} catch (LdapException $e) { } catch (LdapException $e) {
Logger::debug($e); Logger::debug($e);
Logger::warning('LADP discovery failed, assuming default LDAP settings.'); Logger::warning('LADP discovery failed, assuming default LDAP capabilities.');
$this->capabilities = new Capability(); // create empty default capabilities $this->capabilities = new Capability(); // create empty default capabilities
$this->discoverySuccess = false;
} }
if ($this->encryption === static::STARTTLS) { if ($this->encryption === static::STARTTLS) {
$force_tls = false; $force_tls = false;
if ($this->capabilities->hasStartTls()) { if ($this->capabilities->hasStartTls()) {
@ -878,6 +876,7 @@ class Connection implements Selectable
} else { } else {
$ldap_conf = $this->getConfigDir('ldap_nocert.conf'); $ldap_conf = $this->getConfigDir('ldap_nocert.conf');
} }
putenv('LDAPRC=' . $ldap_conf); // TODO: Does not have any effect putenv('LDAPRC=' . $ldap_conf); // TODO: Does not have any effect
if (getenv('LDAPRC') !== $ldap_conf) { if (getenv('LDAPRC') !== $ldap_conf) {
throw new LdapException('putenv failed'); throw new LdapException('putenv failed');