2013-06-07 17:30:18 +02:00
|
|
|
<?php
|
2013-06-10 17:03:01 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-06-28 16:47:30 +02:00
|
|
|
/**
|
2013-10-23 15:10:33 +02:00
|
|
|
* This file is part of Icinga Web 2.
|
2013-09-04 18:27:16 +02:00
|
|
|
*
|
2013-10-23 15:10:33 +02:00
|
|
|
* Icinga Web 2 - Head for multiple monitoring backends.
|
2014-01-23 12:09:48 +01:00
|
|
|
* Copyright (C) 2014 Icinga Development Team
|
2013-09-04 18:27:16 +02:00
|
|
|
*
|
2013-06-28 16:47:30 +02:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2013-09-04 18:27:16 +02:00
|
|
|
*
|
2013-06-28 16:47:30 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2013-09-04 18:27:16 +02:00
|
|
|
*
|
2013-06-28 16:47:30 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2013-09-04 18:27:16 +02:00
|
|
|
*
|
2014-01-23 12:09:48 +01:00
|
|
|
* @copyright 2014 Icinga Development Team <info@icinga.org>
|
2013-10-23 15:10:33 +02:00
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
|
|
|
|
* @author Icinga Development Team <info@icinga.org>
|
|
|
|
*
|
2013-06-28 16:47:30 +02:00
|
|
|
*/
|
2013-06-10 17:03:01 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-06-07 17:30:18 +02:00
|
|
|
|
|
|
|
namespace Icinga\Authentication;
|
|
|
|
|
2014-02-14 17:28:11 +01:00
|
|
|
use Exception;
|
|
|
|
use Zend_Config;
|
2014-01-23 12:09:48 +01:00
|
|
|
use Icinga\User;
|
|
|
|
use Icinga\Web\Session;
|
2014-02-26 11:19:52 +01:00
|
|
|
use Icinga\Logger\Logger;
|
2014-02-20 13:53:28 +01:00
|
|
|
use Icinga\Exception\NotReadableError;
|
2014-01-23 12:09:48 +01:00
|
|
|
use Icinga\Application\Config as IcingaConfig;
|
2014-02-14 17:28:11 +01:00
|
|
|
use Icinga\User\Preferences;
|
|
|
|
use Icinga\User\Preferences\PreferencesStore;
|
2013-11-06 10:20:15 +01:00
|
|
|
|
2013-06-07 17:30:18 +02:00
|
|
|
class Manager
|
|
|
|
{
|
2013-08-28 10:16:18 +02:00
|
|
|
/**
|
|
|
|
* Singleton instance
|
|
|
|
*
|
|
|
|
* @var self
|
|
|
|
*/
|
2014-01-23 12:09:48 +01:00
|
|
|
private static $instance;
|
2013-06-10 13:28:54 +02:00
|
|
|
|
2014-02-26 17:36:20 +01:00
|
|
|
/**
|
2014-03-03 17:21:17 +01:00
|
|
|
* Authenticated user
|
2013-08-28 10:16:18 +02:00
|
|
|
*
|
2013-08-13 18:08:21 +02:00
|
|
|
* @var User
|
|
|
|
**/
|
2014-01-23 12:09:48 +01:00
|
|
|
private $user;
|
2013-07-12 15:37:36 +02:00
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
2014-03-03 19:03:39 +01:00
|
|
|
* If the user was authenticated from the REMOTE_USER server variable
|
2014-01-22 12:50:17 +01:00
|
|
|
*
|
|
|
|
* @var Boolean
|
|
|
|
*/
|
2014-03-03 19:03:39 +01:00
|
|
|
private $fromRemoteUser = false;
|
2014-01-22 12:50:17 +01:00
|
|
|
|
2014-03-03 17:21:17 +01:00
|
|
|
private function __construct()
|
2013-06-07 17:30:18 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
2014-02-14 12:11:49 +01:00
|
|
|
* Get the authentication manager
|
2013-08-28 10:16:18 +02:00
|
|
|
*
|
2014-03-03 17:21:17 +01:00
|
|
|
* @return self
|
2013-08-28 10:16:18 +02:00
|
|
|
*/
|
2014-03-03 17:21:17 +01:00
|
|
|
public static function getInstance()
|
2013-06-07 17:30:18 +02:00
|
|
|
{
|
|
|
|
if (self::$instance === null) {
|
2014-03-03 17:21:17 +01:00
|
|
|
self::$instance = new static();
|
2013-06-07 17:30:18 +02:00
|
|
|
}
|
|
|
|
return self::$instance;
|
|
|
|
}
|
|
|
|
|
2014-03-03 17:21:17 +01:00
|
|
|
public function setAuthenticated(User $user, $persist = true)
|
2013-08-13 18:08:21 +02:00
|
|
|
{
|
2014-03-03 17:21:17 +01:00
|
|
|
$username = $user->getUsername();
|
2013-08-13 18:08:21 +02:00
|
|
|
try {
|
2014-03-03 17:21:17 +01:00
|
|
|
$config = IcingaConfig::app();
|
|
|
|
} catch (NotReadableError $e) {
|
2014-03-03 19:03:39 +01:00
|
|
|
Logger::error(
|
2014-03-03 17:21:17 +01:00
|
|
|
new Exception('Cannot load preferences for user "' . $username . '". An exception was thrown', 0, $e)
|
2014-02-21 10:16:16 +01:00
|
|
|
);
|
2014-03-03 17:21:17 +01:00
|
|
|
$config = new Zend_Config(array());
|
2013-08-13 18:08:21 +02:00
|
|
|
}
|
2014-03-03 17:21:17 +01:00
|
|
|
if (($preferencesConfig = $config->preferences) !== null) {
|
2013-08-28 10:16:18 +02:00
|
|
|
try {
|
2014-03-03 17:21:17 +01:00
|
|
|
$preferencesStore = PreferencesStore::create(
|
|
|
|
$preferencesConfig,
|
|
|
|
$user
|
2013-08-28 10:16:18 +02:00
|
|
|
);
|
2014-03-03 17:21:17 +01:00
|
|
|
$preferences = new Preferences($preferencesStore->load());
|
|
|
|
} catch (NotReadableError $e) {
|
2013-08-28 10:16:18 +02:00
|
|
|
Logger::error(
|
2014-03-03 17:21:17 +01:00
|
|
|
new Exception(
|
|
|
|
'Cannot load preferences for user "' . $username . '". An exception was thrown', 0, $e
|
|
|
|
)
|
2013-08-28 10:16:18 +02:00
|
|
|
);
|
2014-03-03 17:21:17 +01:00
|
|
|
$preferences = new Preferences();
|
2013-08-28 10:16:18 +02:00
|
|
|
}
|
2014-03-03 17:21:17 +01:00
|
|
|
} else {
|
|
|
|
$preferences = new Preferences();
|
2013-08-28 10:16:18 +02:00
|
|
|
}
|
2014-03-03 17:21:17 +01:00
|
|
|
$user->setPreferences($preferences);
|
2014-02-12 17:01:11 +01:00
|
|
|
$membership = new Membership();
|
|
|
|
$groups = $membership->getGroupsByUsername($username);
|
2014-02-18 09:33:33 +01:00
|
|
|
$user->setGroups($groups);
|
2014-02-12 17:01:11 +01:00
|
|
|
$admissionLoader = new AdmissionLoader();
|
2014-02-18 09:33:33 +01:00
|
|
|
$user->setPermissions(
|
2014-02-12 17:01:11 +01:00
|
|
|
$admissionLoader->getPermissions($username, $groups)
|
|
|
|
);
|
2014-02-18 09:33:33 +01:00
|
|
|
$user->setRestrictions(
|
2014-02-12 17:01:11 +01:00
|
|
|
$admissionLoader->getRestrictions($username, $groups)
|
|
|
|
);
|
2014-02-18 09:33:33 +01:00
|
|
|
$this->user = $user;
|
2013-06-10 13:28:54 +02:00
|
|
|
if ($persist == true) {
|
2014-02-26 19:23:34 +01:00
|
|
|
$session = Session::getSession();
|
|
|
|
$session->refreshId();
|
2013-06-10 13:28:54 +02:00
|
|
|
$this->persistCurrentUser();
|
|
|
|
}
|
2013-06-07 17:30:18 +02:00
|
|
|
}
|
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
2014-01-23 12:09:48 +01:00
|
|
|
* Writes the current user to the session
|
2013-08-13 18:08:21 +02:00
|
|
|
**/
|
2013-06-07 17:30:18 +02:00
|
|
|
public function persistCurrentUser()
|
|
|
|
{
|
2014-01-23 12:09:48 +01:00
|
|
|
$session = Session::getSession();
|
|
|
|
$session->set('user', $this->user);
|
|
|
|
$session->write();
|
2013-06-07 17:30:18 +02:00
|
|
|
}
|
2013-09-04 18:27:16 +02:00
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
2013-08-13 18:08:21 +02:00
|
|
|
* Tries to authenticate the user with the current session
|
|
|
|
**/
|
2013-06-07 17:30:18 +02:00
|
|
|
public function authenticateFromSession()
|
|
|
|
{
|
2014-01-23 12:09:48 +01:00
|
|
|
$this->user = Session::getSession()->get('user');
|
2013-06-07 17:30:18 +02:00
|
|
|
}
|
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
2013-08-13 18:08:21 +02:00
|
|
|
* Returns true when the user is currently authenticated
|
|
|
|
*
|
2014-01-23 12:09:48 +01:00
|
|
|
* @param Boolean $ignoreSession Set to true to prevent authentication by session
|
2013-08-13 18:08:21 +02:00
|
|
|
*
|
2013-08-28 10:16:18 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
2013-06-10 13:28:54 +02:00
|
|
|
public function isAuthenticated($ignoreSession = false)
|
2013-06-07 17:30:18 +02:00
|
|
|
{
|
2013-06-10 13:28:54 +02:00
|
|
|
if ($this->user === null && !$ignoreSession) {
|
2013-06-07 17:30:18 +02:00
|
|
|
$this->authenticateFromSession();
|
|
|
|
}
|
2013-06-10 13:28:54 +02:00
|
|
|
return is_object($this->user);
|
2013-06-07 17:30:18 +02:00
|
|
|
}
|
|
|
|
|
2014-01-22 14:06:59 +01:00
|
|
|
/**
|
|
|
|
* Whether an authenticated user has a given permission
|
|
|
|
*
|
|
|
|
* This is true if the user owns this permission, false if not.
|
|
|
|
* Also false if there is no authenticated user
|
|
|
|
*
|
|
|
|
* TODO: I'd like to see wildcard support, e.g. module/*
|
|
|
|
*
|
|
|
|
* @param string $permission Permission name
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasPermission($permission)
|
|
|
|
{
|
|
|
|
if (! $this->isAuthenticated()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
foreach ($this->user->getPermissions() as $p) {
|
|
|
|
if ($p === $permission) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get applied restrictions matching a given restriction name
|
|
|
|
*
|
|
|
|
* Returns a list of applied restrictions, empty if no user is
|
|
|
|
* authenticated
|
|
|
|
*
|
|
|
|
* @param string $restriction Restriction name
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getRestrictions($restriction)
|
|
|
|
{
|
|
|
|
if (! $this->isAuthenticated()) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
return $this->user->getRestrictions($restriction);
|
|
|
|
}
|
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
2014-01-23 12:09:48 +01:00
|
|
|
* Purges the current authorization information and removes the user from the session
|
2013-08-13 18:08:21 +02:00
|
|
|
**/
|
2013-06-07 17:30:18 +02:00
|
|
|
public function removeAuthorization()
|
|
|
|
{
|
|
|
|
$this->user = null;
|
2014-01-23 12:09:48 +01:00
|
|
|
$this->persistCurrentUser();
|
2013-06-07 17:30:18 +02:00
|
|
|
}
|
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
2013-11-20 19:10:38 +01:00
|
|
|
* Returns the current user or null if no user is authenticated
|
2013-08-13 18:08:21 +02:00
|
|
|
*
|
2013-11-20 19:10:38 +01:00
|
|
|
* @return User
|
2013-08-13 18:08:21 +02:00
|
|
|
**/
|
2013-06-07 17:30:18 +02:00
|
|
|
public function getUser()
|
|
|
|
{
|
|
|
|
return $this->user;
|
|
|
|
}
|
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
2014-01-23 12:09:48 +01:00
|
|
|
* Getter for groups belonged to authenticated user
|
2013-08-28 10:16:18 +02:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* @see User::getGroups
|
2013-08-13 18:08:21 +02:00
|
|
|
**/
|
2013-06-07 17:30:18 +02:00
|
|
|
public function getGroups()
|
|
|
|
{
|
|
|
|
return $this->user->getGroups();
|
|
|
|
}
|
2014-02-26 17:36:20 +01:00
|
|
|
|
2014-03-03 19:03:39 +01:00
|
|
|
/**
|
|
|
|
* Tries to authenticate the user from the session, and then from the REMOTE_USER superglobal, that can be set by
|
|
|
|
* an external authentication provider.
|
|
|
|
*/
|
|
|
|
public function authenticateFromRemoteUser()
|
|
|
|
{
|
|
|
|
$this->fromRemoteUser = true;
|
|
|
|
$this->authenticateFromSession();
|
|
|
|
if ($this->user !== null) {
|
|
|
|
if (array_key_exists('REMOTE_USER', $_SERVER) && $this->user->getUsername() !== $_SERVER["REMOTE_USER"]) {
|
|
|
|
// Remote user has changed, clear all sessions
|
|
|
|
$this->removeAuthorization();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (array_key_exists('REMOTE_USER', $_SERVER) && $_SERVER["REMOTE_USER"]) {
|
|
|
|
$this->user = new User($_SERVER["REMOTE_USER"]);
|
|
|
|
$this->persistCurrentUser();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-26 17:36:20 +01:00
|
|
|
/**
|
|
|
|
* If the session was established from the REMOTE_USER server variable.
|
|
|
|
*/
|
|
|
|
public function isAuthenticatedFromRemoteUser()
|
|
|
|
{
|
|
|
|
return $this->fromRemoteUser;
|
|
|
|
}
|
2013-06-07 17:30:18 +02:00
|
|
|
}
|