2013-06-07 11:44:37 +02:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
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
|
|
|
/**
|
2015-07-29 15:46:53 +02:00
|
|
|
* Information if the user is externally authenticated
|
2014-07-30 12:35:55 +02:00
|
|
|
*
|
|
|
|
* Keys:
|
|
|
|
*
|
|
|
|
* 0: origin username
|
|
|
|
* 1: origin field name
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2015-07-29 15:44:32 +02:00
|
|
|
protected $externalUserInformation = array();
|
2014-07-30 12:35:55 +02:00
|
|
|
|
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
|
|
|
|
2015-07-30 09:31:58 +02:00
|
|
|
/**
|
|
|
|
* Whether the user is authenticated using a HTTP authentication mechanism
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $isHttpUser = false;
|
|
|
|
|
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-30 15:50:58 +02:00
|
|
|
if (! empty($permissions)) {
|
2015-01-22 15:20:19 +01:00
|
|
|
natcasesort($permissions);
|
2014-09-30 15:50:58 +02:00
|
|
|
$this->permissions = array_combine($permissions, $permissions);
|
|
|
|
}
|
2014-09-18 14:37:18 +02:00
|
|
|
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
|
|
|
/**
|
2015-07-29 15:44:32 +02:00
|
|
|
* Set additional external user information
|
2014-07-30 12:35:55 +02:00
|
|
|
*
|
2015-07-29 15:46:53 +02:00
|
|
|
* @param string $username
|
2014-07-30 12:35:55 +02:00
|
|
|
* @param string $field
|
|
|
|
*/
|
2015-07-29 15:44:32 +02:00
|
|
|
public function setExternalUserInformation($username, $field)
|
2014-07-30 12:35:55 +02:00
|
|
|
{
|
2015-07-29 15:44:32 +02:00
|
|
|
$this->externalUserInformation = array($username, $field);
|
2014-07-30 12:35:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-07-29 15:44:32 +02:00
|
|
|
* Get additional external user information
|
2014-07-30 12:35:55 +02:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2015-07-29 15:44:32 +02:00
|
|
|
public function getExternalUserInformation()
|
2014-07-30 12:35:55 +02:00
|
|
|
{
|
2015-07-29 15:44:32 +02:00
|
|
|
return $this->externalUserInformation;
|
2014-07-30 12:35:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-07-29 15:44:32 +02:00
|
|
|
* Return true if user has external user information set
|
2014-07-30 12:35:55 +02:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2015-07-29 15:44:32 +02:00
|
|
|
public function isExternalUser()
|
2014-07-30 12:35:55 +02:00
|
|
|
{
|
2015-07-29 15:44:32 +02:00
|
|
|
return ! empty($this->externalUserInformation);
|
2014-07-30 12:35:55 +02:00
|
|
|
}
|
2014-09-18 14:37:18 +02:00
|
|
|
|
2015-07-30 09:31:58 +02:00
|
|
|
/**
|
|
|
|
* Get whether the user is authenticated using a HTTP authentication mechanism
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getIsHttpUser()
|
|
|
|
{
|
|
|
|
return $this->isHttpUser;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set whether the user is authenticated using a HTTP authentication mechanism
|
|
|
|
*
|
|
|
|
* @param bool $isHttpUser
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setIsHttpUser($isHttpUser = true)
|
|
|
|
{
|
|
|
|
$this->isHttpUser = (bool) $isHttpUser;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-09-18 14:37:18 +02:00
|
|
|
/**
|
|
|
|
* Whether the user has a given permission
|
|
|
|
*
|
2015-05-05 12:36:26 +02:00
|
|
|
* @param string $requiredPermission
|
2014-09-18 14:37:18 +02:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2015-05-05 12:36:26 +02:00
|
|
|
public function can($requiredPermission)
|
2014-09-18 14:37:18 +02:00
|
|
|
{
|
2015-05-05 12:36:26 +02:00
|
|
|
if (isset($this->permissions['*']) || isset($this->permissions[$requiredPermission])) {
|
2014-09-18 14:37:18 +02:00
|
|
|
return true;
|
|
|
|
}
|
2015-08-27 14:24:04 +02:00
|
|
|
|
|
|
|
$requiredWildcard = strpos($requiredPermission, '*');
|
2015-05-05 12:36:26 +02:00
|
|
|
foreach ($this->permissions as $grantedPermission) {
|
2015-08-27 14:24:04 +02:00
|
|
|
if ($requiredWildcard !== false) {
|
|
|
|
if (($grantedWildcard = strpos($grantedPermission, '*')) !== false) {
|
|
|
|
$wildcard = min($requiredWildcard, $grantedWildcard);
|
|
|
|
} else {
|
|
|
|
$wildcard = $requiredWildcard;
|
|
|
|
}
|
2015-03-12 15:27:44 +01:00
|
|
|
} else {
|
2015-05-05 12:36:26 +02:00
|
|
|
$wildcard = strpos($grantedPermission, '*');
|
2015-03-12 15:27:44 +01:00
|
|
|
}
|
2015-08-27 14:24:04 +02:00
|
|
|
|
2014-09-18 14:37:18 +02:00
|
|
|
if ($wildcard !== false) {
|
2015-05-05 12:36:26 +02:00
|
|
|
if (substr($requiredPermission, 0, $wildcard) === substr($grantedPermission, 0, $wildcard)) {
|
2015-02-02 13:41:02 +01:00
|
|
|
return true;
|
|
|
|
}
|
2015-05-05 12:36:26 +02:00
|
|
|
} elseif ($requiredPermission === $grantedPermission) {
|
2015-02-02 13:44:54 +01:00
|
|
|
return true;
|
2014-09-18 14:37:18 +02:00
|
|
|
}
|
|
|
|
}
|
2015-08-27 14:24:04 +02:00
|
|
|
|
2014-09-18 14:37:18 +02:00
|
|
|
return false;
|
2014-07-30 12:35:55 +02:00
|
|
|
}
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|