From 7f6ae13b641c4a918411183e2ab14b2b007e17d4 Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Tue, 6 Jan 2015 11:32:43 +0200 Subject: [PATCH] action filters --- ipf/admin/controllers/components.php | 70 +++++++++++++++----------- ipf/admin/controllers/file_browser.php | 20 ++------ ipf/controller/base.php | 39 +++++++++++++- 3 files changed, 84 insertions(+), 45 deletions(-) diff --git a/ipf/admin/controllers/components.php b/ipf/admin/controllers/components.php index 268333f..6427217 100644 --- a/ipf/admin/controllers/components.php +++ b/ipf/admin/controllers/components.php @@ -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); } } diff --git a/ipf/admin/controllers/file_browser.php b/ipf/admin/controllers/file_browser.php index 06e31b1..a65e169 100644 --- a/ipf/admin/controllers/file_browser.php +++ b/ipf/admin/controllers/file_browser.php @@ -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]); diff --git a/ipf/controller/base.php b/ipf/controller/base.php index 669d6d9..916b4a3 100644 --- a/ipf/controller/base.php +++ b/ipf/controller/base.php @@ -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; } } -- 2.49.0