AuthBackendPage: set the subform's defaults before creating it

refs #2990
This commit is contained in:
Alexander A. Klimov 2017-10-06 13:26:21 +02:00
parent 4603f0657d
commit b7b7796e34
2 changed files with 56 additions and 15 deletions

View File

@ -22,6 +22,13 @@ class LdapBackendForm extends Form
*/
protected $resources;
/**
* Default values for the form elements
*
* @var string[]
*/
protected $suggestions = array();
/**
* Initialize this form
*/
@ -60,7 +67,8 @@ class LdapBackendForm extends Form
'label' => $this->translate('Backend Name'),
'description' => $this->translate(
'The name of this authentication provider that is used to differentiate it from others.'
)
),
'value' => $this->getSuggestion('name')
)
);
$this->addElement(
@ -74,7 +82,8 @@ class LdapBackendForm extends Form
),
'multiOptions' => !empty($this->resources)
? array_combine($this->resources, $this->resources)
: array()
: array(),
'value' => $this->getSuggestion('resource')
)
);
@ -141,7 +150,7 @@ class LdapBackendForm extends Form
'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' => $userClass
'value' => $this->getSuggestion('user_class', $userClass)
)
);
$this->addElement(
@ -150,7 +159,7 @@ class LdapBackendForm extends Form
array(
'preserveDefault' => true,
'allowEmpty' => true,
'value' => $filter,
'value' => $this->getSuggestion('filter', $filter),
'label' => $this->translate('LDAP Filter'),
'description' => $this->translate(
'An additional filter to use when looking up users using the specified connection. '
@ -193,7 +202,7 @@ class LdapBackendForm extends Form
'description' => $this->translate(
'The attribute name used for storing the user name on the LDAP server.'
),
'value' => $userNameAttribute
'value' => $this->getSuggestion('user_name_attribute', $userNameAttribute)
)
);
$this->addElement(
@ -201,7 +210,7 @@ class LdapBackendForm extends Form
'backend',
array(
'disabled' => true,
'value' => $isAd ? 'msldap' : 'ldap'
'value' => $this->getSuggestion('backend', $isAd ? 'msldap' : 'ldap')
)
);
$this->addElement(
@ -215,7 +224,7 @@ class LdapBackendForm extends Form
'The path where users can be found on the LDAP server. Leave ' .
'empty to select all users available using the specified connection.'
),
'value' => $baseDn
'value' => $this->getSuggestion('base_dn', $baseDn)
)
);
@ -233,7 +242,8 @@ class LdapBackendForm extends Form
. ' If your LDAP backend holds usernames with a domain part or if it is not necessary in your setup'
. ' to authenticate users based on their domains, leave this field empty.'
),
'preserveDefault' => true
'preserveDefault' => true,
'value' => $this->getSuggestion('domain')
)
);
@ -318,4 +328,41 @@ class LdapBackendForm extends Form
}
}
}
/**
* Get the default values for the form elements
*
* @return string[]
*/
public function getSuggestions()
{
return $this->suggestions;
}
/**
* Get the default value for the given form element or the given default
*
* @param string $element
* @param string $default
*
* @return string
*/
public function getSuggestion($element, $default = null)
{
return isset($this->suggestions[$element]) ? $this->suggestions[$element] : $default;
}
/**
* Set the default values for the form elements
*
* @param string[] $suggestions
*
* @return $this
*/
public function setSuggestions(array $suggestions)
{
$this->suggestions = $suggestions;
return $this;
}
}

View File

@ -85,6 +85,7 @@ class AuthBackendPage extends Form
}
$backendForm = new LdapBackendForm();
$backendForm->setSuggestions($this->suggestions);
$backendForm->setResources(array($this->config['name']));
$backendForm->create($formData);
$backendForm->getElement('resource')->setIgnore(true);
@ -110,13 +111,6 @@ class AuthBackendPage extends Form
'value' => $type
)
);
foreach ($this->suggestions as $key => $suggestion) {
$element = $backendForm->getElement($key);
if ($element !== null) {
$element->setValue($suggestion);
}
}
} else { // $this->config['type'] === 'external'
$backendForm = new ExternalBackendForm();
$backendForm->create($formData);