2013-08-16 16:24:12 +02:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2013-08-16 16:24:12 +02:00
|
|
|
|
2015-06-02 09:58:57 +02:00
|
|
|
namespace Icinga\Forms\Config\UserBackend;
|
2013-08-16 16:24:12 +02:00
|
|
|
|
2014-08-11 10:43:54 +02:00
|
|
|
use Exception;
|
2014-08-29 15:16:13 +02:00
|
|
|
use Icinga\Web\Form;
|
2014-11-18 13:11:52 +01:00
|
|
|
use Icinga\Data\ConfigObject;
|
2014-04-16 11:50:58 +02:00
|
|
|
use Icinga\Data\ResourceFactory;
|
2014-09-29 11:06:16 +02:00
|
|
|
use Icinga\Exception\AuthenticationException;
|
2015-06-05 17:20:31 +02:00
|
|
|
use Icinga\Authentication\User\UserBackend;
|
2013-08-16 16:24:12 +02:00
|
|
|
|
|
|
|
/**
|
2015-06-02 09:58:57 +02:00
|
|
|
* Form class for adding/modifying LDAP user backends
|
2013-08-16 16:24:12 +02:00
|
|
|
*/
|
2014-08-29 15:16:13 +02:00
|
|
|
class LdapBackendForm extends Form
|
2013-08-16 16:24:12 +02:00
|
|
|
{
|
|
|
|
/**
|
2014-08-29 15:16:13 +02:00
|
|
|
* The ldap resource names the user can choose from
|
2014-08-11 10:43:54 +02:00
|
|
|
*
|
2014-07-29 12:21:58 +02:00
|
|
|
* @var array
|
2014-04-16 11:50:58 +02:00
|
|
|
*/
|
2014-07-29 12:21:58 +02:00
|
|
|
protected $resources;
|
2014-04-16 11:50:58 +02:00
|
|
|
|
2014-08-11 10:39:13 +02:00
|
|
|
/**
|
|
|
|
* Initialize this form
|
|
|
|
*/
|
|
|
|
public function init()
|
2013-08-16 16:24:12 +02:00
|
|
|
{
|
2014-08-29 15:16:13 +02:00
|
|
|
$this->setName('form_config_authbackend_ldap');
|
|
|
|
}
|
2013-08-16 16:24:12 +02:00
|
|
|
|
2014-08-29 15:16:13 +02:00
|
|
|
/**
|
|
|
|
* Set the resource names the user can choose from
|
|
|
|
*
|
|
|
|
* @param array $resources The resources to choose from
|
|
|
|
*
|
2015-04-07 14:23:26 +02:00
|
|
|
* @return $this
|
2014-08-29 15:16:13 +02:00
|
|
|
*/
|
|
|
|
public function setResources(array $resources)
|
|
|
|
{
|
|
|
|
$this->resources = $resources;
|
|
|
|
return $this;
|
2014-07-29 12:21:58 +02:00
|
|
|
}
|
2013-08-16 16:24:12 +02:00
|
|
|
|
2014-08-11 10:43:54 +02:00
|
|
|
/**
|
|
|
|
* @see Form::createElements()
|
|
|
|
*/
|
2014-07-29 12:21:58 +02:00
|
|
|
public function createElements(array $formData)
|
|
|
|
{
|
2015-06-05 17:20:31 +02:00
|
|
|
$isAd = isset($formData['type']) ? $formData['type'] === 'msldap' : false;
|
|
|
|
|
2014-09-03 12:21:31 +02:00
|
|
|
$this->addElement(
|
|
|
|
'text',
|
|
|
|
'name',
|
|
|
|
array(
|
|
|
|
'required' => true,
|
2015-01-19 11:26:23 +01:00
|
|
|
'label' => $this->translate('Backend Name'),
|
|
|
|
'description' => $this->translate(
|
2015-03-11 09:52:14 +01:00
|
|
|
'The name of this authentication provider that is used to differentiate it from others.'
|
2014-10-21 16:15:04 +02:00
|
|
|
)
|
2013-10-23 12:25:51 +02:00
|
|
|
)
|
|
|
|
);
|
2014-09-03 12:21:31 +02:00
|
|
|
$this->addElement(
|
|
|
|
'select',
|
|
|
|
'resource',
|
|
|
|
array(
|
|
|
|
'required' => true,
|
2015-03-11 09:52:14 +01:00
|
|
|
'label' => $this->translate('LDAP Connection'),
|
|
|
|
'description' => $this->translate(
|
|
|
|
'The LDAP connection to use for authenticating with this provider.'
|
|
|
|
),
|
2014-09-03 12:21:31 +02:00
|
|
|
'multiOptions' => false === empty($this->resources)
|
|
|
|
? array_combine($this->resources, $this->resources)
|
|
|
|
: array()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->addElement(
|
|
|
|
'text',
|
|
|
|
'user_class',
|
|
|
|
array(
|
2015-06-05 17:20:31 +02:00
|
|
|
'preserveDefault' => true,
|
|
|
|
'required' => ! $isAd,
|
|
|
|
'ignore' => $isAd,
|
|
|
|
'disabled' => $isAd ?: null,
|
|
|
|
'label' => $this->translate('LDAP User Object Class'),
|
|
|
|
'description' => $this->translate('The object class used for storing users on the LDAP server.'),
|
|
|
|
'value' => $isAd ? 'user' : 'inetOrgPerson'
|
2014-09-03 12:21:31 +02:00
|
|
|
)
|
|
|
|
);
|
2015-03-11 09:52:14 +01:00
|
|
|
$this->addElement(
|
|
|
|
'text',
|
|
|
|
'filter',
|
|
|
|
array(
|
|
|
|
'allowEmpty' => true,
|
|
|
|
'label' => $this->translate('LDAP Filter'),
|
|
|
|
'description' => $this->translate(
|
|
|
|
'An additional filter to use when looking up users using the specified connection. '
|
|
|
|
. 'Leave empty to not to use any additional filter rules.'
|
|
|
|
),
|
|
|
|
'requirement' => $this->translate(
|
|
|
|
'The filter needs to be expressed as standard LDAP expression, without'
|
|
|
|
. ' outer parentheses. (e.g. &(foo=bar)(bar=foo) or foo=bar)'
|
|
|
|
),
|
|
|
|
'validators' => array(
|
|
|
|
array(
|
|
|
|
'Callback',
|
|
|
|
false,
|
|
|
|
array(
|
|
|
|
'callback' => function ($v) {
|
|
|
|
return strpos($v, '(') !== 0;
|
|
|
|
},
|
|
|
|
'messages' => array(
|
|
|
|
'callbackValue' => $this->translate('The filter must not be wrapped in parantheses.')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2014-09-03 12:21:31 +02:00
|
|
|
$this->addElement(
|
|
|
|
'text',
|
|
|
|
'user_name_attribute',
|
|
|
|
array(
|
2015-06-05 17:20:31 +02:00
|
|
|
'preserveDefault' => true,
|
|
|
|
'required' => ! $isAd,
|
|
|
|
'ignore' => $isAd,
|
|
|
|
'disabled' => $isAd ?: null,
|
|
|
|
'label' => $this->translate('LDAP User Name Attribute'),
|
|
|
|
'description' => $this->translate(
|
2015-03-11 09:52:14 +01:00
|
|
|
'The attribute name used for storing the user name on the LDAP server.'
|
2015-01-19 11:26:23 +01:00
|
|
|
),
|
2015-06-05 17:20:31 +02:00
|
|
|
'value' => $isAd ? 'sAMAccountName' : 'uid'
|
2014-09-03 12:21:31 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->addElement(
|
|
|
|
'hidden',
|
|
|
|
'backend',
|
|
|
|
array(
|
2014-11-18 15:06:36 +01:00
|
|
|
'disabled' => true,
|
2015-06-05 17:20:31 +02:00
|
|
|
'value' => $isAd ? 'msldap' : 'ldap'
|
2014-09-03 12:21:31 +02:00
|
|
|
)
|
|
|
|
);
|
2014-10-14 14:44:31 +02:00
|
|
|
$this->addElement(
|
|
|
|
'text',
|
|
|
|
'base_dn',
|
|
|
|
array(
|
|
|
|
'required' => false,
|
2015-03-11 09:52:14 +01:00
|
|
|
'label' => $this->translate('LDAP Base DN'),
|
2015-01-19 11:26:23 +01:00
|
|
|
'description' => $this->translate(
|
2015-03-11 09:52:14 +01:00
|
|
|
'The path where users can be found on the LDAP server. Leave ' .
|
|
|
|
'empty to select all users available using the specified connection.'
|
2015-01-19 11:26:23 +01:00
|
|
|
)
|
2014-10-14 14:44:31 +02:00
|
|
|
)
|
|
|
|
);
|
2014-09-09 13:24:39 +02:00
|
|
|
return $this;
|
2013-08-16 16:24:12 +02:00
|
|
|
}
|
|
|
|
|
2013-08-27 14:37:22 +02:00
|
|
|
/**
|
2015-06-02 09:58:57 +02:00
|
|
|
* Validate that the selected resource is a valid ldap user backend
|
2014-04-16 11:50:58 +02:00
|
|
|
*
|
2014-08-29 15:16:13 +02:00
|
|
|
* @see Form::onSuccess()
|
2013-08-27 14:37:22 +02:00
|
|
|
*/
|
2014-11-14 14:59:12 +01:00
|
|
|
public function onSuccess()
|
2013-08-26 16:56:23 +02:00
|
|
|
{
|
2015-06-02 09:58:57 +02:00
|
|
|
if (false === static::isValidUserBackend($this)) {
|
2014-04-28 16:45:37 +02:00
|
|
|
return false;
|
|
|
|
}
|
2014-08-29 15:16:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate the configuration by creating a backend and requesting the user count
|
|
|
|
*
|
|
|
|
* @param Form $form The form to fetch the configuration values from
|
|
|
|
*
|
|
|
|
* @return bool Whether validation succeeded or not
|
|
|
|
*/
|
2015-06-02 09:58:57 +02:00
|
|
|
public static function isValidUserBackend(Form $form)
|
2014-08-29 15:16:13 +02:00
|
|
|
{
|
2013-08-26 16:56:23 +02:00
|
|
|
try {
|
2015-06-05 17:20:31 +02:00
|
|
|
$ldapUserBackend = UserBackend::create(null, new ConfigObject($form->getValues()));
|
2014-08-29 15:16:13 +02:00
|
|
|
$ldapUserBackend->assertAuthenticationPossible();
|
2014-09-29 11:06:16 +02:00
|
|
|
} catch (AuthenticationException $e) {
|
2015-03-11 09:52:14 +01:00
|
|
|
if (($previous = $e->getPrevious()) !== null) {
|
|
|
|
$form->addError($previous->getMessage());
|
|
|
|
} else {
|
|
|
|
$form->addError($e->getMessage());
|
|
|
|
}
|
|
|
|
|
2014-09-29 11:06:16 +02:00
|
|
|
return false;
|
2014-08-29 15:16:13 +02:00
|
|
|
} catch (Exception $e) {
|
2015-01-19 13:47:53 +01:00
|
|
|
$form->addError(sprintf($form->translate('Unable to validate authentication: %s'), $e->getMessage()));
|
2013-08-26 16:56:23 +02:00
|
|
|
return false;
|
|
|
|
}
|
2014-10-15 08:49:17 +02:00
|
|
|
|
2014-04-16 11:50:58 +02:00
|
|
|
return true;
|
2013-11-06 19:02:30 +01:00
|
|
|
}
|
2014-09-29 11:06:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the configuration for the chosen resource
|
|
|
|
*
|
2014-11-18 13:11:52 +01:00
|
|
|
* @return ConfigObject
|
2015-06-05 17:20:31 +02:00
|
|
|
*
|
|
|
|
* @todo Check whether it's possible to drop this (Or even all occurences!)
|
2014-09-29 11:06:16 +02:00
|
|
|
*/
|
|
|
|
public function getResourceConfig()
|
|
|
|
{
|
|
|
|
return ResourceFactory::getResourceConfig($this->getValue('resource'));
|
|
|
|
}
|
2013-08-16 16:24:12 +02:00
|
|
|
}
|