diff --git a/application/controllers/AccountController.php b/application/controllers/AccountController.php new file mode 100644 index 000000000..25bc97756 --- /dev/null +++ b/application/controllers/AccountController.php @@ -0,0 +1,73 @@ +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'); + } +} diff --git a/application/controllers/NavigationController.php b/application/controllers/NavigationController.php index 51b5dae1c..221ff39f4 100644 --- a/application/controllers/NavigationController.php +++ b/application/controllers/NavigationController.php @@ -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( diff --git a/application/controllers/PreferenceController.php b/application/controllers/PreferenceController.php deleted file mode 100644 index bb4655c27..000000000 --- a/application/controllers/PreferenceController.php +++ /dev/null @@ -1,65 +0,0 @@ - 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'); - } -} diff --git a/application/forms/Account/ChangePasswordForm.php b/application/forms/Account/ChangePasswordForm.php new file mode 100644 index 000000000..60c58604b --- /dev/null +++ b/application/forms/Account/ChangePasswordForm.php @@ -0,0 +1,123 @@ +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; + } +} diff --git a/application/views/scripts/account/index.phtml b/application/views/scripts/account/index.phtml new file mode 100644 index 000000000..efc2bcbf6 --- /dev/null +++ b/application/views/scripts/account/index.phtml @@ -0,0 +1,11 @@ +
+ +
+
+ +

translate('Account') ?>

+ + +

translate('Preferences') ?>

+ +
diff --git a/application/views/scripts/preference/index.phtml b/application/views/scripts/preference/index.phtml deleted file mode 100644 index 4ca5e6821..000000000 --- a/application/views/scripts/preference/index.phtml +++ /dev/null @@ -1,6 +0,0 @@ -
- -
-
- -
\ No newline at end of file diff --git a/library/Icinga/Application/Web.php b/library/Icinga/Application/Web.php index 7af9b9b8f..df5cbf729 100644 --- a/library/Icinga/Application/Web.php +++ b/library/Icinga/Application/Web.php @@ -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'), diff --git a/library/Icinga/Authentication/AuthChain.php b/library/Icinga/Authentication/AuthChain.php index c2cf8b9da..396e947a1 100644 --- a/library/Icinga/Authentication/AuthChain.php +++ b/library/Icinga/Authentication/AuthChain.php @@ -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; } }