2015-08-28 17:16:58 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Import;
|
|
|
|
|
2015-12-03 13:36:33 +01:00
|
|
|
use Icinga\Data\ResourceFactory;
|
2016-02-17 10:49:28 +01:00
|
|
|
use Icinga\Module\Director\Hook\ImportSourceHook;
|
2015-08-28 17:16:58 +02:00
|
|
|
use Icinga\Module\Director\Util;
|
|
|
|
use Icinga\Module\Director\Web\Form\QuickForm;
|
|
|
|
|
|
|
|
class ImportSourceLdap extends ImportSourceHook
|
|
|
|
{
|
|
|
|
protected $connection;
|
|
|
|
|
|
|
|
public function fetchData()
|
|
|
|
{
|
2016-02-17 10:00:40 +01:00
|
|
|
$columns = $this->listColumns();
|
2020-03-09 10:19:14 +01:00
|
|
|
$query = $this->connection()
|
|
|
|
->select()
|
|
|
|
->setUsePagedResults()
|
|
|
|
->from($this->settings['objectclass'], $columns);
|
2016-02-17 10:00:40 +01:00
|
|
|
|
2015-08-28 17:16:58 +02:00
|
|
|
if ($base = $this->settings['base']) {
|
|
|
|
$query->setBase($base);
|
|
|
|
}
|
2015-12-02 12:13:50 +01:00
|
|
|
if ($filter = $this->settings['filter']) {
|
|
|
|
$query->setNativeFilter($filter);
|
2015-08-28 17:16:58 +02:00
|
|
|
}
|
|
|
|
|
2016-02-17 10:00:40 +01:00
|
|
|
if (in_array('dn', $columns)) {
|
|
|
|
$result = $query->fetchAll();
|
|
|
|
foreach ($result as $dn => $row) {
|
|
|
|
$row->dn = $dn;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
} else {
|
|
|
|
return $query->fetchAll();
|
|
|
|
}
|
2015-08-28 17:16:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function listColumns()
|
|
|
|
{
|
|
|
|
return preg_split('/,\s*/', $this->settings['query'], -1, PREG_SPLIT_NO_EMPTY);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function addSettingsFormFields(QuickForm $form)
|
|
|
|
{
|
|
|
|
Util::addLDAPResourceFormElement($form, 'resource');
|
|
|
|
$form->addElement('text', 'base', array(
|
2016-03-14 13:27:50 +01:00
|
|
|
'label' => $form->translate('LDAP Search Base'),
|
|
|
|
'description' => $form->translate(
|
|
|
|
'Your LDAP search base. Often something like OU=Users,OU=HQ,DC=your,DC=company,DC=tld'
|
|
|
|
)
|
2015-08-28 17:16:58 +02:00
|
|
|
));
|
|
|
|
$form->addElement('text', 'objectclass', array(
|
2016-03-14 13:27:50 +01:00
|
|
|
'label' => $form->translate('Object class'),
|
|
|
|
'description' => $form->translate(
|
|
|
|
'An object class to search for. Might be "user", "group", "computer" or similar'
|
|
|
|
)
|
2015-08-28 17:16:58 +02:00
|
|
|
));
|
|
|
|
$form->addElement('text', 'filter', array(
|
|
|
|
'label' => 'LDAP filter',
|
2016-03-14 13:27:50 +01:00
|
|
|
'description' => $form->translate(
|
|
|
|
'A custom LDAP filter to use in addition to the object class. This allows'
|
|
|
|
. ' for a lot of flexibility but requires LDAP filter skills. Simple filters'
|
|
|
|
. ' might look as follows: operatingsystem=*server*'
|
|
|
|
)
|
2015-08-28 17:16:58 +02:00
|
|
|
));
|
|
|
|
$form->addElement('textarea', 'query', array(
|
2016-03-14 13:27:50 +01:00
|
|
|
'label' => $form->translate('Properties'),
|
|
|
|
'description' => $form->translate(
|
|
|
|
'The LDAP properties that should be fetched. This is required to be a'
|
2017-10-09 10:46:10 +02:00
|
|
|
. ' comma-separated list like: "cn, dnshostname, operatingsystem, sAMAccountName"'
|
2016-03-14 13:27:50 +01:00
|
|
|
),
|
|
|
|
'spellcheck' => 'false',
|
|
|
|
'required' => true,
|
|
|
|
'rows' => 5,
|
2015-08-28 17:16:58 +02:00
|
|
|
));
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function connection()
|
|
|
|
{
|
|
|
|
if ($this->connection === null) {
|
|
|
|
$this->connection = ResourceFactory::create($this->settings['resource']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->connection;
|
|
|
|
}
|
|
|
|
}
|