Skip to content

Commit

Permalink
Fetch users from backends and populate multi-option with usernames
Browse files Browse the repository at this point in the history
refs #6134
  • Loading branch information
majentsch committed May 28, 2014
1 parent 526ca70 commit 288c97b
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 4 deletions.
3 changes: 2 additions & 1 deletion application/controllers/InstallController.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ protected function createWizard()
// t('Authentication') => 'Icinga\Form\Install\AuthenticationPage',
t('Administration') => 'Icinga\Form\Install\AdministrationPage',
// t('Preferences') => 'Icinga\Form\Install\PreferencesPage',
// t('Logging') => 'Icinga\Form\Install\LoggingPage',
t('Logging') => 'Icinga\Form\Install\LoggingPage',
// t('Database Setup') => 'Icinga\Form\Install\DatabasePage',
// t('Summary') => 'Icinga\Form\Install\SummaryPage'

)
);

Expand Down
56 changes: 53 additions & 3 deletions application/forms/Install/AdministrationPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@

namespace Icinga\Form\Install;

use Icinga\Authentication\Backend\DbUserBackend;
use Icinga\Authentication\Backend\LdapUserBackend;
use Icinga\Authentication\UserBackend;
use Icinga\Data\ResourceFactory;
use Icinga\Web\Wizard\Page;
use Zend_Config;
use Icinga\Web\Form;
Expand Down Expand Up @@ -76,12 +80,19 @@ class AdministrationPage extends Page
*/
protected $title = '';

/**
* The authentication-backend to use for fetching available users
*
* @var Zend_Config
*/
private $backendConfiguration = array();

/**
* Contains all available users.
*
* @var array
*/
private $users = array();
private $users;

/**
* Initialise this form.
Expand All @@ -90,6 +101,43 @@ public function init()
{
$this->setName('administration');
$this->title = t('Administration');

}

/**
* Fetch all users from the current Backend.
*/
private function getUsersFromBackend()
{
if (isset($this->users)) {
return $this->users;
}

// TODO: Fetch type of backend from authentication-page configuration (refs #6141)
$databaseConf = $this->wizard->getConfig()->get('authentication');
$this->backendConfiguration = $databaseConf;

switch ($this->authenticationMode) {
case self::AUTHENTICATION_MODE_DATABASE:
// TODO: Use backend from Authentication-Page instead (refs #6141)
$authBackend = new DbUserBackend(
ResourceFactory::createResource(ResourceFactory::getResourceConfig('internal_db'))
);
$this->users = $authBackend->getUsers();
break;

case self::AUTHENTICATION_MODE_LDAP:
// TODO: Use backend from Authentication-Page instead (refs #6141)
// TODO: Use userClass and userNameAttribute from authentication-page configuration refs (#6141)
$authBackend = new LdapUserBackend(
ResourceFactory::createResource(ResourceFactory::getResourceConfig('internal_ldap')),
'inetOrgPerson',
'uid'
);
$this->users = $authBackend->getUsers();
break;
}
return $this->users;
}

/**
Expand All @@ -99,13 +147,15 @@ public function init()
*/
public function create()
{
$users = $this->getUsersFromBackend();
$config = $this->getConfig();

if (empty($users) && $this->authenticationMode === self::AUTHENTICATION_MODE_LDAP) {
$this->addErrorMessage(
t(
'No users available in the given LDAP backend, installation not possible.'
. ' Create a new user .'
. ' Change the used backend configuration in the page "configuration" or add at'
. ' least one user to your ldap backend.'
)
);
return;
Expand All @@ -129,7 +179,7 @@ public function create()
'required' => true,
'disableHidden' => true,
'label' => t('Create User?'),
'helptext' => t('Do you want to give administration privileges to a new user or an existing one?')
'helptext' => t('You can give administration privileges to an existing user or a new one.')
)
);
$this->enableAutoSubmit(array('internal_administrator_select_type'));
Expand Down
14 changes: 14 additions & 0 deletions library/Icinga/Authentication/Backend/DbUserBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,18 @@ public function count()

return ($row !== false) ? $row->count : 0;
}

public function getUsers()
{
$select = new Zend_Db_Select($this->conn->getConnection());
$query = $select->from(
'account',
array('username')
)->query();
$users = array();
while ($username = $query->fetchColumn()) {
$users[] = $username;
}
return $users;
}
}
17 changes: 17 additions & 0 deletions library/Icinga/Authentication/Backend/LdapUserBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,22 @@ public function count()
)
);
}

/**
* Get a list of available users.
*
* @return array
*/
public function getUsers()
{
$users = $this->conn->select()
->from($this->userClass, array($this->userNameAttribute))
->fetchAll();
$usernames = array();
foreach ($users as $userObject) {
$usernames[] = $userObject->uid;
}
return $usernames;
}
}

0 comments on commit 288c97b

Please sign in to comment.