]> git.andy128k.dev Git - ipf.git/commitdiff
action filters
authorAndrey Kutejko <andy128k@gmail.com>
Tue, 6 Jan 2015 09:32:43 +0000 (11:32 +0200)
committerAndrey Kutejko <andy128k@gmail.com>
Tue, 6 Jan 2015 09:32:43 +0000 (11:32 +0200)
ipf/admin/controllers/components.php
ipf/admin/controllers/file_browser.php
ipf/controller/base.php

index 268333f34c110f57c9fa7db969046b73c8fb3f06..64272173ec29371b5608a836b2d46107c3a3b360 100644 (file)
@@ -4,66 +4,80 @@ class IPF_Admin_Components_Controller extends IPF_Admin_Base_Controller
 {
     function listItems()
     {
-        $component = $this->getComponent(array('view'));
-        $context = $component->listItems();
-        if ($context instanceof IPF_HTTP_Response)
-            return $context;
-        return $this->render($component->listTemplate(), $context);
+        $this->checkPermissions('view');
+        return array(
+            $this->component->listTemplate(),
+            $this->component->listItems(),
+        );
     }
 
     function addItem()
     {
-        $component = $this->getComponent(array('view', 'add'));
-        $context = $component->addItem();
-        if ($context instanceof IPF_HTTP_Response)
-            return $context;
-        return $this->render($component->addTemplate(), $context);
+        $this->checkPermissions('view', 'add');
+        return array(
+            $this->component->addTemplate(),
+            $this->component->addItem(),
+        );
     }
 
     function editItem()
     {
-        $component = $this->getComponent(array('view', 'change'));
-        $context = $component->editItem($this->params[3]);
-        if ($context instanceof IPF_HTTP_Response)
-            return $context;
-        return $this->render($component->editTemplate(), $context);
+        $this->checkPermissions('view', 'change');
+        return array(
+            $this->component->editTemplate(),
+            $this->component->editItem($this->params[3]),
+        );
     }
 
     function deleteItem()
     {
-        $component = $this->getComponent(array('view', 'delete'));
-        $context = $component->deleteItem($this->params[3]);
-        if ($context instanceof IPF_HTTP_Response)
-            return $context;
-        return $this->render($component->deleteTemplate(), $context);
+        $this->checkPermissions('view', 'delete');
+        return array(
+            $this->component->deleteTemplate(),
+            $this->component->deleteItem($this->params[3]),
+        );
     }
 
     function reorder()
     {
-        $component = $this->getComponent(array('view', 'change'));
+        $this->checkPermissions('view', 'change');
 
         if (!isset($this->request->POST['ids']) || !is_array($this->request->POST['ids']))
             return new IPF_HTTP_Response_NotFound($request);
 
-        if ($component->reorder($this->request->POST['ids']))
+        if ($this->component->reorder($this->request->POST['ids']))
             return new IPF_HTTP_Response_Json("Ok");
         else
             return new IPF_HTTP_Response_Json('Cannot find model');
     }
 
-    protected function getComponent($requiredPermissions)
+    // protected
+
+    protected function before($action)
     {
         $this->ensureUserIsStaff();
 
-        $component = IPF_Admin_App::getComponentBySlugs($this->request->params[1], $this->request->params[2]);
-        if (!$component)
+        $this->component = IPF_Admin_App::getComponentBySlugs($this->request->params[1], $this->request->params[2]);
+        if (!$this->component)
             throw new IPF_HTTP_Error404;
 
-        if (!$component->isAccessible($requiredPermissions, $this->request))
+        $this->component->request = $this->request;
+    }
+
+    protected function checkPermissions()
+    {
+        $requiredPermissions = func_get_args();
+        if (!$this->component->isAccessible($requiredPermissions, $this->request))
             throw new IPF_Admin_AccessDenied;
+    }
 
-        $component->request = $this->request;
-        return $component;
+    protected function after__listItems__addItem__editItem__deleteItem($action, $response)
+    {
+        list($template, $context) = $response;
+        if ($context instanceof IPF_HTTP_Response)
+            return $context;
+        else
+            return $this->render($template, $context);
     }
 }
 
