2013-06-14 13:51:44 +02:00
|
|
|
<?php
|
2016-02-08 15:41:00 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */
|
2013-06-14 13:51:44 +02:00
|
|
|
|
2015-08-27 13:09:58 +02:00
|
|
|
namespace Icinga\Controllers;
|
2013-06-14 13:51:44 +02:00
|
|
|
|
2018-03-28 14:42:48 +02:00
|
|
|
use Icinga\Application\Hook\AuthenticationHook;
|
2014-12-29 14:30:47 +01:00
|
|
|
use Icinga\Application\Icinga;
|
|
|
|
use Icinga\Forms\Authentication\LoginForm;
|
2015-07-29 14:17:07 +02:00
|
|
|
use Icinga\Web\Controller;
|
2015-11-27 15:35:39 +01:00
|
|
|
use Icinga\Web\Helper\CookieHelper;
|
2014-03-06 12:07:24 +01:00
|
|
|
use Icinga\Web\Url;
|
2013-07-12 16:10:56 +02:00
|
|
|
|
2013-06-14 13:51:44 +02:00
|
|
|
/**
|
2013-08-16 14:56:23 +02:00
|
|
|
* Application wide controller for authentication
|
2013-06-14 13:51:44 +02:00
|
|
|
*/
|
2015-07-29 14:17:07 +02:00
|
|
|
class AuthenticationController extends Controller
|
2013-06-14 13:51:44 +02:00
|
|
|
{
|
|
|
|
/**
|
2015-08-13 08:12:30 +02:00
|
|
|
* {@inheritdoc}
|
2013-06-14 13:51:44 +02:00
|
|
|
*/
|
2013-08-30 15:50:49 +02:00
|
|
|
protected $requiresAuthentication = false;
|
2013-06-14 13:51:44 +02:00
|
|
|
|
2015-08-13 08:12:30 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
protected $innerLayout = 'inline';
|
|
|
|
|
2013-06-14 13:51:44 +02:00
|
|
|
/**
|
2013-08-16 14:56:23 +02:00
|
|
|
* Log into the application
|
2013-06-14 13:51:44 +02:00
|
|
|
*/
|
|
|
|
public function loginAction()
|
|
|
|
{
|
2014-12-29 14:30:06 +01:00
|
|
|
$icinga = Icinga::app();
|
2015-07-29 14:17:07 +02:00
|
|
|
if (($requiresSetup = $icinga->requiresSetup()) && $icinga->setupTokenExists()) {
|
2014-11-18 13:13:02 +01:00
|
|
|
$this->redirectNow(Url::fromPath('setup'));
|
2014-09-10 14:48:33 +02:00
|
|
|
}
|
2015-07-29 14:17:07 +02:00
|
|
|
$form = new LoginForm();
|
|
|
|
if ($this->Auth()->isAuthenticated()) {
|
2018-03-28 14:42:48 +02:00
|
|
|
// Call provided AuthenticationHook(s) when login action is called
|
|
|
|
// but icinga web user is already authenticated
|
|
|
|
AuthenticationHook::triggerLogin($this->Auth()->getUser());
|
2015-07-29 14:17:07 +02:00
|
|
|
$this->redirectNow($form->getRedirectUrl());
|
2013-06-24 18:46:45 +02:00
|
|
|
}
|
2015-07-29 14:17:07 +02:00
|
|
|
if (! $requiresSetup) {
|
2015-11-27 15:35:39 +01:00
|
|
|
$cookies = new CookieHelper($this->getRequest());
|
|
|
|
if (! $cookies->isSupported()) {
|
2015-08-27 13:21:43 +02:00
|
|
|
$this
|
|
|
|
->getResponse()
|
|
|
|
->setBody("Cookies must be enabled to run this application.\n")
|
|
|
|
->setHttpResponseCode(403)
|
|
|
|
->sendResponse();
|
2015-11-27 15:35:39 +01:00
|
|
|
exit;
|
2015-08-13 11:21:05 +02:00
|
|
|
}
|
2015-07-29 14:17:07 +02:00
|
|
|
$form->handleRequest();
|
|
|
|
}
|
|
|
|
$this->view->form = $form;
|
|
|
|
$this->view->title = $this->translate('Icinga Web 2 Login');
|
|
|
|
$this->view->requiresSetup = $requiresSetup;
|
2013-06-14 13:51:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-08-16 14:56:23 +02:00
|
|
|
* Log out the current user
|
2013-06-14 13:51:44 +02:00
|
|
|
*/
|
|
|
|
public function logoutAction()
|
|
|
|
{
|
2014-06-22 20:08:55 +02:00
|
|
|
$auth = $this->Auth();
|
2014-10-01 08:13:17 +02:00
|
|
|
if (! $auth->isAuthenticated()) {
|
|
|
|
$this->redirectToLogin();
|
|
|
|
}
|
2015-07-29 15:44:32 +02:00
|
|
|
// Get info whether the user is externally authenticated before removing authorization which destroys the
|
|
|
|
// session and the user object
|
|
|
|
$isExternalUser = $auth->getUser()->isExternalUser();
|
2018-03-28 14:42:48 +02:00
|
|
|
// Call provided AuthenticationHook(s) when logout action is called
|
|
|
|
AuthenticationHook::triggerLogout($auth->getUser());
|
2013-06-24 18:46:45 +02:00
|
|
|
$auth->removeAuthorization();
|
2015-07-29 15:44:32 +02:00
|
|
|
if ($isExternalUser) {
|
2015-07-29 14:17:07 +02:00
|
|
|
$this->getResponse()->setHttpResponseCode(401);
|
2014-02-26 17:36:20 +01:00
|
|
|
} else {
|
2014-10-01 08:13:17 +02:00
|
|
|
$this->redirectToLogin();
|
2014-02-26 17:36:20 +01:00
|
|
|
}
|
2013-06-14 13:51:44 +02:00
|
|
|
}
|
|
|
|
}
|