[icinga-checkins] icinga.org: icingaweb2-module-director/master: ObjectController: improve response codes for REST

git at icinga.org git at icinga.org
Wed Feb 17 19:35:50 CET 2016


Module: icingaweb2-module-director
Branch: master
Commit: fa85bf071cb81a17f455e93953799270b8bd9efb
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=fa85bf071cb81a17f455e93953799270b8bd9efb

Author: Thomas Gelf <thomas at gelf.net>
Date:   Wed Feb 17 19:22:36 2016 +0100

ObjectController: improve response codes for REST

---

 library/Director/Web/Controller/ObjectController.php |   15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/library/Director/Web/Controller/ObjectController.php b/library/Director/Web/Controller/ObjectController.php
index ea7e0a7..cf125d5 100644
--- a/library/Director/Web/Controller/ObjectController.php
+++ b/library/Director/Web/Controller/ObjectController.php
@@ -3,6 +3,7 @@
 namespace Icinga\Module\Director\Web\Controller;
 
 use Exception;
+use Icinga\Exception\NotFoundError;
 use Icinga\Web\Url;
 use Icinga\Module\Director\Objects\IcingaObject;
 
@@ -265,6 +266,7 @@ abstract class ObjectController extends ActionController
 
         switch ($request->getMethod()) {
             case 'DELETE':
+                $this->requireObject();
                 $name = $this->object->object_name;
                 $obj = $this->object->toPlainObject(false, true);
                 $form = $this->loadForm(
@@ -304,6 +306,7 @@ abstract class ObjectController extends ActionController
                 return $this->sendJson($object->toPlainObject(false, true));
 
             case 'GET':
+                $this->requireObject();
                 return $this->sendJson(
                     $this->object->toPlainObject(
                         $this->params->shift('resolved'),
@@ -316,4 +319,16 @@ abstract class ObjectController extends ActionController
                 throw new Exception('Unsupported method ' . $request->getMethod());
         }
     }
+
+    protected function requireObject()
+    {
+        if (! $this->object) {
+            $this->getResponse()->setHttpResponseCode(404);
+            if (! $this->params->get('name')) {
+                throw new NotFoundError('You need to pass a "name" parameter to access a specific object');
+            } else {
+                throw new NotFoundError('No such object available');
+            }
+        }
+    }
 }



More information about the icinga-checkins mailing list