Change interface of SearchDashboard to allow setting a user before calling ::search()

Because search dashlets are provided by modules, the authenticated user's module permissions have to be validated before loading search dashlets provided by modules.

refs #9644
This commit is contained in:
Eric Lippmann 2015-07-23 12:53:08 +02:00
parent b4f4ce4406
commit e8e01257e6
1 changed files with 36 additions and 39 deletions

View File

@ -12,6 +12,11 @@ use Icinga\Web\Url;
*/
class SearchDashboard extends Dashboard
{
/**
* Name for the search pane
*
* @var string
*/
const SEARCH_PANE = 'search';
/**
@ -19,13 +24,39 @@ class SearchDashboard extends Dashboard
*
* @param string $searchString
*
* @return Dashboard|SearchDashboard
* @return $this
*/
public static function search($searchString = '')
public function search($searchString = '')
{
$dashboard = new static('searchDashboard');
$dashboard->loadSearchDashlets($searchString);
return $dashboard;
$pane = $this->createPane(self::SEARCH_PANE)->getPane(self::SEARCH_PANE)->setTitle(t('Search'));
$this->activate(self::SEARCH_PANE);
$manager = Icinga::app()->getModuleManager();
$searchUrls = array();
foreach ($manager->getLoadedModules() as $module) {
if ($this->getUser()->can($manager::MODULE_PERMISSION_NS . $module->getName())) {
$moduleSearchUrls = $module->getSearchUrls();
if (! empty($moduleSearchUrls)) {
if ($searchString === '') {
$pane->add(t('Ready to search'), 'search/hint');
return $this;
}
$searchUrls = array_merge($searchUrls, $moduleSearchUrls);
}
}
}
usort($searchUrls, array($this, 'compareSearchUrls'));
foreach (array_reverse($searchUrls) as $searchUrl) {
$pane->addDashlet(
$searchUrl->title . ': ' . $searchString,
Url::fromPath($searchUrl->url, array('q' => $searchString))
);
}
return $this;
}
/**
@ -43,40 +74,6 @@ class SearchDashboard extends Dashboard
return parent::render();
}
/**
* Loads search dashlets
*
* @param string $searchString
*/
protected function loadSearchDashlets($searchString)
{
$pane = $this->createPane(self::SEARCH_PANE)->getPane(self::SEARCH_PANE)->setTitle(t('Search'));
$this->activate(self::SEARCH_PANE);
$manager = Icinga::app()->getModuleManager();
$searchUrls = array();
foreach ($manager->getLoadedModules() as $module) {
$moduleSearchUrls = $module->getSearchUrls();
if (! empty($moduleSearchUrls)) {
if ($searchString === '') {
$pane->add(t('Ready to search'), 'search/hint');
return;
}
$searchUrls = array_merge($searchUrls, $moduleSearchUrls);
}
}
usort($searchUrls, array($this, 'compareSearchUrls'));
foreach (array_reverse($searchUrls) as $searchUrl) {
$pane->addDashlet(
$searchUrl->title . ': ' . $searchString,
Url::fromPath($searchUrl->url, array('q' => $searchString))
);
}
}
/**
* Compare search URLs based on their priority
*