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

View File

@ -12,6 +12,11 @@ use Icinga\Web\Url;
*/ */
class SearchDashboard extends Dashboard class SearchDashboard extends Dashboard
{ {
/**
* Name for the search pane
*
* @var string
*/
const SEARCH_PANE = 'search'; const SEARCH_PANE = 'search';
/** /**
@ -19,13 +24,39 @@ class SearchDashboard extends Dashboard
* *
* @param string $searchString * @param string $searchString
* *
* @return Dashboard|SearchDashboard * @return $this
*/ */
public static function search($searchString = '') public function search($searchString = '')
{ {
$dashboard = new static('searchDashboard'); $pane = $this->createPane(self::SEARCH_PANE)->getPane(self::SEARCH_PANE)->setTitle(t('Search'));
$dashboard->loadSearchDashlets($searchString); $this->activate(self::SEARCH_PANE);
return $dashboard;
$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(); 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 * Compare search URLs based on their priority
* *