[icinga-checkins] icinga.org: icingaweb2/feature/security-gui-5647: security: Add actions for creating, updating and removing restrictions

git at icinga.org git at icinga.org
Thu Nov 6 17:41:08 CET 2014


Module: icingaweb2
Branch: feature/security-gui-5647
Commit: ebd77ee5c10145a8ebb82968c44c441a3db29f5c
URL:    https://git.icinga.org/?p=icingaweb2.git;a=commit;h=ebd77ee5c10145a8ebb82968c44c441a3db29f5c

Author: Eric Lippmann <eric.lippmann at netways.de>
Date:   Thu Nov  6 17:39:20 2014 +0100

security: Add actions for creating, updating and removing restrictions

refs #5647

---

 application/controllers/SecurityController.php |  112 ++++++++++++++++++++++++
 1 file changed, 112 insertions(+)

diff --git a/application/controllers/SecurityController.php b/application/controllers/SecurityController.php
index c3f9dc6..bcd83a5 100644
--- a/application/controllers/SecurityController.php
+++ b/application/controllers/SecurityController.php
@@ -5,6 +5,7 @@
 use Icinga\Application\Config;
 use Icinga\Form\ConfirmRemovalForm;
 use Icinga\Form\Security\PermissionForm;
+use Icinga\Form\Security\RestrictionForm;
 use Icinga\Web\Controller\ActionController;
 use Icinga\Web\Notification;
 use Icinga\Web\Request;
@@ -127,4 +128,115 @@ class SecurityController extends ActionController
         $this->view->name = $name;
         $this->view->form = $confirmation;
     }
+
+    public function newRestrictionAction()
+    {
+        $restriction = new RestrictionForm(array(
+            'onSuccess' => function (Request $request, RestrictionForm $restriction) {
+                $name = $restriction->getElement('name')->getValue();
+                $values = $restriction->getValues();
+                try {
+                    $restriction->add($name, $values);
+                } catch (InvalidArgumentException $e) {
+                    $restriction->addError($e->getMessage());
+                    return false;
+                }
+                if ($restriction->save()) {
+                    Notification::success(t('Restriction set'));
+                    return true;
+                }
+                return false;
+            }
+        ));
+        $restriction
+            ->setIniConfig(Config::app('restrictions', true))
+            ->setRedirectUrl('security')
+            ->handleRequest();
+        $this->view->form = $restriction;
+    }
+
+    public function updateRestrictionAction()
+    {
+        $name = $this->_request->getParam('restriction');
+        if (empty($name)) {
+            throw new Zend_Controller_Action_Exception(
+                sprintf($this->translate('Required parameter \'%s\' missing'), 'restriction'),
+                400
+            );
+        }
+        $restriction = new RestrictionForm();
+        try {
+            $restriction
+                ->setIniConfig(Config::app('restrictions', true))
+                ->load($name);
+        } catch (InvalidArgumentException $e) {
+            throw new Zend_Controller_Action_Exception(
+                $e->getMessage(),
+                400
+            );
+        }
+        $restriction
+            ->setOnSuccess(function (Request $request, RestrictionForm $restriction) use ($name) {
+                $oldName = $name;
+                $name = $restriction->getElement('name')->getValue();
+                $values = $restriction->getValues();
+                try {
+                    $restriction->update($name, $values, $oldName);
+                } catch (InvalidArgumentException $e) {
+                    $restriction->addError($e->getMessage());
+                    return false;
+                }
+                if ($restriction->save()) {
+                    Notification::success(t('Restriction set'));
+                    return true;
+                }
+                return false;
+            })
+            ->setRedirectUrl('security')
+            ->handleRequest();
+        $this->view->name = $name;
+        $this->view->form = $restriction;
+    }
+
+    public function removeRestrictionAction()
+    {
+        $name = $this->_request->getParam('restriction');
+        if (empty($name)) {
+            throw new Zend_Controller_Action_Exception(
+                sprintf($this->translate('Required parameter \'%s\' missing'), 'restriction'),
+                400
+            );
+        }
+        $restriction = new RestrictionForm();
+        try {
+            $restriction
+                ->setIniConfig(Config::app('restrictions', true))
+                ->load($name);
+        } catch (InvalidArgumentException $e) {
+            throw new Zend_Controller_Action_Exception(
+                $e->getMessage(),
+                400
+            );
+        }
+        $confirmation = new ConfirmRemovalForm(array(
+            'onSuccess' => function (Request $request, ConfirmRemovalForm $confirmation) use ($name, $restriction) {
+                try {
+                    $restriction->remove($name);
+                } catch (InvalidArgumentException $e) {
+                    Notification::error($e->getMessage());
+                    return false;
+                }
+                if ($restriction->save()) {
+                    Notification::success(sprintf(t('Restriction \'%s\' has been successfully removed'), $name));
+                    return true;
+                }
+                return false;
+            }
+        ));
+        $confirmation
+            ->setRedirectUrl('security')
+            ->handleRequest();
+        $this->view->name = $name;
+        $this->view->form = $confirmation;
+    }
 }



More information about the icinga-checkins mailing list