Add tests on Ldap class and fix some related bugs
This commit is contained in:
parent
d183309280
commit
191a119606
6 changed files with 1327 additions and 21 deletions
|
@ -4,7 +4,9 @@
|
||||||
"description": "Object oriented interface for searching and manipulating LDAP entries & filters",
|
"description": "Object oriented interface for searching and manipulating LDAP entries & filters",
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpstan/phpstan": "^1.10",
|
"phpstan/phpstan": "^1.10",
|
||||||
"phpunit/phpunit": "^9"
|
"phpunit/phpunit": "^9",
|
||||||
|
"lstrojny/phpunit-function-mocker": "^1.1",
|
||||||
|
"mockery/mockery": "^1.5"
|
||||||
},
|
},
|
||||||
"authors": [
|
"authors": [
|
||||||
{
|
{
|
||||||
|
|
|
@ -10,3 +10,9 @@ parameters:
|
||||||
-
|
-
|
||||||
message: "#Method .*::test.*\\(\\) has no return type specified\\.#"
|
message: "#Method .*::test.*\\(\\) has no return type specified\\.#"
|
||||||
path: tests/*
|
path: tests/*
|
||||||
|
-
|
||||||
|
message: "#Call to an undefined method Mockery\\\\ExpectationInterface|Mockery\\\\HigherOrderMessage::once\\(\\)\\.#"
|
||||||
|
path: tests/*
|
||||||
|
-
|
||||||
|
message: "#Call to an undefined method PHPUnit\\\\Extension\\\\FunctionMocker::expects\\(\\)\\.#"
|
||||||
|
path: tests/*
|
||||||
|
|
|
@ -416,8 +416,6 @@ class Entry {
|
||||||
if ($this->ldap)
|
if ($this->ldap)
|
||||||
// @phpstan-ignore-next-line
|
// @phpstan-ignore-next-line
|
||||||
return $this->ldap->error($error);
|
return $this->ldap->error($error);
|
||||||
// @phpstan-ignore-next-line
|
|
||||||
error_log($error);
|
|
||||||
throw new LdapException($error); // @phpstan-ignore-line
|
throw new LdapException($error); // @phpstan-ignore-line
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
41
src/Ldap.php
41
src/Ldap.php
|
@ -21,6 +21,8 @@ namespace EesyLDAP;
|
||||||
* @property-read string $scope
|
* @property-read string $scope
|
||||||
* @property-read bool $raise_on_error
|
* @property-read bool $raise_on_error
|
||||||
* @property-read bool $use_schema
|
* @property-read bool $use_schema
|
||||||
|
* @property-read array<string,string> $config_aliases
|
||||||
|
* @property-read array<string,mixed> $default_config
|
||||||
* @property-read Schema|false $schema
|
* @property-read Schema|false $schema
|
||||||
*/
|
*/
|
||||||
class Ldap {
|
class Ldap {
|
||||||
|
@ -180,7 +182,7 @@ class Ldap {
|
||||||
break;
|
break;
|
||||||
case 'filter':
|
case 'filter':
|
||||||
if (is_string($value))
|
if (is_string($value))
|
||||||
$value = new Filter($value);
|
$value = Filter::parse($value);
|
||||||
elseif (!$value instanceof Filter) {
|
elseif (!$value instanceof Filter) {
|
||||||
$this -> error(
|
$this -> error(
|
||||||
"Invalid filter found in configuration: expect to be a string or a \EesyLDAP\Filter ".
|
"Invalid filter found in configuration: expect to be a string or a \EesyLDAP\Filter ".
|
||||||
|
@ -199,8 +201,6 @@ class Ldap {
|
||||||
);
|
);
|
||||||
continue 2;
|
continue 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
$this -> config[$key] = $value;
|
$this -> config[$key] = $value;
|
||||||
}
|
}
|
||||||
|
@ -238,7 +238,7 @@ class Ldap {
|
||||||
public function connect($raise=null, $bind_dn=null, $bind_password=null) {
|
public function connect($raise=null, $bind_dn=null, $bind_password=null) {
|
||||||
if ($this -> _link)
|
if ($this -> _link)
|
||||||
return true;
|
return true;
|
||||||
if (!self :: check_ldap_extension($raise))
|
if (!$this -> check_ldap_extension($raise))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
foreach($this -> hosts as $host) {
|
foreach($this -> hosts as $host) {
|
||||||
|
@ -264,7 +264,7 @@ class Ldap {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$this -> bind($bind_dn, $bind_password)) {
|
if (!$this -> bind($bind_dn, $bind_password, false)) {
|
||||||
$this -> close();
|
$this -> close();
|
||||||
$this -> error(
|
$this -> error(
|
||||||
"Fail to bind on %s", false);
|
"Fail to bind on %s", false);
|
||||||
|
@ -277,7 +277,7 @@ class Ldap {
|
||||||
if (in_array($option, $this -> required_options)) {
|
if (in_array($option, $this -> required_options)) {
|
||||||
$this -> close();
|
$this -> close();
|
||||||
$this -> error('Fail to set option %s on %s', false, $option, $host);
|
$this -> error('Fail to set option %s on %s', false, $option, $host);
|
||||||
continue;
|
continue 2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -350,8 +350,8 @@ class Ldap {
|
||||||
return $this -> error("%s: unkown error", $raise, $prefix);
|
return $this -> error("%s: unkown error", $raise, $prefix);
|
||||||
$err = $this->_link?$this->_link->err2str($errno):false;
|
$err = $this->_link?$this->_link->err2str($errno):false;
|
||||||
if (!$err)
|
if (!$err)
|
||||||
return $this -> error("%s: error #%s", $raise, $errno);
|
return $this -> error("%s: error #%s", $raise, $prefix, $errno);
|
||||||
return $this -> error("%s: %s (#%s)", $raise, $err, $errno);
|
return $this -> error("%s: %s (#%s)", $raise, $prefix, $err, $errno);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -366,8 +366,6 @@ class Ldap {
|
||||||
if ($extra_args)
|
if ($extra_args)
|
||||||
$error = call_user_func_array('sprintf', array_merge(array($error), $extra_args));
|
$error = call_user_func_array('sprintf', array_merge(array($error), $extra_args));
|
||||||
// Note: sprintf always return string
|
// Note: sprintf always return string
|
||||||
// @phpstan-ignore-next-line
|
|
||||||
error_log($error);
|
|
||||||
if (is_null($raise))
|
if (is_null($raise))
|
||||||
$raise = $this -> raise_on_error;
|
$raise = $this -> raise_on_error;
|
||||||
if ($raise)
|
if ($raise)
|
||||||
|
@ -449,14 +447,17 @@ class Ldap {
|
||||||
* - attributes: Array of attribute names, which the entries in the expected result should contain.
|
* - attributes: Array of attribute names, which the entries in the expected result should contain.
|
||||||
* It is good practice to limit this to just the ones you need.
|
* It is good practice to limit this to just the ones you need.
|
||||||
*
|
*
|
||||||
* @param string|null $base Base DN of the search (optional, default: null == use config)
|
|
||||||
* @param string|null $filter Filter of the search (optional, default: null == use config)
|
* @param string|null $filter Filter of the search (optional, default: null == use config)
|
||||||
|
* @param string|Entry|null $base Base DN of the search (optional, default: null == use config)
|
||||||
|
* @param string|null $scope Scope of the search (optional, default: null == use params or config)
|
||||||
|
* @param array<string>|null $attributes Expected attributes returned by the search (optional,
|
||||||
|
* default: null == use params or config)
|
||||||
* @param array<string,mixed>|null $params Other optional parameters of the search (optional, default: null == use config)
|
* @param array<string,mixed>|null $params Other optional parameters of the search (optional, default: null == use config)
|
||||||
* @param bool|null $raise See error() (optional, default: null)
|
* @param bool|null $raise See error() (optional, default: null)
|
||||||
* @return array<string,Entry>|false Array of Entry object with DN as key, or False in case of error
|
* @return array<string,Entry>|false Array of Entry object with DN as key, or False in case of error
|
||||||
* @throws LdapException
|
* @throws LdapException
|
||||||
*/
|
*/
|
||||||
public function search($base=null, $filter=null, $params=null, $raise=null) {
|
public function search($filter=null, $base=null, $scope=null, $attributes=null, $params=null, $raise=null) {
|
||||||
if (!$this -> _link)
|
if (!$this -> _link)
|
||||||
return $this -> error("Can't search: no LDAP link");
|
return $this -> error("Can't search: no LDAP link");
|
||||||
if (is_null($base))
|
if (is_null($base))
|
||||||
|
@ -469,17 +470,22 @@ class Ldap {
|
||||||
$filter = $filter->as_string(); // convert Filter object as string
|
$filter = $filter->as_string(); // convert Filter object as string
|
||||||
|
|
||||||
// Adjust search function to expected scope
|
// Adjust search function to expected scope
|
||||||
$scope = self :: get_param($params, 'scope', $this->scope, 'string');
|
$scope = $scope?$scope:self :: get_param($params, 'scope', $this->scope, 'string');
|
||||||
if (!is_string($scope) || !array_key_exists($scope, self :: $search_function_by_scope))
|
if (!is_string($scope) || !array_key_exists($scope, self :: $search_function_by_scope))
|
||||||
return $this -> error("Invalid scope '%s' specified", $raise, $scope);
|
return $this -> error("Invalid scope '%s' specified", $raise, $scope);
|
||||||
|
|
||||||
|
// Compute expected attributes
|
||||||
|
$attributes = $attributes?$attributes:self :: get_param($params, 'attributes', array(), 'array');
|
||||||
|
if (!is_array($attributes))
|
||||||
|
return $this -> error("Invalid expected attributes specified: must be an array", $raise);
|
||||||
|
|
||||||
// Run the search
|
// Run the search
|
||||||
$search = @call_user_func(
|
$search = @call_user_func(
|
||||||
// @phpstan-ignore-next-line
|
// @phpstan-ignore-next-line
|
||||||
array($this->_link, self :: $search_function_by_scope[$scope]),
|
array($this->_link, self :: $search_function_by_scope[$scope]),
|
||||||
$base,
|
$base,
|
||||||
$filter,
|
$filter,
|
||||||
self :: get_param($params, 'attributes', array(), 'array'),
|
$attributes,
|
||||||
self :: get_param($params, 'attrsonly', false, 'bool')?1:0,
|
self :: get_param($params, 'attrsonly', false, 'bool')?1:0,
|
||||||
self :: get_param($params, 'sizelimit', 0, 'int'),
|
self :: get_param($params, 'sizelimit', 0, 'int'),
|
||||||
self :: get_param($params, 'timelimit', 0, 'int')
|
self :: get_param($params, 'timelimit', 0, 'int')
|
||||||
|
@ -521,16 +527,17 @@ class Ldap {
|
||||||
*
|
*
|
||||||
* @param string $dn DN of the expected entry
|
* @param string $dn DN of the expected entry
|
||||||
* @param string|null $filter Filter of the search (optional, default: null == use config)
|
* @param string|null $filter Filter of the search (optional, default: null == use config)
|
||||||
|
* @param array<string>|null $attributes Expected attributes returned by the search (optional,
|
||||||
|
* default: null == use params or config)
|
||||||
* @param array<string,mixed>|null $params Other optional parameters of the search (optional,
|
* @param array<string,mixed>|null $params Other optional parameters of the search (optional,
|
||||||
* default: null == use config, see search())
|
* default: null == use config, see search())
|
||||||
* @param bool|null $raise See error() (optional, default: null)
|
* @param bool|null $raise See error() (optional, default: null)
|
||||||
* @return Entry|false The requested Entry object, or False in case of error
|
* @return Entry|false The requested Entry object, or False in case of error
|
||||||
* @throws LdapException
|
* @throws LdapException
|
||||||
*/
|
*/
|
||||||
public function get_entry($dn, $filter=null, $params=null, $raise=null) {
|
public function get_entry($dn, $filter=null, $attributes=null, $params=null, $raise=null) {
|
||||||
$params = is_array($params)?$params:array();
|
$params = is_array($params)?$params:array();
|
||||||
$params['scope'] = 'base';
|
$entries = $this -> search($filter, $dn, 'base', $attributes, $params, $raise);
|
||||||
$entries = $this -> search($dn, $filter, $params, $raise);
|
|
||||||
if (is_array($entries) && count($entries) == 1)
|
if (is_array($entries) && count($entries) == 1)
|
||||||
return array_pop($entries);
|
return array_pop($entries);
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -100,7 +100,7 @@ class Schema {
|
||||||
public static function load(&$ldap, $raise=null) {
|
public static function load(&$ldap, $raise=null) {
|
||||||
$entry = $ldap->get_entry(
|
$entry = $ldap->get_entry(
|
||||||
'cn=SubSchema', '(objectclass=*)',
|
'cn=SubSchema', '(objectclass=*)',
|
||||||
array('attributes' => array_keys(self :: $attributes_to_entry_types))
|
array_keys(self :: $attributes_to_entry_types)
|
||||||
);
|
);
|
||||||
if (!$entry instanceof Entry)
|
if (!$entry instanceof Entry)
|
||||||
return $ldap->error('Fail to load cn=SubSchema entry', $raise);
|
return $ldap->error('Fail to load cn=SubSchema entry', $raise);
|
||||||
|
|
1293
tests/LdapTest.php
Normal file
1293
tests/LdapTest.php
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue