Merge branch 'bugfix/preferences-and-navigation-items-stored-in-ini-files-rely-on-case-sensitive-usernames-11051'

fixes #11051
This commit is contained in:
Alexander A. Klimov 2016-02-11 13:54:53 +01:00
commit d34b0f0d51
6 changed files with 101 additions and 11 deletions

View File

@ -7,6 +7,7 @@ use Exception;
use Icinga\Application\Config;
use Icinga\Exception\NotFoundError;
use Icinga\Data\DataArray\ArrayDatasource;
use Icinga\Data\Filter\FilterMatchCaseInsensitive;
use Icinga\Forms\ConfirmRemovalForm;
use Icinga\Forms\Navigation\NavigationConfigForm;
use Icinga\Web\Controller;
@ -78,7 +79,7 @@ class NavigationController extends Controller
$config->getConfigObject()->setKeyColumn('name');
$query = $config->select();
if ($owner !== null) {
$query->where('owner', $owner);
$query->applyFilter(new FilterMatchCaseInsensitive('owner', '=', $owner));
}
foreach ($query as $itemConfig) {

View File

@ -472,11 +472,18 @@ class Config implements Countable, Iterator, Selectable
$filename = $type . 's.ini';
}
return static::resolvePath(
($username ? 'preferences' . DIRECTORY_SEPARATOR . $username : 'navigation')
. DIRECTORY_SEPARATOR
. $filename
);
if ($username) {
$path = static::resolvePath(implode(DIRECTORY_SEPARATOR, array('preferences', $username, $filename)));
if (realpath($path) === false) {
$path = static::resolvePath(implode(
DIRECTORY_SEPARATOR,
array('preferences', strtolower($username), $filename)
));
}
} else {
$path = static::resolvePath('navigation' . DIRECTORY_SEPARATOR . $filename);
}
return $path;
}
/**

View File

@ -172,7 +172,7 @@ class Web extends EmbeddedWeb
{
// TODO: Provide a more sophisticated solution
if (isset($config['owner']) && $config['owner'] === $this->user->getUsername()) {
if (isset($config['owner']) && strtolower($config['owner']) === strtolower($this->user->getUsername())) {
unset($config['owner']);
unset($config['users']);
unset($config['groups']);
@ -195,7 +195,7 @@ class Web extends EmbeddedWeb
if (isset($config['users'])) {
$users = array_map('trim', explode(',', strtolower($config['users'])));
if (in_array('*', $users, true) || in_array($this->user->getUsername(), $users, true)) {
if (in_array('*', $users, true) || in_array(strtolower($this->user->getUsername()), $users, true)) {
unset($config['owner']);
unset($config['users']);
unset($config['groups']);

View File

@ -11,12 +11,20 @@ class FilterExpression extends Filter
protected $sign;
protected $expression;
/**
* Does this filter compare case sensitive?
*
* @var bool
*/
protected $caseSensitive;
public function __construct($column, $sign, $expression)
{
$column = trim($column);
$this->column = $column;
$this->sign = $sign;
$this->expression = $expression;
$this->caseSensitive = true;
}
public function isExpression()
@ -55,6 +63,16 @@ class FilterExpression extends Filter
return $this->expression;
}
/**
* Return whether this filter compares case sensitive
*
* @return bool
*/
public function getCaseSensitive()
{
return $this->caseSensitive;
}
public function setExpression($expression)
{
$this->expression = $expression;
@ -69,6 +87,19 @@ class FilterExpression extends Filter
return $this;
}
/**
* Set this filter's case sensitivity
*
* @param bool $caseSensitive
*
* @return $this
*/
public function setCaseSensitive($caseSensitive = true)
{
$this->caseSensitive = $caseSensitive;
return $this;
}
public function listFilteredColumns()
{
return array($this->getColumn());
@ -97,6 +128,26 @@ class FilterExpression extends Filter
return $this->column . $this->sign . $expression;
}
/**
* If $var is a scalar, do the same as strtolower() would do.
* If $var is an array, map $this->strtolowerRecursive() to its elements.
* Otherwise, return $var unchanged.
*
* @param mixed $var
*
* @return mixed
*/
protected function strtolowerRecursive($var)
{
if ($var === null || is_scalar($var)) {
return strtolower($var);
}
if (is_array($var)) {
return array_map(array($this, 'strtolowerRecursive'), $var);
}
return $var;
}
public function matches($row)
{
try {
@ -106,11 +157,18 @@ class FilterExpression extends Filter
return false;
}
if (is_array($this->expression)) {
return in_array($rowValue, $this->expression);
if ($this->caseSensitive) {
$expression = $this->expression;
} else {
$rowValue = $this->strtolowerRecursive($rowValue);
$expression = $this->strtolowerRecursive($this->expression);
}
$expression = (string) $this->expression;
if (is_array($expression)) {
return in_array($rowValue, $expression);
}
$expression = (string) $expression;
if (strpos($expression, '*') === false) {
if (is_array($rowValue)) {
return in_array($expression, $rowValue);

View File

@ -0,0 +1,12 @@
<?php
/* Icinga Web 2 | (c) 2016 Icinga Development Team | GPLv2+ */
namespace Icinga\Data\Filter;
class FilterMatchCaseInsensitive extends FilterMatch
{
public function __construct($column, $sign, $expression) {
parent::__construct($column, $sign, $expression);
$this->caseSensitive = false;
}
}

View File

@ -0,0 +1,12 @@
<?php
/* Icinga Web 2 | (c) 2016 Icinga Development Team | GPLv2+ */
namespace Icinga\Data\Filter;
class FilterMatchNotCaseInsensitive extends FilterMatchNot
{
public function __construct($column, $sign, $expression) {
parent::__construct($column, $sign, $expression);
$this->caseSensitive = false;
}
}