2013-06-07 11:44:37 +02:00
|
|
|
<?php
|
2013-06-10 17:03:01 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-06-28 16:47:30 +02:00
|
|
|
/**
|
2013-07-15 12:26:10 +02:00
|
|
|
* This file is part of Icinga 2 Web.
|
2013-07-26 15:58:16 +02:00
|
|
|
*
|
2013-07-15 12:26:10 +02:00
|
|
|
* Icinga 2 Web - Head for multiple monitoring backends.
|
2013-06-28 16:47:30 +02:00
|
|
|
* Copyright (C) 2013 Icinga Development Team
|
2013-07-26 15:58: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-07-26 15:58: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-07-26 15:58: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-07-26 15:58:16 +02:00
|
|
|
*
|
2013-06-28 16:47:30 +02:00
|
|
|
* @copyright 2013 Icinga Development Team <info@icinga.org>
|
2013-07-15 12:26:10 +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 11:44:37 +02:00
|
|
|
|
2013-07-26 15:58:16 +02:00
|
|
|
namespace Icinga;
|
|
|
|
|
2013-08-07 17:12:57 +02:00
|
|
|
use \DateTimeZone;
|
|
|
|
use \InvalidArgumentException;
|
2013-07-26 15:58:16 +02:00
|
|
|
use Icinga\User\Preferences;
|
2013-06-07 11:44:37 +02:00
|
|
|
|
|
|
|
/**
|
2013-07-26 15:58:16 +02:00
|
|
|
* This class represents an authorized user
|
|
|
|
*
|
|
|
|
* You can retrieve authorization information (@TODO: Not implemented yet) or
|
|
|
|
* to retrieve user information
|
2013-06-07 11:44:37 +02:00
|
|
|
*
|
|
|
|
*/
|
2013-06-10 13:28:54 +02:00
|
|
|
class User
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
2013-07-26 15:58:16 +02:00
|
|
|
/**
|
|
|
|
* Username
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $username;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Firstname
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $firstname;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lastname
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $lastname;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Users email address
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $email;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Domain
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $domain;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* More information about user
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $additionalInformation = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set of permissions
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $permissions = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Groups for this user
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $groups = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Preferences object
|
|
|
|
*
|
|
|
|
* @var Preferences
|
|
|
|
*/
|
|
|
|
private $preferences;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a user object given the provided information
|
|
|
|
*
|
|
|
|
* @param string $username
|
|
|
|
* @param string $firstname
|
|
|
|
* @param string $lastname
|
|
|
|
* @param string $email
|
|
|
|
*/
|
2013-06-11 13:53:42 +02:00
|
|
|
public function __construct($username, $firstname = null, $lastname = null, $email = null)
|
2013-06-10 13:28:54 +02:00
|
|
|
{
|
|
|
|
$this->setUsername($username);
|
2013-06-11 13:53:42 +02:00
|
|
|
|
|
|
|
if ($firstname !== null) {
|
|
|
|
$this->setFirstname($firstname);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($lastname !== null) {
|
|
|
|
$this->setLastname($lastname);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($email !== null) {
|
|
|
|
$this->setEmail($email);
|
|
|
|
}
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
2013-07-26 15:58:16 +02:00
|
|
|
* Setter for preferences
|
|
|
|
*
|
|
|
|
* @param Preferences $preferences
|
|
|
|
*/
|
|
|
|
public function setPreferences(Preferences $preferences)
|
|
|
|
{
|
|
|
|
$this->preferences = $preferences;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Getter for preferences
|
|
|
|
*
|
|
|
|
* @return Preferences
|
|
|
|
*/
|
|
|
|
public function getPreferences()
|
|
|
|
{
|
|
|
|
return $this->preferences;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-07-30 10:47:50 +02:00
|
|
|
* Return all groups this user belongs to
|
2013-07-26 15:58:16 +02:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-06-10 13:28:54 +02:00
|
|
|
public function getGroups()
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
2013-06-10 13:28:54 +02:00
|
|
|
return $this->groups;
|
|
|
|
}
|
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
2013-07-30 10:47:50 +02:00
|
|
|
* Set the groups this user belongs to
|
2013-07-26 15:58:16 +02:00
|
|
|
*/
|
2013-06-10 13:28:54 +02:00
|
|
|
public function setGroups(array $groups)
|
|
|
|
{
|
|
|
|
$this->groups = $groups;
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
2013-07-26 15:58:16 +02:00
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
2013-07-30 10:47:50 +02:00
|
|
|
* Return true if the user is a member of this group
|
2013-07-26 15:58:16 +02:00
|
|
|
*
|
|
|
|
* @param string $group
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isMemberOf($group)
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
2013-06-10 13:28:54 +02:00
|
|
|
return in_array($group, $this->groups);
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
2013-07-30 10:47:50 +02:00
|
|
|
* Return permission information for this user
|
2013-07-26 15:58:16 +02:00
|
|
|
*
|
|
|
|
* @return Array
|
|
|
|
*/
|
2013-06-10 13:28:54 +02:00
|
|
|
public function getPermissions()
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
|
|
|
return $this->permissions;
|
|
|
|
}
|
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
2013-07-26 15:58:16 +02:00
|
|
|
* Getter for username
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-06-10 13:28:54 +02:00
|
|
|
public function getUsername()
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
2013-06-10 13:28:54 +02:00
|
|
|
return $this->username;
|
|
|
|
}
|
2013-06-07 11:44:37 +02:00
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
2013-07-26 15:58:16 +02:00
|
|
|
* Setter for username
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*/
|
2013-06-10 13:28:54 +02:00
|
|
|
public function setUsername($name)
|
|
|
|
{
|
|
|
|
$this->username = $name;
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
2013-07-26 15:58:16 +02:00
|
|
|
* Getter for firstname
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-06-10 13:28:54 +02:00
|
|
|
public function getFirstname()
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
2013-06-10 13:28:54 +02:00
|
|
|
return $this->firstname;
|
|
|
|
}
|
|
|
|
|
2013-07-26 15:58:16 +02:00
|
|
|
/**
|
|
|
|
* Setter for firstname
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*/
|
2013-06-10 13:28:54 +02:00
|
|
|
public function setFirstname($name)
|
|
|
|
{
|
|
|
|
$this->firstname = $name;
|
|
|
|
}
|
2013-06-07 11:44:37 +02:00
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
2013-07-26 15:58:16 +02:00
|
|
|
* Getter for lastname
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-06-10 13:28:54 +02:00
|
|
|
public function getLastname()
|
|
|
|
{
|
|
|
|
return $this->lastname;
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
2013-07-26 15:58:16 +02:00
|
|
|
* Setter for lastname
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*/
|
2013-06-10 13:28:54 +02:00
|
|
|
public function setLastname($name)
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
2013-06-10 13:28:54 +02:00
|
|
|
$this->lastname = $name;
|
|
|
|
}
|
2013-06-07 11:44:37 +02:00
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
2013-07-26 15:58:16 +02:00
|
|
|
* Getter for email
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-06-10 13:28:54 +02:00
|
|
|
public function getEmail()
|
|
|
|
{
|
|
|
|
return $this->email;
|
|
|
|
}
|
2013-07-26 15:58:16 +02:00
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
2013-07-26 15:58:16 +02:00
|
|
|
* Setter for mail
|
|
|
|
*
|
|
|
|
* @param string $mail
|
|
|
|
* @throws InvalidArgumentException When an invalid mail is provided
|
|
|
|
*/
|
2013-06-10 13:28:54 +02:00
|
|
|
public function setEmail($mail)
|
|
|
|
{
|
|
|
|
if (filter_var($mail, FILTER_VALIDATE_EMAIL)) {
|
|
|
|
$this->mail = $mail;
|
|
|
|
} else {
|
2013-07-26 15:58:16 +02:00
|
|
|
throw new InvalidArgumentException("Invalid mail given for user $this->username: $mail");
|
2013-06-10 13:28:54 +02:00
|
|
|
}
|
|
|
|
}
|
2013-07-26 15:58:16 +02:00
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
2013-07-26 15:58:16 +02:00
|
|
|
* Setter for domain
|
|
|
|
*
|
|
|
|
* @param string $domain
|
|
|
|
*/
|
2013-06-10 13:28:54 +02:00
|
|
|
public function setDomain($domain)
|
|
|
|
{
|
|
|
|
$this->domain = $domain;
|
|
|
|
}
|
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
2013-07-26 15:58:16 +02:00
|
|
|
* Getter for domain
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-06-10 13:28:54 +02:00
|
|
|
public function getDomain()
|
|
|
|
{
|
|
|
|
return $this->domain;
|
|
|
|
}
|
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
2013-07-30 10:47:50 +02:00
|
|
|
* Set additional information about user
|
2013-07-26 15:58:16 +02:00
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @param string $value
|
|
|
|
*/
|
2013-06-10 13:28:54 +02:00
|
|
|
public function setAdditional($key, $value)
|
|
|
|
{
|
|
|
|
$this->additionalInformation[$key] = $value;
|
|
|
|
}
|
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
2013-07-26 15:58:16 +02:00
|
|
|
* Getter for additional information
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return mixed|null
|
|
|
|
*/
|
2013-06-10 13:28:54 +02:00
|
|
|
public function getAdditional($key)
|
|
|
|
{
|
|
|
|
if (isset($this->additionalInformation[$key])) {
|
|
|
|
return $this->additionalInformation[$key];
|
|
|
|
}
|
|
|
|
return null;
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
2013-08-06 18:02:40 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the user's timezone
|
|
|
|
*
|
2013-08-07 17:12:57 +02:00
|
|
|
* If the user did not set a timezone, the default timezone set via config.ini is returned
|
2013-08-06 18:02:40 +02:00
|
|
|
*
|
2013-08-07 17:12:57 +02:00
|
|
|
* @return DateTimeZone
|
2013-08-06 18:02:40 +02:00
|
|
|
*/
|
2013-08-07 01:47:32 +02:00
|
|
|
public function getTimeZone()
|
2013-08-06 18:02:40 +02:00
|
|
|
{
|
|
|
|
$tz = $this->preferences->get('timezone');
|
|
|
|
if ($tz === null) {
|
|
|
|
$tz = date_default_timezone_get();
|
|
|
|
}
|
2013-08-07 17:12:57 +02:00
|
|
|
return new DateTimeZone($tz);
|
2013-08-06 18:02:40 +02:00
|
|
|
}
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|