ObjectController: adjust to fit new base controller

This commit is contained in:
Thomas Gelf 2017-06-22 00:44:31 +02:00
parent 31d1d44da5
commit 6597cd2027
1 changed files with 18 additions and 21 deletions

View File

@ -8,10 +8,13 @@ use Icinga\Exception\InvalidPropertyException;
use Icinga\Exception\NotFoundError; use Icinga\Exception\NotFoundError;
use Icinga\Module\Director\Exception\NestingError; use Icinga\Module\Director\Exception\NestingError;
use Icinga\Module\Director\Objects\IcingaObject; use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Web\Controller\Extension\ObjectRestrictions;
use Icinga\Module\Director\Web\Form\DirectorObjectForm; use Icinga\Module\Director\Web\Form\DirectorObjectForm;
abstract class ObjectController extends ActionController abstract class ObjectController extends ActionController
{ {
use ObjectRestrictions;
/** @var IcingaObject */ /** @var IcingaObject */
protected $object; protected $object;
@ -22,16 +25,6 @@ abstract class ObjectController extends ActionController
'endpoint' 'endpoint'
); );
protected function loadRestrictions()
{
return array();
}
protected function allowsObject(IcingaObject $object)
{
return true;
}
public function init() public function init()
{ {
parent::init(); parent::init();
@ -40,16 +33,16 @@ abstract class ObjectController extends ActionController
$response = $this->getResponse(); $response = $this->getResponse();
try { try {
$this->loadObject(); $this->loadObject();
return $this->handleApiRequest(); $this->handleApiRequest();
} catch (NotFoundError $e) { } catch (NotFoundError $e) {
$response->setHttpResponseCode(404); $response->setHttpResponseCode(404);
return $this->sendJson((object) array('error' => $e->getMessage())); $this->sendJson($response, (object) array('error' => $e->getMessage()));
} catch (Exception $e) { } catch (Exception $e) {
if ($response->getHttpResponseCode() === 200) { if ($response->getHttpResponseCode() === 200) {
$response->setHttpResponseCode(500); $response->setHttpResponseCode(500);
} }
return $this->sendJson((object) array('error' => $e->getMessage())); $this->sendJson($response, (object) array('error' => $e->getMessage()));
} }
} }
@ -119,7 +112,7 @@ abstract class ObjectController extends ActionController
); );
} }
return $this->editAction(); $this->editAction();
} }
public function renderAction() public function renderAction()
@ -178,8 +171,9 @@ abstract class ObjectController extends ActionController
$this->view->form = $form = $this->loadForm($formName) $this->view->form = $form = $this->loadForm($formName)
->setDb($this->db()) ->setDb($this->db())
->setApi($this->getApiIfAvailable()); ->setApi($this->getApiIfAvailable());
/** @var DirectorObjectForm */
$form->setObject($object); $form->setObject($object);
$form->setObjectRestrictions($this->loadRestrictions()); $form->setAuth($this->Auth());
$this->view->title = $object->object_name; $this->view->title = $object->object_name;
$this->view->form->handleRequest(); $this->view->form->handleRequest();
@ -279,7 +273,7 @@ abstract class ObjectController extends ActionController
$form = $this->view->form = $this $form = $this->view->form = $this
->loadForm('icingaObjectField') ->loadForm('icingaObjectField')
->setDb($this->db) ->setDb($this->db())
->setIcingaObject($object); ->setIcingaObject($object);
if ($id = $this->params->get('field_id')) { if ($id = $this->params->get('field_id')) {
@ -393,13 +387,13 @@ abstract class ObjectController extends ActionController
switch ($request->getMethod()) { switch ($request->getMethod()) {
case 'DELETE': case 'DELETE':
$this->requireObject(); $this->requireObject();
$name = $this->object->object_name;
$obj = $this->object->toPlainObject(false, true); $obj = $this->object->toPlainObject(false, true);
$form = $this->loadForm( $this->loadForm(
'icingaDeleteObject' 'icingaDeleteObject'
)->setObject($this->object)->setRequest($request)->onSuccess(); )->setObject($this->object)->setRequest($request)->onSuccess();
return $this->sendJson($obj); $this->sendJson($this->getResponse(), $obj);
break;
case 'POST': case 'POST':
case 'PUT': case 'PUT':
@ -443,17 +437,20 @@ abstract class ObjectController extends ActionController
$response->setHttpResponseCode(304); $response->setHttpResponseCode(304);
} }
return $this->sendJson($object->toPlainObject(false, true)); $this->sendJson($response, $object->toPlainObject(false, true));
break;
case 'GET': case 'GET':
$this->requireObject(); $this->requireObject();
return $this->sendJson( $this->sendJson(
$this->getResponse(),
$this->object->toPlainObject( $this->object->toPlainObject(
$this->params->shift('resolved'), $this->params->shift('resolved'),
! $this->params->shift('withNull'), ! $this->params->shift('withNull'),
$this->params->shift('properties') $this->params->shift('properties')
) )
); );
break;
default: default:
$request->getResponse()->setHttpResponseCode(400); $request->getResponse()->setHttpResponseCode(400);