Skip to content

Commit

Permalink
Detect AD ldap server and automatically fill in presets
Browse files Browse the repository at this point in the history
Detect wether the added server is an Active Directory server and use the
information fetched from the discovery to fill the form with more accurate
preset values.

refs #6141
  • Loading branch information
majentsch committed May 28, 2014
1 parent bf6f410 commit 9bd053e
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 83 deletions.
1 change: 0 additions & 1 deletion application/forms/Config/ResourceForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@ public function isValid($data)
if (isset($data['resource_force_creation']) && $data['resource_force_creation']) {
return true;
}
var_dump($data);
if ($data['resource_type_old'] === $data['resource_type']) {
if (!$this->isValidResource($data)) {
$this->addForceCreationCheckbox();
Expand Down
99 changes: 71 additions & 28 deletions application/forms/Install/AuthenticationPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
*/
use Icinga\Web\Wizard\Page;

use Icinga\Data\ResourceFactory;
use Icinga\Form\Config\Authentication\LdapBackendForm;
use Icinga\Form\Config\Resource\ResourceBaseForm;
use Icinga\Form\Config\Authentication\BaseBackendForm;
use Icinga\Form\Config\ResourceForm;
use Icinga\Protocol\Ldap\Connection;
use Zend_Form;
Expand Down Expand Up @@ -66,6 +69,13 @@ class AuthenticationPage extends Page
*/
private $resourceForm = null;

/**
* The sub form used to configure the authentication backend.
*
* @var BaseBackendForm
*/
private $authForm = null;

/**
* The sub form used to configure the authentication backend.
*
Expand Down Expand Up @@ -96,6 +106,16 @@ public function init()
*/
public function create()
{
$this->addElement(
'text',
'resource_name',
array(
'required' => true,
'label' => t('Resource Name'),
'helptext' => t('Specify the name of the new resource.')
)
);

$this->addElement(
'select',
'authentication_mode',
Expand Down Expand Up @@ -137,56 +157,75 @@ public function create()
// Try to discover servers
$connections = Connection::discoverServerlistForDomain($hostname);
if (count($connections) > 0) {
array_unshift($connections, '');
$this->addHostSelectBox($connections, count($connections) - 1);
foreach ($connections as $connection) {
// TODO: Check Connections
}
$this->addLdapResourceForm($hostname, $this->getRequest()->getParam('resource_name'));
} else {
$this->addErrorMessage(t('No Servers found on this domain.'));
}
} else if (isset($hostname) && $ip->isValid($hostname)) {
$this->addHostSelectBox(array($hostname), 1);
$this->addLdapResourceForm();
$this->addLdapResourceForm($hostname, $this->getRequest()->getParam('resource_name'));
} else {
$this->addCheckButton();
}
if ($this->getRequest()->getParam('ldap_hostname') !== NULL) {
$this->addLdapResourceForm();
}
break;
case self::AUTHENTICATION_MODE_EXTERNAL:
// TODO: external subform
break;
}
}

private function addLdapResourceForm()
private function addLdapResourceForm($hostname, $resourceName)
{
$hostname = $this->getElement('ldap_hostname')->getValue();
if ($hostname === '') {
return;
}

$form = new LdapResourceForm();
$form->setResource(new Zend_Config(array()));
$form->buildForm();
$form->setDefault('resource_ldap_hostname', $hostname);
$form->getElement('resource_ldap_hostname')->setValue($hostname);
$this->setResourceSubForm($form);

$this->addLdapBackendForm($hostname, $form);
}

private function addLdapBackendForm($hostname, $resourceForm)
{
$name = 'ldap_authentication';

$form = new LdapBackendForm();
$form->setBackendName($name);
$config = $this->getConfig()->get('backend', new Zend_Config(array()));
$form->setBackend($config);
$form->buildForm();

$form->getElement('backend_' . $name . '_resource')
->setValue($this->getElement('resource_name')->getValue());

$form->removeElement('backend_' . $name . '_resource');

// TODO: Get credentials form input.
$this->discoverCapabilities(array(
'hostname' => $hostname,
'port' => '636',
'bind_dn' => 'DC=int,DC=netways,DC=de',
'root_dn' => 'DC=int,DC=netways,DC=de',
'bind_pw' => 'passwort'
));
$cap = $this->discoverCapabilities($hostname);
if ($cap->msCapabilities->ActiveDirectoryOid) {
// Host is an ActiveDirectory server
if (isset($cap->defaultNamingContext)) {
$resourceForm->setDefault('resource_ldap_root_dn', $cap->defaultNamingContext);
$resourceForm->getElement('resource_ldap_root_dn')->setValue($cap->defaultNamingContext);
}
$form->setDefault('backend_' . $name . '_user_name_attribute', 'sAMAccountName');
$form->getElement('backend_' . $name . '_user_name_attribute')->setValue('sAMAccountName');
$form->setDefault('backend_' . $name . '_user_class', 'user');
$form->getElement('backend_' . $name . '_user_class')->setValue('user');
}
$this->setAuthSubForm($form);
}

private function discoverCapabilities($config)
private function discoverCapabilities($hostname)
{
$conn = new Connection(new Zend_Config($config));
var_dump($conn->getDefaultNamingContext());
var_dump($conn->namingContexts());
$conn = new Connection(
new Zend_Config(array('hostname' => $hostname))
);
$conn->connect();
return $conn->getCapabilities();
}

private function addHostSelectBox($connections, $count)
Expand Down Expand Up @@ -221,15 +260,19 @@ private function addCheckButton()

private function setResourceSubForm(ResourceBaseForm $form)
{
$form->setResource($this->getConfig()->get(
'resource',
new Zend_Config(array()))
);
$config = $this->getConfig()->get('resource', new Zend_Config(array()));
$form->setResource($config);
$form->buildForm();
$this->addSubForm($form, 'resource');
$this->resourceForm = $form;
}

private function setAuthSubForm(BaseBackendForm $form)
{
$this->addSubForm($form, 'backend');
return $form;
}

/**
* Return if the given set of data is valid.
*
Expand Down
1 change: 1 addition & 0 deletions library/Icinga/Data/ResourceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public static function createResource(Zend_Config $config)
$resource = new DbConnection($config);
break;
case 'ldap':
var_dump($config);
$resource = new LdapConnection($config);
break;
case 'statusdat':
Expand Down

0 comments on commit 9bd053e

Please sign in to comment.