index 06e31b147904b19700de564ed0b9766578dae274..a65e169366feac3d238a4cd0edfc97e19f9ea20c 100644 (file)
@@ -60,11 +60,14 @@ class IPF_Admin_FileBrowser_Controller extends IPF_Admin_Base_Controller
         return $name;
     }
 
-    function index()
+    protected function before($action)
     {
         $this->ensureUserIsStaff();
         $this->setCurrentDir();
+    }
 
+    function index()
+    {
         $request = $this->request;
         $match = $this->params;
         $upload_path = IPF::getUploadPath();
@@ -129,9 +132,6 @@ class IPF_Admin_FileBrowser_Controller extends IPF_Admin_Base_Controller
 
     function rename()
     {
-        $this->ensureUserIsStaff();
-        $this->setCurrentDir();
-
         $old_name = $this->validateName($this->request->POST['old_name']);
         $new_name = $this->validateName($this->request->POST['new_name']);
         rename($this->dir . $old_name, $this->dir . $new_name);
@@ -141,9 +141,6 @@ class IPF_Admin_FileBrowser_Controller extends IPF_Admin_Base_Controller
 
     function move()
     {
-        $this->ensureUserIsStaff();
-        $this->setCurrentDir();
-
         $destination = $this->validatePath(IPF::getUploadPath() . DIRECTORY_SEPARATOR . \PFF\Arr::get($this->request->POST, 'destination', ''));
         $name = $this->validateName($this->request->POST['name']);
         rename($this->dir . $name, $destination . $name);
@@ -153,9 +150,6 @@ class IPF_Admin_FileBrowser_Controller extends IPF_Admin_Base_Controller
 
     function mkdir()
     {
-        $this->ensureUserIsStaff();
-        $this->setCurrentDir();
-
         $name = $this->validateName($this->request->POST['name']);
         mkdir($this->dir . $name);
 
@@ -164,9 +158,6 @@ class IPF_Admin_FileBrowser_Controller extends IPF_Admin_Base_Controller
 
     function delete()
     {
-        $this->ensureUserIsStaff();
-        $this->setCurrentDir();
-
         $name = $this->validateName($this->request->POST['name']);
         IPF_Utils::removeDirectories($this->dir . $name);
 
@@ -175,9 +166,6 @@ class IPF_Admin_FileBrowser_Controller extends IPF_Admin_Base_Controller
 
     function upload()
     {
-        $this->ensureUserIsStaff();
-        $this->setCurrentDir();
-
         $count = count($this->request->FILES['files']['name']);
         for ($i = 0; $i < $count; ++$i) {
             $uploadfile = $this->dir . basename($this->request->FILES['files']['name'][$i]);
index 669d6d978948f353e6fa9a8aa947eef5f0a67432..916b4a3bb6c3c9e8f8c3f06485b2c34e939bd055 100644 (file)
@@ -8,7 +8,44 @@ class IPF_Controller
     {
         $this->request = $request;
         $this->params = $matches;
-        return $this->$action();
+
+        $response = $this->before($action);
+        if ($response instanceof IPF_HTTP_Response)
+            return $response;
+
+        $response = $this->$action();
+
+        return $this->after($action, $response);
+    }
+
+    protected function before($action)
+    {
+        foreach ($this->filtersForAction($action, 'before') as $method) {
+            $response = $this->$method($action);
+            if ($response)
+                return $response;
+        }
+    }
+
+    protected function after($action, $response)
+    {
+        foreach ($this->filtersForAction($action, 'after') as $method) {
+            $response = $this->$method($action, $response);
+        }
+        return $response;
+    }
+
+    private function filtersForAction($action, $when)
+    {
+        $filters = array();
+        foreach (get_class_methods($this) as $method) {
+            if (preg_match('/^'.$when.'((?:__\w+?)+)$/', $method, $actions)) {
+                $actions = array_filter(explode('__', $actions[1]));
+                if (in_array($action, $actions))
+                    $filters[] = $method;
+            }
+        }
+        return $filters;
     }
 }