AdminAccountPage: Use a select input to choose how to define a user

refs #8704
This commit is contained in:
Johannes Meyer 2015-06-30 10:58:17 +02:00
parent 2158b2701c
commit c787eadcfe
1 changed files with 69 additions and 94 deletions

View File

@ -35,7 +35,9 @@ class AdminAccountPage extends Form
public function init() public function init()
{ {
$this->setName('setup_admin_account'); $this->setName('setup_admin_account');
$this->setViewScript('form/setup-admin-account.phtml'); $this->addDescription($this->translate(
'Now it\'s time to configure your first administrative account for Icinga Web 2.'
));
} }
/** /**
@ -70,14 +72,53 @@ class AdminAccountPage extends Form
public function createElements(array $formData) public function createElements(array $formData)
{ {
$choices = array(); $choices = array();
if ($this->backendConfig['backend'] !== 'db') { if ($this->backendConfig['backend'] !== 'db') {
$choices['by_name'] = $this->translate('By Name', 'setup.admin'); $choices['by_name'] = $this->translate('By Name', 'setup.admin');
$choice = isset($formData['user_type']) ? $formData['user_type'] : 'by_name';
} else {
$choices['new_user'] = $this->translate('New User', 'setup.admin');
$choice = isset($formData['user_type']) ? $formData['user_type'] : 'new_user';
}
if (in_array($this->backendConfig['backend'], array('db', 'ldap', 'msldap'))) {
$users = $this->fetchUsers();
if (! empty($users)) {
$choices['existing_user'] = $this->translate('Existing User', 'setup.admin');
}
}
if (count($choices) > 1) {
$this->addDescription($this->translate(
'Below are several options you can choose from for how to define the desired account.'
));
$this->addElement(
'select',
'user_type',
array(
'required' => true,
'autosubmit' => true,
'label' => $this->translate('Type Of Definition'),
'multiOptions' => $choices,
'value' => $choice
)
);
} else {
$this->addElement(
'hidden',
'user_type',
array(
'required' => true,
'value' => key($choices)
)
);
}
if ($choice === 'by_name') {
$this->addElement( $this->addElement(
'text', 'text',
'by_name', 'by_name',
array( array(
'required' => !isset($formData['user_type']) || $formData['user_type'] === 'by_name', 'required' => true,
'value' => $this->getUsername(), 'value' => $this->getUsername(),
'label' => $this->translate('Username'), 'label' => $this->translate('Username'),
'description' => $this->translate( 'description' => $this->translate(
@ -86,53 +127,35 @@ class AdminAccountPage extends Form
) )
) )
); );
if (! $this->request->isXmlHttpRequest()) {
// In case JS is disabled we must not provide client side validation as
// the user is required to input data even he has changed his mind
$this->getElement('by_name')->setAttrib('required', null);
$this->getElement('by_name')->setAttrib('aria-required', null);
}
} }
if (in_array($this->backendConfig['backend'], array('db', 'ldap', 'msldap'))) { if ($choice === 'existing_user') {
$users = $this->fetchUsers(); $this->addElement(
if (false === empty($users)) { 'select',
$choices['existing_user'] = $this->translate('Existing User'); 'existing_user',
$this->addElement( array(
'select', 'required' => true,
'existing_user', 'label' => $this->translate('Username'),
array( 'description' => sprintf(
'required' => isset($formData['user_type']) && $formData['user_type'] === 'existing_user', $this->translate(
'label' => $this->translate('Username'), 'Choose a user reported by the %s backend as the initial administrative account',
'description' => sprintf( 'setup.admin'
$this->translate(
'Choose a user reported by the %s backend as the initial administrative account',
'setup.admin'
),
$this->backendConfig['backend'] === 'db'
? $this->translate('database', 'setup.admin.authbackend')
: 'LDAP'
), ),
'multiOptions' => array_combine($users, $users) $this->backendConfig['backend'] === 'db'
) ? $this->translate('database', 'setup.admin.authbackend')
); : 'LDAP'
if (! $this->request->isXmlHttpRequest()) { ),
// In case JS is disabled we must not provide client side validation as 'multiOptions' => array_combine($users, $users)
// the user is required to input data even he has changed his mind )
$this->getElement('existing_user')->setAttrib('required', null); );
$this->getElement('existing_user')->setAttrib('aria-required', null);
}
}
} }
if ($this->backendConfig['backend'] === 'db') { if ($choice === 'new_user') {
$choices['new_user'] = $this->translate('New User');
$required = isset($formData['user_type']) && $formData['user_type'] === 'new_user';
$this->addElement( $this->addElement(
'text', 'text',
'new_user', 'new_user',
array( array(
'required' => $required, 'required' => true,
'label' => $this->translate('Username'), 'label' => $this->translate('Username'),
'description' => $this->translate( 'description' => $this->translate(
'Enter the username to be used when creating an initial administrative account' 'Enter the username to be used when creating an initial administrative account'
@ -143,7 +166,7 @@ class AdminAccountPage extends Form
'password', 'password',
'new_user_password', 'new_user_password',
array( array(
'required' => $required, 'required' => true,
'label' => $this->translate('Password'), 'label' => $this->translate('Password'),
'description' => $this->translate('Enter the password to assign to the newly created account') 'description' => $this->translate('Enter the password to assign to the newly created account')
) )
@ -152,44 +175,16 @@ class AdminAccountPage extends Form
'password', 'password',
'new_user_2ndpass', 'new_user_2ndpass',
array( array(
'required' => $required, 'required' => true,
'label' => $this->translate('Repeat password'), 'label' => $this->translate('Repeat password'),
'description' => $this->translate('Please repeat the password given above to avoid typing errors'), 'description' => $this->translate(
'Please repeat the password given above to avoid typing errors'
),
'validators' => array( 'validators' => array(
array('identical', false, array('new_user_password')) array('identical', false, array('new_user_password'))
) )
) )
); );
if (! $this->request->isXmlHttpRequest()) {
// In case JS is disabled we must not provide client side validation as
// the user is required to input data even he has changed his mind
foreach (array('new_user', 'new_user_password', 'new_user_2ndpass') as $elementName) {
$this->getElement($elementName)->setAttrib('aria-required', null);
$this->getElement($elementName)->setAttrib('required', null);
}
}
}
if (count($choices) > 1) {
$this->addElement(
'radio',
'user_type',
array(
'required' => true,
'autosubmit' => true,
'value' => key($choices),
'multiOptions' => $choices
)
);
} else {
$this->addElement(
'hidden',
'user_type',
array(
'required' => true,
'value' => key($choices)
)
);
} }
} }
@ -214,26 +209,6 @@ class AdminAccountPage extends Form
return true; return true;
} }
/**
* Return whether the given values (possibly incomplete) are valid
*
* Unsets all empty text-inputs so that they are not being validated when auto-submitting the form.
*
* @param array $formData
*
* @return type
*/
public function isValidPartial(array $formData)
{
foreach (array('by_name', 'new_user', 'new_user_password', 'new_user_2ndpass') as $elementName) {
if (isset($formData[$elementName]) && $formData[$elementName] === '') {
unset($formData[$elementName]);
}
}
return parent::isValidPartial($formData);
}
/** /**
* Return the name of the externally authenticated user * Return the name of the externally authenticated user
* *