mirror of
https://github.com/Icinga/icingaweb2-module-director.git
synced 2025-09-26 11:19:16 +02:00
ActionController: use traits, less code
This commit is contained in:
parent
c99673f591
commit
117eeee67e
@ -2,16 +2,15 @@
|
|||||||
|
|
||||||
namespace Icinga\Module\Director\Web\Controller;
|
namespace Icinga\Module\Director\Web\Controller;
|
||||||
|
|
||||||
|
use Icinga\Application\Benchmark;
|
||||||
use Icinga\Data\Paginatable;
|
use Icinga\Data\Paginatable;
|
||||||
use Icinga\Exception\AuthenticationException;
|
|
||||||
use Icinga\Exception\ConfigurationError;
|
|
||||||
use Icinga\Exception\NotFoundError;
|
use Icinga\Exception\NotFoundError;
|
||||||
use Icinga\Module\Director\Core\CoreApi;
|
|
||||||
use Icinga\Module\Director\Db;
|
use Icinga\Module\Director\Db;
|
||||||
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
|
|
||||||
use Icinga\Module\Director\Monitoring;
|
use Icinga\Module\Director\Monitoring;
|
||||||
use Icinga\Module\Director\Objects\IcingaEndpoint;
|
|
||||||
use Icinga\Module\Director\Objects\IcingaObject;
|
use Icinga\Module\Director\Objects\IcingaObject;
|
||||||
|
use Icinga\Module\Director\Web\Controller\Extension\CoreApi;
|
||||||
|
use Icinga\Module\Director\Web\Controller\Extension\DirectorDb;
|
||||||
|
use Icinga\Module\Director\Web\Controller\Extension\RestApi;
|
||||||
use Icinga\Module\Director\Web\Form\FormLoader;
|
use Icinga\Module\Director\Web\Form\FormLoader;
|
||||||
use Icinga\Module\Director\Web\Form\QuickBaseForm;
|
use Icinga\Module\Director\Web\Form\QuickBaseForm;
|
||||||
use Icinga\Module\Director\Web\Table\QuickTable;
|
use Icinga\Module\Director\Web\Table\QuickTable;
|
||||||
@ -19,37 +18,33 @@ use Icinga\Module\Director\Web\Table\TableLoader;
|
|||||||
use Icinga\Security\SecurityException;
|
use Icinga\Security\SecurityException;
|
||||||
use Icinga\Web\Controller;
|
use Icinga\Web\Controller;
|
||||||
use Icinga\Web\Widget;
|
use Icinga\Web\Widget;
|
||||||
|
use ipl\Web\Component\ControlsAndContent;
|
||||||
|
use ipl\Web\Controller\Extension\ControlsAndContentHelper;
|
||||||
|
use ipl\Zf1\SimpleViewRenderer;
|
||||||
|
|
||||||
abstract class ActionController extends Controller
|
abstract class ActionController extends Controller implements ControlsAndContent
|
||||||
{
|
{
|
||||||
/** @var Db */
|
use DirectorDb;
|
||||||
protected $db;
|
use CoreApi;
|
||||||
|
use RestApi;
|
||||||
|
use ControlsAndContentHelper;
|
||||||
|
|
||||||
protected $isApified = false;
|
protected $isApified = false;
|
||||||
|
|
||||||
/** @var CoreApi */
|
|
||||||
private $api;
|
|
||||||
|
|
||||||
/** @var Monitoring */
|
/** @var Monitoring */
|
||||||
private $monitoring;
|
private $monitoring;
|
||||||
|
|
||||||
protected $icingaConfig;
|
|
||||||
|
|
||||||
public function init()
|
public function init()
|
||||||
{
|
{
|
||||||
if ($this->getRequest()->isApiRequest()) {
|
$this->checkForRestApiRequest();
|
||||||
if (! $this->hasPermission('director/api')) {
|
|
||||||
throw new AuthenticationException('You are not allowed to access this API');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (! $this->isApified()) {
|
|
||||||
throw new NotFoundError('No such API endpoint found');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->checkDirectorPermissions();
|
$this->checkDirectorPermissions();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getAuth()
|
||||||
|
{
|
||||||
|
return $this->Auth();
|
||||||
|
}
|
||||||
|
|
||||||
protected function checkDirectorPermissions()
|
protected function checkDirectorPermissions()
|
||||||
{
|
{
|
||||||
$this->assertPermission('director/admin');
|
$this->assertPermission('director/admin');
|
||||||
@ -78,9 +73,17 @@ abstract class ActionController extends Controller
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function isApified()
|
/**
|
||||||
|
* @param int $interval
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setAutorefreshInterval($interval)
|
||||||
{
|
{
|
||||||
return $this->isApified;
|
if (! $this->getRequest()->isApiRequest()) {
|
||||||
|
parent::setAutorefreshInterval($interval);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function applyPaginationLimits(Paginatable $paginatable, $limit = 25, $offset = null)
|
protected function applyPaginationLimits(Paginatable $paginatable, $limit = 25, $offset = null)
|
||||||
@ -119,27 +122,6 @@ abstract class ActionController extends Controller
|
|||||||
return TableLoader::load($name, $this->Module());
|
return TableLoader::load($name, $this->Module());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function sendJson($object)
|
|
||||||
{
|
|
||||||
$this->getResponse()->setHeader('Content-Type', 'application/json', true);
|
|
||||||
$this->_helper->layout()->disableLayout();
|
|
||||||
$this->_helper->viewRenderer->setNoRender(true);
|
|
||||||
if (version_compare(PHP_VERSION, '5.4.0') >= 0) {
|
|
||||||
echo json_encode($object, JSON_PRETTY_PRINT) . "\n";
|
|
||||||
} else {
|
|
||||||
echo json_encode($object);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function sendJsonError($message, $code = null)
|
|
||||||
{
|
|
||||||
if ($code !== null) {
|
|
||||||
$this->setHttpResponseCode((int) $code);
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->sendJson((object) array('error' => $message));
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function singleTab($label)
|
protected function singleTab($label)
|
||||||
{
|
{
|
||||||
return $this->view->tabs = Widget::create('tabs')->add(
|
return $this->view->tabs = Widget::create('tabs')->add(
|
||||||
@ -193,6 +175,16 @@ abstract class ActionController extends Controller
|
|||||||
return $this->view->tabs;
|
return $this->view->tabs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $permission
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function assertPermission($permission)
|
||||||
|
{
|
||||||
|
parent::assertPermission($permission);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
protected function setImportTabs()
|
protected function setImportTabs()
|
||||||
{
|
{
|
||||||
$this->view->tabs = Widget::create('tabs')->add(
|
$this->view->tabs = Widget::create('tabs')->add(
|
||||||
@ -317,84 +309,53 @@ abstract class ActionController extends Controller
|
|||||||
throw new NotFoundError('Not accessible via API');
|
throw new NotFoundError('Not accessible via API');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->getResponse()->setHeader('Content-Type', 'application/json', true);
|
||||||
|
$this->_helper->layout()->disableLayout();
|
||||||
|
$this->_helper->viewRenderer->setNoRender(true);
|
||||||
|
|
||||||
|
echo '{ "objects": [' . "\n";
|
||||||
$objects = array();
|
$objects = array();
|
||||||
foreach ($dummy::loadAll($this->db) as $object) {
|
Db\Cache\PrefetchCache::initialize($this->db());
|
||||||
$objects[] = $object->toPlainObject(false, true);
|
|
||||||
|
$out = '';
|
||||||
|
$cnt = 0;
|
||||||
|
foreach ($dummy::prefetchAll($this->db) as $object) {
|
||||||
|
// $objects[] = $object->toPlainObject(false, true);
|
||||||
|
// continue;
|
||||||
|
$out .= json_encode($object->toPlainObject(false, true), JSON_PRETTY_PRINT) . "\n";
|
||||||
|
$cnt++;
|
||||||
|
if ($cnt > 50) {
|
||||||
|
echo $out;
|
||||||
|
flush();
|
||||||
|
$cnt = 0;
|
||||||
|
$out = '';
|
||||||
}
|
}
|
||||||
return $this->sendJson((object) array('objects' => $objects));
|
}
|
||||||
|
|
||||||
|
if ($cnt > 0) {
|
||||||
|
echo $out;
|
||||||
|
}
|
||||||
|
|
||||||
|
echo "] }\n";
|
||||||
|
Benchmark::measure('All done');
|
||||||
|
// $this->sendJson((object) array('objects' => $objects));
|
||||||
|
echo Benchmark::dump();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->view->table = $this->applyPaginationLimits($table);
|
$this->view->table = $this->applyPaginationLimits($table);
|
||||||
$this->provideQuickSearch();
|
$this->provideQuickSearch();
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: just return json_last_error_msg() for PHP >= 5.5.0
|
public function postDispatch()
|
||||||
protected function getLastJsonError()
|
|
||||||
{
|
{
|
||||||
switch (json_last_error()) {
|
if ($this->view->content || $this->view->controls) {
|
||||||
case JSON_ERROR_DEPTH:
|
$viewRenderer = new SimpleViewRenderer();
|
||||||
return 'The maximum stack depth has been exceeded';
|
$viewRenderer->replaceZendViewRenderer();
|
||||||
case JSON_ERROR_CTRL_CHAR:
|
$this->view = $viewRenderer->view;
|
||||||
return 'Control character error, possibly incorrectly encoded';
|
|
||||||
case JSON_ERROR_STATE_MISMATCH:
|
|
||||||
return 'Invalid or malformed JSON';
|
|
||||||
case JSON_ERROR_SYNTAX:
|
|
||||||
return 'Syntax error';
|
|
||||||
case JSON_ERROR_UTF8:
|
|
||||||
return 'Malformed UTF-8 characters, possibly incorrectly encoded';
|
|
||||||
default:
|
|
||||||
return 'An error occured when parsing a JSON string';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getApiIfAvailable()
|
parent::postDispatch(); // TODO: Change the autogenerated stub
|
||||||
{
|
|
||||||
if ($this->api === null) {
|
|
||||||
if ($this->db->hasDeploymentEndpoint()) {
|
|
||||||
$endpoint = $this->db()->getDeploymentEndpoint();
|
|
||||||
$this->api = $endpoint->api();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->api;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function api($endpointName = null)
|
|
||||||
{
|
|
||||||
if ($this->api === null) {
|
|
||||||
if ($endpointName === null) {
|
|
||||||
$endpoint = $this->db()->getDeploymentEndpoint();
|
|
||||||
} else {
|
|
||||||
$endpoint = IcingaEndpoint::load($endpointName, $this->db());
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->api = $endpoint->api();
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->api;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws ConfigurationError
|
|
||||||
*
|
|
||||||
* @return Db
|
|
||||||
*/
|
|
||||||
protected function db()
|
|
||||||
{
|
|
||||||
if ($this->db === null) {
|
|
||||||
$resourceName = $this->Config()->get('db', 'resource');
|
|
||||||
if ($resourceName) {
|
|
||||||
$this->db = Db::fromResourceName($resourceName);
|
|
||||||
} else {
|
|
||||||
if ($this->getRequest()->isApiRequest()) {
|
|
||||||
throw new ConfigurationError('Icinga Director is not correctly configured');
|
|
||||||
} else {
|
|
||||||
$this->redirectNow('director');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->db;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user