Skip to content

Commit

Permalink
LdapResourceForm: Validate the host field and do not require a port
Browse files Browse the repository at this point in the history
fixes #7990
  • Loading branch information
Johannes Meyer committed Mar 10, 2015
1 parent f87a43b commit a34d602
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 14 deletions.
36 changes: 29 additions & 7 deletions application/forms/Config/Resource/LdapResourceForm.php
Expand Up @@ -32,7 +32,7 @@ public function createElements(array $formData)
array(
'required' => true,
'label' => $this->translate('Resource Name'),
'description' => $this->translate('The unique name of this resource')
'description' => $this->translate('The unique name of this resource.')
)
);
$this->addElement(
Expand All @@ -42,18 +42,40 @@ public function createElements(array $formData)
'required' => true,
'label' => $this->translate('Host'),
'description' => $this->translate(
'The hostname or address of the LDAP server to use for authentication'
'The hostname, address or URL of the LDAP server.'
),
'value' => 'localhost'
'value' => 'localhost',
'validators' => array(
array(
'Callback',
false,
array(
'callback' => function ($v) {
return strpos($v, '?') === false;
},
'messages' => array(
'callbackValue' => $this->translate(
'The URL pointing to the LDAP server must not contain any filter attributes.'
)
)
)
)
),
'requirement' => $this->translate(
'The LDAP server\'s URL must have the following format: [ldap[s]://]host[:port]'
)
)
);
$this->addElement(
'number',
'port',
array(
'required' => true,
'allowEmpty' => true,
'label' => $this->translate('Port'),
'description' => $this->translate('The port of the LDAP server to use for authentication'),
'description' => $this->translate(
'The port of the LDAP server. Leave empty if you\'ll set this as part of the URL above.'
. ' If not set the default port (389) is being used.'
),
'value' => 389
)
);
Expand All @@ -74,7 +96,7 @@ public function createElements(array $formData)
array(
'required' => true,
'label' => $this->translate('Bind DN'),
'description' => $this->translate('The user dn to use for querying the ldap server')
'description' => $this->translate('The user dn to use for querying the ldap server.')
)
);
$this->addElement(
Expand All @@ -84,7 +106,7 @@ public function createElements(array $formData)
'required' => true,
'renderPassword' => true,
'label' => $this->translate('Bind Password'),
'description' => $this->translate('The password to use for querying the ldap server')
'description' => $this->translate('The password to use for querying the ldap server.')
)
);

Expand Down
3 changes: 1 addition & 2 deletions library/Icinga/Protocol/Ldap/Connection.php
Expand Up @@ -3,7 +3,6 @@

namespace Icinga\Protocol\Ldap;

use Exception;
use Icinga\Exception\ProgrammingError;
use Icinga\Protocol\Ldap\Exception as LdapException;
use Icinga\Application\Platform;
Expand Down Expand Up @@ -76,7 +75,7 @@ public function __construct(ConfigObject $config)
$this->bind_dn = $config->bind_dn;
$this->bind_pw = $config->bind_pw;
$this->root_dn = $config->root_dn;
$this->port = $config->get('port', $this->port);
$this->port = $config->get('port') ?: $this->port;
}

public function getHostname()
Expand Down
13 changes: 8 additions & 5 deletions library/Icinga/Web/Form/Element/Number.php
Expand Up @@ -132,12 +132,15 @@ public function getStep()
*/
public function isValid($value, $context = null)
{
$this->setValue($value);
$value = $this->getValue();
if (! is_numeric($value)) {
$this->addError(sprintf($this->translate('\'%s\' is not a valid number'), $value));
if (! parent::isValid($value, $context)) {
return false;
}
return parent::isValid($value, $context);

if ((! empty($value) || !$this->getAllowEmpty()) && !is_numeric($value)) {
$this->addError(sprintf(t('\'%s\' is not a valid number'), $value));
return false;
}

return true;
}
}

0 comments on commit a34d602

Please sign in to comment.