LdapUtils: Add isDN function

This commit is contained in:
Markus Frosch 2017-08-18 13:10:45 +02:00 committed by Eric Lippmann
parent c22efa4519
commit 28b1a00d4a
2 changed files with 63 additions and 4 deletions

View File

@ -19,8 +19,9 @@ class LdapUtils
* UTF-8 chars like German umlauts would otherwise be escaped and shown
* as backslash-prefixed hexcode-sequenzes.
*
* @param string DN
* @param boolean Returns 'type=value' when true and 'value' when false
* @param string $dn DN
* @param boolean $with_type Returns 'type=value' when true and 'value' when false
*
* @return string
*/
public static function explodeDN($dn, $with_type = true)
@ -45,7 +46,8 @@ class LdapUtils
*
* TODO: throw away, this is not how it shall be done
*
* @param string DN-component
* @param array $parts DN-component
*
* @return string
*/
public static function implodeDN($parts)
@ -61,12 +63,28 @@ class LdapUtils
return $str;
}
/**
* Test if supplied value looks like a DN
*
* @param mixed $value
*
* @return bool
*/
public static function isDn($value)
{
if (is_string($value)) {
return ldap_dn2ufn($value) !== false;
}
return false;
}
/**
* Quote a string that should be used in a DN
*
* Special characters will be escaped
*
* @param string DN-component
* @param string $str DN-component
*
* @return string
*/
public static function quoteForDN($str)

View File

@ -0,0 +1,41 @@
<?php
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */
namespace Tests\Icinga\Protocol\Ldap;
use Icinga\Protocol\Ldap\LdapUtils;
use Icinga\Test\BaseTestCase;
class LdapUtilsTest extends BaseTestCase
{
protected static $validDn = array(
'dc=example,dc=com',
'dc=example, dc=com',
'dc = example , dc = com',
'DC=EXAMPLE,DC=COM',
'0.9.2342.19200300.100.1.25=Example,0.9.2342.19200300.100.1.25=Com',
'CN=host,OU=Datacenter Servers,DC=example,DC=com',
'CN=Doe\, John,OU=Admin Users,DC=example,DC=com'
);
protected static $invalidDn = array(
'testuser',
'heinzimüller',
'test.user@example.com',
'test,user@example.com',
);
public function testIsDnForValidValues()
{
foreach (static::$validDn as $dn) {
$this->assertTrue(LdapUtils::isDn($dn), 'DN should be tested as valid value: ' . $dn);
}
}
public function testIsDnForInvalidValues()
{
foreach (static::$invalidDn as $dn) {
$this->assertFalse(LdapUtils::isDn($dn), 'DN should be tested as invalid value: ' . $dn);
}
}
}