Merge branch 'feature/change-password-10616'

resolves 
This commit is contained in:
Eric Lippmann 2016-08-04 16:09:41 +02:00
commit ecbb0926dc
8 changed files with 216 additions and 78 deletions
application
library/Icinga
Application
Authentication

View File

@ -0,0 +1,73 @@
<?php
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */
namespace Icinga\Controllers;
use Icinga\Application\Config;
use Icinga\Authentication\User\UserBackend;
use Icinga\Data\ConfigObject;
use Icinga\Exception\ConfigurationError;
use Icinga\Forms\Account\ChangePasswordForm;
use Icinga\Forms\PreferenceForm;
use Icinga\User\Preferences\PreferencesStore;
use Icinga\Web\Controller;
/**
* My Account
*/
class AccountController extends Controller
{
/**
* {@inheritdoc}
*/
public function init()
{
$this->getTabs()
->add('account', array(
'title' => $this->translate('Update your account'),
'label' => $this->translate('My Account'),
'url' => 'account'
))
->add('navigation', array(
'title' => $this->translate('List and configure your own navigation items'),
'label' => $this->translate('Navigation'),
'url' => 'navigation'
));
}
/**
* My account
*/
public function indexAction()
{
$config = Config::app()->getSection('global');
$user = $this->Auth()->getUser();
if ($user->getAdditional('backend_type') === 'db') {
try {
$userBackend = UserBackend::create($user->getAdditional('backend_name'));
} catch (ConfigurationError $e) {
$userBackend = null;
}
if ($userBackend !== null) {
$changePasswordForm = new ChangePasswordForm();
$changePasswordForm
->setBackend($userBackend)
->handleRequest();
$this->view->changePasswordForm = $changePasswordForm;
}
}
$form = new PreferenceForm();
$form->setPreferences($user->getPreferences());
if ($config->get('config_backend', 'ini') !== 'none') {
$form->setStore(PreferencesStore::create(new ConfigObject(array(
'store' => $config->get('config_backend', 'ini'),
'resource' => $config->config_resource
)), $user));
}
$form->handleRequest();
$this->view->form = $form;
$this->getTabs()->activate('account');
}
}

View File

@ -128,11 +128,11 @@ class NavigationController extends Controller
$this->getTabs()
->add(
'preferences',
'account',
array(
'title' => $this->translate('Adjust the preferences of Icinga Web 2 according to your needs'),
'label' => $this->translate('Preferences'),
'url' => 'preference'
'title' => $this->translate('Update your account'),
'label' => $this->translate('My Account'),
'url' => 'account'
)
)
->add(

View File

@ -1,65 +0,0 @@
<?php
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */
namespace Icinga\Controllers;
use Icinga\Application\Config;
use Icinga\Data\ConfigObject;
use Icinga\Forms\PreferenceForm;
use Icinga\User\Preferences\PreferencesStore;
use Icinga\Web\Controller\BasePreferenceController;
use Icinga\Web\Url;
use Icinga\Web\Widget\Tab;
/**
* Application wide preference controller for user preferences
*
* @TODO(el): Rename to PreferencesController: https://dev.icinga.org/issues/10014
*/
class PreferenceController extends BasePreferenceController
{
/**
* Create tabs for this preference controller
*
* @return array
*
* @see BasePreferenceController::createProvidedTabs()
*/
public static function createProvidedTabs()
{
return array(
'preferences' => new Tab(array(
'title' => t('Adjust the preferences of Icinga Web 2 according to your needs'),
'label' => t('Preferences'),
'url' => 'preference'
)),
'navigation' => new Tab(array(
'title' => t('List and configure your own navigation items'),
'label' => t('Navigation'),
'url' => 'navigation'
))
);
}
/**
* Show form to adjust user preferences
*/
public function indexAction()
{
$config = Config::app()->getSection('global');
$user = $this->getRequest()->getUser();
$form = new PreferenceForm();
$form->setPreferences($user->getPreferences());
if ($config->get('config_backend', 'ini') !== 'none') {
$form->setStore(PreferencesStore::create(new ConfigObject(array(
'store' => $config->get('config_backend', 'ini'),
'resource' => $config->config_resource
)), $user));
}
$form->handleRequest();
$this->view->form = $form;
$this->getTabs()->activate('preferences');
}
}

View File

@ -0,0 +1,123 @@
<?php
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */
namespace Icinga\Forms\Account;
use Icinga\Authentication\User\DbUserBackend;
use Icinga\Data\Filter\Filter;
use Icinga\User;
use Icinga\Web\Form;
use Icinga\Web\Notification;
/**
* Form for changing user passwords
*/
class ChangePasswordForm extends Form
{
/**
* The user backend
*
* @var DbUserBackend
*/
protected $backend;
/**
* {@inheritdoc}
*/
public function init()
{
$this->setSubmitLabel($this->translate('Update Account'));
}
/**
* {@inheritdoc}
*/
public function createElements(array $formData)
{
$this->addElement(
'password',
'old_password',
array(
'label' => $this->translate('Old Password'),
'required' => true
)
);
$this->addElement(
'password',
'new_password',
array(
'label' => $this->translate('New Password'),
'required' => true
)
);
$this->addElement(
'password',
'new_password_confirmation',
array(
'label' => $this->translate('Confirm New Password'),
'required' => true,
'validators' => array(
array('identical', false, array('new_password'))
)
)
);
}
/**
* {@inheritdoc}
*/
public function onSuccess()
{
$backend = $this->getBackend();
$backend->update(
$backend->getBaseTable(),
array('password' => $this->getElement('new_password')->getValue()),
Filter::where('user_name', $this->Auth()->getUser()->getUsername())
);
Notification::success($this->translate('Account updated'));
}
/**
* {@inheritdoc}
*/
public function isValid($formData)
{
$valid = parent::isValid($formData);
if (! $valid) {
return false;
}
$oldPasswordEl = $this->getElement('old_password');
if (! $this->backend->authenticate($this->Auth()->getUser(), $oldPasswordEl->getValue())) {
$oldPasswordEl->addError($this->translate('Old password is invalid'));
$this->markAsError();
return false;
}
return true;
}
/**
* Get the user backend
*
* @return DbUserBackend
*/
public function getBackend()
{
return $this->backend;
}
/**
* Set the user backend
*
* @param DbUserBackend $backend
*
* @return $this
*/
public function setBackend(DbUserBackend $backend)
{
$this->backend = $backend;
return $this;
}
}

View File

@ -0,0 +1,11 @@
<div class="controls">
<?= $tabs ?>
</div>
<div class="content">
<?php if (isset($changePasswordForm)): ?>
<h1><?= $this->translate('Account') ?></h1>
<?= $changePasswordForm ?>
<?php endif ?>
<h1><?= $this->translate('Preferences') ?></h1>
<?= $form ?>
</div>

View File

@ -1,6 +0,0 @@
<div class="controls">
<?= $tabs; ?>
</div>
<div class="content">
<?= $form; ?>
</div>

View File

@ -346,10 +346,10 @@ class Web extends EmbeddedWeb
'icon' => 'user',
'priority' => 900,
'children' => array(
'preferences' => array(
'label' => t('Preferences'),
'account' => array(
'label' => t('My Account'),
'priority' => 100,
'url' => 'preference'
'url' => 'account'
),
'logout' => array(
'label' => t('Logout'),

View File

@ -118,6 +118,8 @@ class AuthChain implements Authenticatable, Iterator
continue;
}
if ($authenticated) {
$user->setAdditional('backend_name', $backend->getName());
$user->setAdditional('backend_type', $this->config->current()->get('backend'));
return true;
}
}