2015-04-24 08:26:44 -04:00
|
|
|
<?php
|
|
|
|
|
|
2015-06-30 05:27:32 -04:00
|
|
|
namespace Icinga\Module\Director\Web\Controller;
|
2015-04-24 08:26:44 -04:00
|
|
|
|
2021-11-24 05:56:18 -05:00
|
|
|
use gipfl\Translation\StaticTranslator;
|
2017-08-16 09:30:46 -04:00
|
|
|
use Icinga\Application\Benchmark;
|
2023-11-17 04:27:25 -05:00
|
|
|
use Icinga\Application\Modules\Module;
|
2015-07-23 10:19:22 -04:00
|
|
|
use Icinga\Data\Paginatable;
|
2019-10-31 10:43:44 -04:00
|
|
|
use Icinga\Exception\NotFoundError;
|
2018-06-07 17:37:28 -04:00
|
|
|
use Icinga\Exception\ProgrammingError;
|
2023-11-17 06:56:07 -05:00
|
|
|
use Icinga\Module\Director\Integration\Icingadb\IcingadbBackend;
|
|
|
|
|
use Icinga\Module\Director\Integration\BackendInterface;
|
2023-11-17 06:31:20 -05:00
|
|
|
use Icinga\Module\Director\Integration\MonitoringModule\Monitoring;
|
2023-11-21 07:07:16 -05:00
|
|
|
use Icinga\Module\Director\ProvidedHook\Icingadb\IcingadbSupport;
|
2017-06-20 18:31:40 -04:00
|
|
|
use Icinga\Module\Director\Web\Controller\Extension\CoreApi;
|
|
|
|
|
use Icinga\Module\Director\Web\Controller\Extension\DirectorDb;
|
|
|
|
|
use Icinga\Module\Director\Web\Controller\Extension\RestApi;
|
2020-01-17 05:58:28 -05:00
|
|
|
use Icinga\Module\Director\Web\Window;
|
2016-11-03 11:06:18 -04:00
|
|
|
use Icinga\Security\SecurityException;
|
2015-04-24 08:26:44 -04:00
|
|
|
use Icinga\Web\Controller;
|
2017-07-04 23:46:16 -04:00
|
|
|
use Icinga\Web\UrlParams;
|
2018-06-07 17:37:28 -04:00
|
|
|
use InvalidArgumentException;
|
2019-05-02 07:23:06 -04:00
|
|
|
use gipfl\IcingaWeb2\Translator;
|
|
|
|
|
use gipfl\IcingaWeb2\Link;
|
|
|
|
|
use gipfl\IcingaWeb2\Widget\ControlsAndContent;
|
|
|
|
|
use gipfl\IcingaWeb2\Controller\Extension\ControlsAndContentHelper;
|
|
|
|
|
use gipfl\IcingaWeb2\Zf1\SimpleViewRenderer;
|
|
|
|
|
use GuzzleHttp\Psr7\ServerRequest;
|
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
2015-04-24 08:26:44 -04:00
|
|
|
|
2017-06-20 18:31:40 -04:00
|
|
|
abstract class ActionController extends Controller implements ControlsAndContent
|
2015-04-24 08:26:44 -04:00
|
|
|
{
|
2017-06-20 18:31:40 -04:00
|
|
|
use DirectorDb;
|
|
|
|
|
use CoreApi;
|
|
|
|
|
use RestApi;
|
|
|
|
|
use ControlsAndContentHelper;
|
2015-04-24 08:26:44 -04:00
|
|
|
|
2015-12-10 07:06:40 -05:00
|
|
|
protected $isApified = false;
|
|
|
|
|
|
2017-07-04 23:46:16 -04:00
|
|
|
/** @var UrlParams Hint for IDE, somehow does not work in web */
|
|
|
|
|
protected $params;
|
|
|
|
|
|
2023-11-17 06:56:07 -05:00
|
|
|
/** @var BackendInterface */
|
2022-10-12 11:08:55 -04:00
|
|
|
private $backend;
|
2016-03-20 06:18:06 -04:00
|
|
|
|
2018-06-07 17:37:28 -04:00
|
|
|
/**
|
|
|
|
|
* @throws SecurityException
|
2018-06-14 02:41:43 -04:00
|
|
|
* @throws \Icinga\Exception\AuthenticationException
|
2018-06-07 17:37:28 -04:00
|
|
|
* @throws \Icinga\Exception\NotFoundError
|
|
|
|
|
*/
|
2015-12-10 07:06:40 -05:00
|
|
|
public function init()
|
|
|
|
|
{
|
2024-10-22 08:31:14 -04:00
|
|
|
if (
|
|
|
|
|
! $this->getRequest()->isApiRequest()
|
2019-10-31 10:43:44 -04:00
|
|
|
&& $this->Config()->get('frontend', 'disabled', 'no') === 'yes'
|
|
|
|
|
) {
|
|
|
|
|
throw new NotFoundError('Not found');
|
|
|
|
|
}
|
2017-08-21 18:13:41 -04:00
|
|
|
$this->initializeTranslator();
|
2017-08-16 09:30:46 -04:00
|
|
|
Benchmark::measure('Director base Controller init()');
|
2017-06-20 18:31:40 -04:00
|
|
|
$this->checkForRestApiRequest();
|
2016-11-03 11:06:18 -04:00
|
|
|
$this->checkDirectorPermissions();
|
2017-10-10 11:30:50 -04:00
|
|
|
$this->checkSpecialDirectorPermissions();
|
2016-11-03 11:06:18 -04:00
|
|
|
}
|
|
|
|
|
|
2017-08-21 18:13:41 -04:00
|
|
|
protected function initializeTranslator()
|
|
|
|
|
{
|
2021-11-24 05:56:18 -05:00
|
|
|
StaticTranslator::set(new Translator('director'));
|
2017-08-21 18:13:41 -04:00
|
|
|
}
|
|
|
|
|
|
2017-06-20 18:31:40 -04:00
|
|
|
public function getAuth()
|
|
|
|
|
{
|
|
|
|
|
return $this->Auth();
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-17 05:58:28 -05:00
|
|
|
/**
|
|
|
|
|
* @codingStandardsIgnoreStart
|
|
|
|
|
* @return Window
|
|
|
|
|
*/
|
|
|
|
|
public function Window()
|
|
|
|
|
{
|
|
|
|
|
// @codingStandardsIgnoreEnd
|
|
|
|
|
if ($this->window === null) {
|
|
|
|
|
$this->window = new Window(
|
|
|
|
|
$this->_request->getHeader('X-Icinga-WindowId', Window::UNDEFINED)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return $this->window;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-07 17:37:28 -04:00
|
|
|
/**
|
|
|
|
|
* @throws SecurityException
|
|
|
|
|
*/
|
2016-11-03 11:06:18 -04:00
|
|
|
protected function checkDirectorPermissions()
|
|
|
|
|
{
|
|
|
|
|
$this->assertPermission('director/admin');
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-07 17:37:28 -04:00
|
|
|
/**
|
|
|
|
|
* @throws SecurityException
|
|
|
|
|
*/
|
2017-10-10 11:30:50 -04:00
|
|
|
protected function checkSpecialDirectorPermissions()
|
|
|
|
|
{
|
|
|
|
|
if ($this->params->get('format') === 'sql') {
|
|
|
|
|
$this->assertPermission('director/showsql');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-03 11:06:18 -04:00
|
|
|
/**
|
|
|
|
|
* Assert that the current user has one of the given permission
|
|
|
|
|
*
|
2017-10-07 11:25:18 -04:00
|
|
|
* @param array $permissions Permission name list
|
2016-11-03 11:06:18 -04:00
|
|
|
*
|
2017-10-07 11:25:18 -04:00
|
|
|
* @return $this
|
2016-11-03 11:06:18 -04:00
|
|
|
* @throws SecurityException If the current user lacks the given permission
|
|
|
|
|
*/
|
|
|
|
|
protected function assertOneOfPermissions($permissions)
|
|
|
|
|
{
|
|
|
|
|
$auth = $this->Auth();
|
|
|
|
|
|
2017-01-13 13:47:54 -05:00
|
|
|
foreach ($permissions as $permission) {
|
|
|
|
|
if ($auth->hasPermission($permission)) {
|
2017-10-07 11:25:18 -04:00
|
|
|
return $this;
|
2017-01-13 13:47:54 -05:00
|
|
|
}
|
2016-11-03 11:06:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new SecurityException(
|
|
|
|
|
'Got none of the following permissions: %s',
|
|
|
|
|
implode(', ', $permissions)
|
|
|
|
|
);
|
2015-12-10 07:06:40 -05:00
|
|
|
}
|
|
|
|
|
|
2017-06-20 18:31:40 -04:00
|
|
|
/**
|
|
|
|
|
* @param int $interval
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
|
|
|
|
public function setAutorefreshInterval($interval)
|
2015-12-10 07:06:40 -05:00
|
|
|
{
|
2017-06-20 18:31:40 -04:00
|
|
|
if (! $this->getRequest()->isApiRequest()) {
|
2018-06-07 17:37:28 -04:00
|
|
|
try {
|
|
|
|
|
parent::setAutorefreshInterval($interval);
|
|
|
|
|
} catch (ProgrammingError $e) {
|
|
|
|
|
throw new InvalidArgumentException($e->getMessage());
|
|
|
|
|
}
|
2017-06-20 18:31:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this;
|
2015-12-10 07:06:40 -05:00
|
|
|
}
|
|
|
|
|
|
2019-05-02 07:23:06 -04:00
|
|
|
/**
|
|
|
|
|
* @return ServerRequestInterface
|
|
|
|
|
*/
|
|
|
|
|
protected function getServerRequest()
|
|
|
|
|
{
|
|
|
|
|
return ServerRequest::fromGlobals();
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-23 10:19:22 -04:00
|
|
|
protected function applyPaginationLimits(Paginatable $paginatable, $limit = 25, $offset = null)
|
|
|
|
|
{
|
|
|
|
|
$limit = $this->params->get('limit', $limit);
|
|
|
|
|
$page = $this->params->get('page', $offset);
|
|
|
|
|
|
|
|
|
|
$paginatable->limit($limit, $page > 0 ? ($page - 1) * $limit : 0);
|
|
|
|
|
|
|
|
|
|
return $paginatable;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-16 09:30:46 -04:00
|
|
|
protected function addAddLink($title, $url, $urlParams = null, $target = '_next')
|
|
|
|
|
{
|
|
|
|
|
$this->actions()->add(Link::create(
|
|
|
|
|
$this->translate('Add'),
|
|
|
|
|
$url,
|
|
|
|
|
$urlParams,
|
|
|
|
|
[
|
|
|
|
|
'class' => 'icon-plus',
|
|
|
|
|
'title' => $title,
|
|
|
|
|
'data-base-target' => $target
|
|
|
|
|
]
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function addBackLink($url, $urlParams = null)
|
|
|
|
|
{
|
|
|
|
|
$this->actions()->add(new Link(
|
|
|
|
|
$this->translate('back'),
|
|
|
|
|
$url,
|
|
|
|
|
$urlParams,
|
|
|
|
|
['class' => 'icon-left-big']
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-26 11:39:36 -04:00
|
|
|
protected function sendUnsupportedMethod()
|
|
|
|
|
{
|
|
|
|
|
$method = strtoupper($this->getRequest()->getMethod()) ;
|
|
|
|
|
$response = $this->getResponse();
|
|
|
|
|
$this->sendJsonError($response, sprintf(
|
|
|
|
|
'Method %s is not supported',
|
|
|
|
|
$method
|
|
|
|
|
), 422); // TODO: check response code
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-20 18:31:40 -04:00
|
|
|
/**
|
|
|
|
|
* @param string $permission
|
|
|
|
|
* @return $this
|
2018-06-07 17:37:28 -04:00
|
|
|
* @throws SecurityException
|
2017-06-20 18:31:40 -04:00
|
|
|
*/
|
|
|
|
|
public function assertPermission($permission)
|
|
|
|
|
{
|
|
|
|
|
parent::assertPermission($permission);
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function postDispatch()
|
2015-04-24 08:26:44 -04:00
|
|
|
{
|
2017-08-16 09:30:46 -04:00
|
|
|
Benchmark::measure('Director postDispatch');
|
2017-06-20 18:31:40 -04:00
|
|
|
if ($this->view->content || $this->view->controls) {
|
|
|
|
|
$viewRenderer = new SimpleViewRenderer();
|
|
|
|
|
$viewRenderer->replaceZendViewRenderer();
|
|
|
|
|
$this->view = $viewRenderer->view;
|
2021-07-12 19:00:40 -04:00
|
|
|
// Hint -> $this->view->compact is the only way since v2.8.0
|
|
|
|
|
if ($this->view->compact || $this->getOriginalUrl()->getParam('view') === 'compact') {
|
2017-09-03 03:35:09 -04:00
|
|
|
if ($this->view->controls) {
|
2024-01-11 07:10:17 -05:00
|
|
|
$this->controls()->getAttributes()->add('class', 'compact');
|
2017-09-03 03:35:09 -04:00
|
|
|
}
|
|
|
|
|
}
|
2017-06-26 18:23:36 -04:00
|
|
|
} else {
|
|
|
|
|
$viewRenderer = null;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-09 08:11:48 -04:00
|
|
|
$cType = $this->getResponse()->getHeader('Content-Type', true);
|
|
|
|
|
if ($this->getRequest()->isApiRequest() || ($cType !== null && $cType !== 'text/html')) {
|
2017-06-26 18:23:36 -04:00
|
|
|
$this->_helper->layout()->disableLayout();
|
|
|
|
|
if ($viewRenderer) {
|
|
|
|
|
$viewRenderer->disable();
|
|
|
|
|
} else {
|
|
|
|
|
$this->_helper->viewRenderer->setNoRender(true);
|
|
|
|
|
}
|
2015-04-24 08:26:44 -04:00
|
|
|
}
|
|
|
|
|
|
2017-06-20 18:31:40 -04:00
|
|
|
parent::postDispatch(); // TODO: Change the autogenerated stub
|
2015-04-24 08:26:44 -04:00
|
|
|
}
|
2016-03-20 06:18:06 -04:00
|
|
|
|
2016-11-01 13:28:36 -04:00
|
|
|
/**
|
2023-11-17 06:56:07 -05:00
|
|
|
* @return BackendInterface
|
2016-11-01 13:28:36 -04:00
|
|
|
*/
|
2023-11-17 06:56:07 -05:00
|
|
|
protected function backend(): BackendInterface
|
2016-03-20 06:18:06 -04:00
|
|
|
{
|
2022-10-12 11:08:55 -04:00
|
|
|
if ($this->backend === null) {
|
2023-11-21 07:07:16 -05:00
|
|
|
if (Module::exists('icingadb') && IcingadbSupport::useIcingaDbAsBackend()) {
|
2023-11-17 06:56:07 -05:00
|
|
|
$this->backend = new IcingadbBackend();
|
2023-11-17 04:27:25 -05:00
|
|
|
} else {
|
2023-11-17 07:43:08 -05:00
|
|
|
$this->backend = new Monitoring($this->getAuth());
|
2023-11-17 04:27:25 -05:00
|
|
|
}
|
2016-03-20 06:18:06 -04:00
|
|
|
}
|
|
|
|
|
|
2022-10-12 11:08:55 -04:00
|
|
|
return $this->backend;
|
2016-03-20 06:18:06 -04:00
|
|
|
}
|
2015-04-24 08:26:44 -04:00
|
|
|
}
|