icingaweb2/application/controllers/AccountController.php
Johannes Rauh 53b01f2915 wip
2026-04-30 10:59:24 +02:00

122 lines
4.2 KiB
PHP

<?php
// SPDX-FileCopyrightText: 2018 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-3.0-or-later
namespace Icinga\Controllers;
use GuzzleHttp\Psr7\ServerRequest;
use Icinga\Application\Config;
use Icinga\Application\Hook\TwoFactorHook;
use Icinga\Authentication\TwoFactorTotp;
use Icinga\Authentication\User\UserBackend;
use Icinga\Common\Database;
use Icinga\Data\ConfigObject;
use Icinga\Exception\ConfigurationError;
use Icinga\Forms\Account\ChangePasswordForm;
use Icinga\Forms\Account\TwoFactorConfigForm;
use Icinga\Forms\Account\TwoFactorChooseMethodForm;
use Icinga\Forms\PreferenceForm;
use Icinga\User\Preferences\PreferencesStore;
use Icinga\Web\Controller;
use ipl\Html\Contract\Form;
/**
* My Account
*/
class AccountController extends Controller
{
use Database;
/**
* {@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'
))
->add(
'devices',
array(
'title' => $this->translate('List of devices you are logged in'),
'label' => $this->translate('My Devices'),
'url' => 'my-devices'
)
);
}
/**
* My account
*/
public function indexAction()
{
$config = Config::app()->getSection('global');
$user = $this->Auth()->getUser();
if ($user->getAdditional('backend_type') === 'db') {
if ($user->can('user/password-change')) {
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;
}
}
}
// #################################################################################### TODO remove this comment
$twoFactorChooseMethodForm = new TwoFactorChooseMethodForm();
$twoFactorChooseMethodForm->handleRequest(ServerRequest::fromGlobals());
$this->view->twoFactorForm = $twoFactorChooseMethodForm;
if (false) {
$twoFactor = TwoFactorTotp::loadFromDb($this->getDb(), $user->getUsername());
if ($twoFactor === null) {
$twoFactor = TwoFactorTotp::generate($user->getUsername());
}
$twoFactorForm = new TwoFactorConfigForm();
$twoFactorForm->setUser($user);
$twoFactorForm->setTwoFactor($twoFactor);
$twoFactorForm->on(Form::ON_SUBMIT, function (TwoFactorConfigForm $form) {
if ($redirectUrl = $form->getRedirectUrl()) {
$this->redirectNow($redirectUrl);
}
});
$twoFactorForm->handleRequest(ServerRequest::fromGlobals());
$this->view->twoFactorForm = $twoFactorForm;
}
// #################################################################################### TODO remove this comment
$form = new PreferenceForm();
$form->setPreferences($user->getPreferences());
if (isset($config->config_resource)) {
$form->setStore(PreferencesStore::create(new ConfigObject(array(
'resource' => $config->config_resource
)), $user));
}
$form->handleRequest();
$this->view->form = $form;
$this->view->title = $this->translate('My Account');
$this->getTabs()->activate('account');
}
}