2013-06-07 11:44:37 +02:00
|
|
|
<?php
|
2013-06-10 17:03:01 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-06-07 11:44:37 +02:00
|
|
|
|
2013-07-26 15:58:16 +02:00
|
|
|
namespace Icinga;
|
|
|
|
|
2014-04-22 09:43:53 +02:00
|
|
|
use DateTimeZone;
|
|
|
|
use InvalidArgumentException;
|
2014-01-22 14:06:59 +01:00
|
|
|
use Icinga\User\Preferences;
|
2013-11-20 19:10:38 +01:00
|
|
|
|
2013-06-07 11:44:37 +02:00
|
|
|
/**
|
2013-07-26 15:58:16 +02:00
|
|
|
* This class represents an authorized user
|
|
|
|
*
|
2014-04-22 09:43:53 +02:00
|
|
|
* You can retrieve authorization information (@TODO: Not implemented yet) or 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
|
|
|
|
*/
|
2014-04-22 09:43:53 +02:00
|
|
|
protected $username;
|
2013-07-26 15:58:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Firstname
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2014-04-22 09:43:53 +02:00
|
|
|
protected $firstname;
|
2013-07-26 15:58:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Lastname
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2014-04-22 09:43:53 +02:00
|
|
|
protected $lastname;
|
2013-07-26 15:58:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Users email address
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2014-04-22 09:43:53 +02:00
|
|
|
protected $email;
|
2013-07-26 15:58:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Domain
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2014-04-22 09:43:53 +02:00
|
|
|
protected $domain;
|
2013-07-26 15:58:16 +02:00
|
|
|
|
|
|
|
/**
|
2014-04-22 09:43:53 +02:00
|
|
|
* More information about this user
|
2013-07-26 15:58:16 +02:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2014-04-22 09:43:53 +02:00
|
|
|
protected $additionalInformation = array();
|
2013-07-26 15:58:16 +02:00
|
|
|
|
2014-07-30 12:35:55 +02:00
|
|
|
/**
|
|
|
|
* Information if the user is external authenticated
|
|
|
|
*
|
|
|
|
* Keys:
|
|
|
|
*
|
|
|
|
* 0: origin username
|
|
|
|
* 1: origin field name
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $remoteUserInformation = array();
|
|
|
|
|
2013-07-26 15:58:16 +02:00
|
|
|
/**
|
|
|
|
* Set of permissions
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2014-04-22 09:43:53 +02:00
|
|
|
protected $permissions = array();
|
2013-07-26 15:58:16 +02:00
|
|
|
|
2014-01-22 14:06:59 +01:00
|
|
|
/**
|
|
|
|
* Set of restrictions
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2014-04-22 09:43:53 +02:00
|
|
|
protected $restrictions = array();
|
2014-01-22 14:06:59 +01:00
|
|
|
|
2013-07-26 15:58:16 +02:00
|
|
|
/**
|
|
|
|
* Groups for this user
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2014-04-22 09:43:53 +02:00
|
|
|
protected $groups = array();
|
2013-07-26 15:58:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Preferences object
|
|
|
|
*
|
|
|
|
* @var Preferences
|
|
|
|
*/
|
2014-04-22 09:43:53 +02:00
|
|
|
protected $preferences;
|
2013-07-26 15:58:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a user object given the provided information
|
|
|
|
*
|
2014-04-22 09:43:53 +02:00
|
|
|
* @param string $username
|
|
|
|
* @param string $firstname
|
|
|
|
* @param string $lastname
|
|
|
|
* @param string $email
|
2013-07-26 15:58:16 +02:00
|
|
|
*/
|
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
|
|
|
|
*
|
2014-04-22 09:43:53 +02:00
|
|
|
* @param Preferences $preferences
|
2013-07-26 15:58:16 +02:00
|
|
|
*/
|
|
|
|
public function setPreferences(Preferences $preferences)
|
|
|
|
{
|
|
|
|
$this->preferences = $preferences;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Getter for preferences
|
|
|
|
*
|
2014-04-22 09:43:53 +02:00
|
|
|
* @return Preferences
|
2013-07-26 15:58:16 +02:00
|
|
|
*/
|
|
|
|
public function getPreferences()
|
|
|
|
{
|
2014-03-04 11:14:20 +01:00
|
|
|
if ($this->preferences === null) {
|
|
|
|
$this->preferences = new Preferences();
|
|
|
|
}
|
2014-04-22 09:43:53 +02:00
|
|
|
|
2013-07-26 15:58:16 +02:00
|
|
|
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
|
|
|
*
|
2014-04-22 09:43:53 +02:00
|
|
|
* @return array
|
2013-07-26 15:58:16 +02:00
|
|
|
*/
|
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
|
2014-04-22 09:43:53 +02:00
|
|
|
*
|
|
|
|
* @param array $groups
|
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
|
|
|
*
|
2014-04-22 09:43:53 +02:00
|
|
|
* @param string $group
|
|
|
|
*
|
|
|
|
* @return boolean
|
2013-07-26 15:58:16 +02:00
|
|
|
*/
|
|
|
|
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
|
|
|
/**
|
2014-09-18 14:37:18 +02:00
|
|
|
* Get the user's permissions
|
2013-07-26 15:58:16 +02:00
|
|
|
*
|
2014-09-18 14:37:18 +02:00
|
|
|
* @return array
|
2013-07-26 15:58:16 +02:00
|
|
|
*/
|
2013-06-10 13:28:54 +02:00
|
|
|
public function getPermissions()
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
|
|
|
return $this->permissions;
|
|
|
|
}
|
|
|
|
|
2014-02-12 17:01:11 +01:00
|
|
|
/**
|
2014-09-18 14:37:18 +02:00
|
|
|
* Set the user's permissions
|
|
|
|
*
|
|
|
|
* @param array $permissions
|
2014-02-12 17:01:11 +01:00
|
|
|
*
|
2014-09-18 14:37:18 +02:00
|
|
|
* @return $this
|
2014-02-12 17:01:11 +01:00
|
|
|
*/
|
|
|
|
public function setPermissions(array $permissions)
|
|
|
|
{
|
2014-09-18 14:37:18 +02:00
|
|
|
natcasesort($permissions);
|
|
|
|
$this->permissions = array_combine($permissions, $permissions);
|
|
|
|
return $this;
|
2014-02-12 17:01:11 +01:00
|
|
|
}
|
|
|
|
|
2014-01-22 14:06:59 +01:00
|
|
|
/**
|
|
|
|
* Return restriction information for this user
|
|
|
|
*
|
2014-02-12 17:01:11 +01:00
|
|
|
* @param string $name
|
2014-04-22 09:43:53 +02:00
|
|
|
*
|
2014-02-12 17:01:11 +01:00
|
|
|
* @return array
|
2014-01-22 14:06:59 +01:00
|
|
|
*/
|
|
|
|
public function getRestrictions($name)
|
|
|
|
{
|
|
|
|
if (array_key_exists($name, $this->restrictions)) {
|
|
|
|
return $this->restrictions[$name];
|
|
|
|
}
|
2014-04-22 09:43:53 +02:00
|
|
|
|
2014-01-22 14:06:59 +01:00
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2014-02-12 17:01:11 +01:00
|
|
|
/**
|
|
|
|
* Settter for restrictions
|
|
|
|
*
|
2014-04-22 09:43:53 +02:00
|
|
|
* @param array $restrictions
|
2014-02-12 17:01:11 +01:00
|
|
|
*/
|
|
|
|
public function setRestrictions(array $restrictions)
|
|
|
|
{
|
|
|
|
$this->restrictions = $restrictions;
|
|
|
|
}
|
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
2013-07-26 15:58:16 +02:00
|
|
|
* Getter for username
|
|
|
|
*
|
2014-04-22 09:43:53 +02:00
|
|
|
* @return string
|
2013-07-26 15:58:16 +02:00
|
|
|
*/
|
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
|
|
|
|
*
|
2014-04-22 09:43:53 +02:00
|
|
|
* @param string $name
|
2013-07-26 15:58:16 +02:00
|
|
|
*/
|
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
|
|
|
|
*
|
2014-04-22 09:43:53 +02:00
|
|
|
* @return string
|
2013-07-26 15:58:16 +02:00
|
|
|
*/
|
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
|
|
|
|
*
|
2014-04-22 09:43:53 +02:00
|
|
|
* @param string $name
|
2013-07-26 15:58:16 +02:00
|
|
|
*/
|
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
|
|
|
|
*
|
2014-04-22 09:43:53 +02:00
|
|
|
* @return string
|
2013-07-26 15:58:16 +02:00
|
|
|
*/
|
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
|
|
|
|
*
|
2014-04-22 09:43:53 +02:00
|
|
|
* @param string $name
|
2013-07-26 15:58:16 +02:00
|
|
|
*/
|
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
|
|
|
|
*
|
2014-04-22 09:43:53 +02:00
|
|
|
* @return string
|
2013-07-26 15:58:16 +02:00
|
|
|
*/
|
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
|
|
|
|
*
|
2014-04-22 09:43:53 +02:00
|
|
|
* @param string $mail
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException When an invalid mail is provided
|
2013-07-26 15:58:16 +02:00
|
|
|
*/
|
2013-06-10 13:28:54 +02:00
|
|
|
public function setEmail($mail)
|
|
|
|
{
|
|
|
|
if (filter_var($mail, FILTER_VALIDATE_EMAIL)) {
|
2013-11-28 17:31:18 +01:00
|
|
|
$this->email = $mail;
|
2013-06-10 13:28:54 +02:00
|
|
|
} 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
|
|
|
|
*
|
2014-04-22 09:43:53 +02:00
|
|
|
* @param string $domain
|
2013-07-26 15:58:16 +02:00
|
|
|
*/
|
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
|
|
|
|
*
|
2014-04-22 09:43:53 +02:00
|
|
|
* @return string
|
2013-07-26 15:58:16 +02:00
|
|
|
*/
|
2013-06-10 13:28:54 +02:00
|
|
|
public function getDomain()
|
|
|
|
{
|
|
|
|
return $this->domain;
|
|
|
|
}
|
|
|
|
|
2014-01-22 14:06:59 +01:00
|
|
|
|
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
|
|
|
*
|
2014-04-22 09:43:53 +02:00
|
|
|
* @param string $key
|
|
|
|
* @param string $value
|
2013-07-26 15:58:16 +02:00
|
|
|
*/
|
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
|
|
|
|
*
|
2014-04-22 09:43:53 +02:00
|
|
|
* @param string $key
|
|
|
|
* @return mixed|null
|
2013-07-26 15:58:16 +02:00
|
|
|
*/
|
2013-06-10 13:28:54 +02:00
|
|
|
public function getAdditional($key)
|
|
|
|
{
|
|
|
|
if (isset($this->additionalInformation[$key])) {
|
|
|
|
return $this->additionalInformation[$key];
|
|
|
|
}
|
2014-04-22 09:43:53 +02:00
|
|
|
|
2013-06-10 13:28:54 +02:00
|
|
|
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();
|
|
|
|
}
|
2014-04-22 09:43:53 +02:00
|
|
|
|
2013-08-07 17:12:57 +02:00
|
|
|
return new DateTimeZone($tz);
|
2013-08-06 18:02:40 +02:00
|
|
|
}
|
2013-11-20 12:01:40 +01:00
|
|
|
|
2014-07-30 12:35:55 +02:00
|
|
|
/**
|
|
|
|
* Set additional remote user information
|
|
|
|
*
|
|
|
|
* @param stirng $username
|
|
|
|
* @param string $field
|
|
|
|
*/
|
|
|
|
public function setRemoteUserInformation($username, $field)
|
|
|
|
{
|
|
|
|
$this->remoteUserInformation = array($username, $field);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get additional remote user information
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getRemoteUserInformation()
|
|
|
|
{
|
|
|
|
return $this->remoteUserInformation;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if user has remote user information set
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isRemoteUser()
|
|
|
|
{
|
2014-09-18 15:18:01 +02:00
|
|
|
return ! empty($this->remoteUserInformation);
|
2014-07-30 12:35:55 +02:00
|
|
|
}
|
2014-09-18 14:37:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the user has a given permission
|
|
|
|
*
|
|
|
|
* @param string $permission
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function can($permission)
|
|
|
|
{
|
|
|
|
if (isset($this->permissions['*']) || isset($this->permissions[$permission])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
foreach ($this->permissions as $permitted) {
|
|
|
|
$wildcard = strpos($permitted, '*');
|
|
|
|
if ($wildcard !== false) {
|
|
|
|
if (substr($permission, 0, $wildcard) === substr($permitted, 0, $wildcard)) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
if ($permission === $permitted) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2014-07-30 12:35:55 +02:00
|
|
|
}
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|