Skip to content

Commit

Permalink
UserController: Add backend selection control
Browse files Browse the repository at this point in the history
refs #8826
  • Loading branch information
Johannes Meyer committed May 8, 2015
1 parent f383ddd commit 1e1b954
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 15 deletions.
67 changes: 53 additions & 14 deletions application/controllers/UserController.php
Expand Up @@ -5,8 +5,8 @@
use Icinga\Application\Config;
use Icinga\Authentication\User\UserBackend;
use Icinga\Authentication\User\UserBackendInterface;
use Icinga\Data\Selectable;
use Icinga\Web\Controller;
use Icinga\Web\Form;
use Icinga\Web\Widget;

class UserController extends Controller
Expand All @@ -32,6 +32,26 @@ public function indexAction()
*/
public function listAction()
{
$backendNames = array_map(
function ($b) { return $b->getName(); },
$this->loadUserBackends('Icinga\Data\Selectable')
);
$this->view->backendSelection = new Form();
$this->view->backendSelection->setAttrib('class', 'backend-selection');
$this->view->backendSelection->setUidDisabled();
$this->view->backendSelection->setMethod('GET');
$this->view->backendSelection->setTokenDisabled();
$this->view->backendSelection->addElement(
'select',
'backend',
array(
'autosubmit' => true,
'label' => $this->translate('Authentication Backend'),
'multiOptions' => array_combine($backendNames, $backendNames),
'value' => $this->params->get('backend')
)
);

$backend = $this->getUserBackend($this->params->get('backend'));
if ($backend === null) {
$this->view->backend = null;
Expand Down Expand Up @@ -67,43 +87,62 @@ public function listAction()
));
}

/**
* Return all user backends implementing the given interface
*
* @param string $interface The class path of the interface, or null if no interface check should be made
*
* @return array
*/
protected function loadUserBackends($interface = null)
{
$backends = array();
foreach (Config::app('authentication') as $backendName => $backendConfig) {
$candidate = UserBackend::create($backendName, $backendConfig);
if (! $interface || $candidate instanceof $interface) {
$backends[] = $candidate;
}
}

return $backends;
}

/**
* Return the given user backend or the first match in order
*
* @param string $name The name of the backend, or null in case the first match should be returned
* @param bool $selectable Whether the backend should implement the Selectable interface
* @param string $interface The interface the backend should implement, no interface check if null
*
* @return UserBackendInterface
*
* @throws Zend_Controller_Action_Exception In case the given backend name is invalid
*/
protected function getUserBackend($name = null, $selectable = true)
protected function getUserBackend($name = null, $interface = 'Icinga\Data\Selectable')
{
$config = Config::app('authentication');
if ($name !== null) {
$config = Config::app('authentication');
if (! $config->hasSection($name)) {
throw new Zend_Controller_Action_Exception(
sprintf($this->translate('Authentication backend "%s" not found'), $name),
404
);
} else {
$backend = UserBackend::create($name, $config->getSection($name));
if ($selectable && !$backend instanceof Selectable) {
if ($interface && !$backend instanceof $interface) {
$interfaceParts = explode('\\', strtolower($interface));
throw new Zend_Controller_Action_Exception(
sprintf($this->translate('Authentication backend "%s" is not able to list users'), $name),
sprintf(
$this->translate('Authentication backend "%s" is not %s'),
$name,
array_pop($interfaceParts)
),
400
);
}
}
} else {
$backend = null;
foreach ($config as $backendName => $backendConfig) {
$candidate = UserBackend::create($backendName, $backendConfig);
if (! $selectable || $candidate instanceof Selectable) {
$backend = $candidate;
break;
}
}
$backends = $this->loadUserBackends($interface);
$backend = array_shift($backends);
}

return $backend;
Expand Down
5 changes: 4 additions & 1 deletion application/views/scripts/user/list.phtml
Expand Up @@ -4,7 +4,10 @@
<?= $this->sortBox; ?>
<?= $this->limiter; ?>
<?= $this->paginator; ?>
<?= $this->filterEditor; ?>
<div>
<?= $this->backendSelection; ?>
<?= $this->filterEditor; ?>
</div>
</div>
<?php endif ?>
<div class="content">
Expand Down
18 changes: 18 additions & 0 deletions public/css/icinga/main-content.less
Expand Up @@ -241,4 +241,22 @@ table.group-list {
td.group-parent, td.group-created, td.group-modified {
text-align: right;
}
}

form.backend-selection {
float: right;

div.element {
margin: 0;

label {
width: auto;
margin-right: 0.5em;
}

select {
width: 11.5em;
margin-left: 0;
}
}
}

0 comments on commit 1e1b954

Please sign in to comment.