LdapResourceForm: Add hostname validation

refs #2645
This commit is contained in:
Johannes Meyer 2017-02-02 11:08:07 +01:00
parent 35ba15a7a6
commit 7864450dde
3 changed files with 942 additions and 890 deletions

View File

@ -4,6 +4,7 @@
namespace Icinga\Forms\Config\Resource; namespace Icinga\Forms\Config\Resource;
use Icinga\Web\Form; use Icinga\Web\Form;
use Icinga\Web\Url;
use Icinga\Protocol\Ldap\LdapConnection; use Icinga\Protocol\Ldap\LdapConnection;
/** /**
@ -20,9 +21,7 @@ class LdapResourceForm extends Form
} }
/** /**
* Create and add elements to this form * {@inheritdoc}
*
* @param array $formData The data sent by the user
*/ */
public function createElements(array $formData) public function createElements(array $formData)
{ {
@ -49,7 +48,38 @@ class LdapResourceForm extends Form
'The hostname or address of the LDAP server to use for authentication.' 'The hostname or address of the LDAP server to use for authentication.'
. ' You can also provide multiple hosts separated by a space' . ' You can also provide multiple hosts separated by a space'
), ),
'value' => 'localhost' 'value' => 'localhost',
'validators' => array(
array(
'Callback',
false,
array(
'callback' => function ($v) {
$withoutScheme = $withScheme = false;
foreach (explode(' ', $v) as $uri) {
if (preg_match('~^(?<!://)[^:]+:\d+$~', $uri)) {
return false;
}
$url = Url::fromPath($uri);
if ($url->getScheme()) {
$withScheme = true;
} else {
$withoutScheme = true;
}
}
return $withScheme ^ $withoutScheme;
},
'messages' => array(
'callbackValue' => $this->translate(
'A protocol scheme such as ldap:// or ldaps:// is mandatory for URIs with a given'
. ' port and for all other URIs as well once a scheme is given for a single one.'
)
)
)
)
)
) )
); );
$this->addElement( $this->addElement(

File diff suppressed because it is too large Load Diff