diff options
author | Bernhard Posselt <nukeawhale@gmail.com> | 2012-11-28 04:29:09 +0400 |
---|---|---|
committer | Bernhard Posselt <nukeawhale@gmail.com> | 2012-11-28 04:29:21 +0400 |
commit | a8c658878bbaab26d975bb043ab7258c10df9e1f (patch) | |
tree | 6326663e0bbba9cb2d04ad2c87102487119d9910 /apptemplate | |
parent | 1e1efcaa6de5c7f2be46ee452d8a866558084369 (diff) |
fixed some bugs in the ajax controller, added response class for JSON and Templates
Diffstat (limited to 'apptemplate')
-rw-r--r-- | apptemplate/admin/settings.php | 3 | ||||
-rw-r--r-- | apptemplate/appinfo/bootstrap.php | 4 | ||||
-rw-r--r-- | apptemplate/appinfo/routes.php | 19 | ||||
-rw-r--r-- | apptemplate/controllers/ajax.controller.php | 9 | ||||
-rw-r--r-- | apptemplate/controllers/index.controller.php | 12 | ||||
-rw-r--r-- | apptemplate/controllers/settings.controller.php | 12 | ||||
-rw-r--r-- | apptemplate/lib/controller.php | 16 | ||||
-rw-r--r-- | apptemplate/lib/response.php | 158 | ||||
-rw-r--r-- | apptemplate/lib/security.php | 14 |
9 files changed, 197 insertions, 50 deletions
diff --git a/apptemplate/admin/settings.php b/apptemplate/admin/settings.php index eb22ad03e..69fe3f51d 100644 --- a/apptemplate/admin/settings.php +++ b/apptemplate/admin/settings.php @@ -29,8 +29,7 @@ require_once \OC_App::getAppPath('apptemplate') . '/appinfo/bootstrap.php'; $container = createDIContainer(); $security = $container['Security']; -$security->setCSRFCheck(false); $security->runChecks(); $controller = $container['SettingsController']; -return $controller->index()->fetchPage(); +return $controller->index()->render(); diff --git a/apptemplate/appinfo/bootstrap.php b/apptemplate/appinfo/bootstrap.php index 7928a906a..696f7650e 100644 --- a/apptemplate/appinfo/bootstrap.php +++ b/apptemplate/appinfo/bootstrap.php @@ -33,6 +33,8 @@ namespace OCA\AppTemplate; \OC::$CLASSPATH['OCA\AppTemplate\Request'] = 'apps/apptemplate/lib/request.php'; \OC::$CLASSPATH['OCA\AppTemplate\Security'] = 'apps/apptemplate/lib/security.php'; \OC::$CLASSPATH['OCA\AppTemplate\Controller'] = 'apps/apptemplate/lib/controller.php'; +\OC::$CLASSPATH['OCA\AppTemplate\TemplateResponse'] = 'apps/apptemplate/lib/response.php'; +\OC::$CLASSPATH['OCA\AppTemplate\JSONResponse'] = 'apps/apptemplate/lib/response.php'; \OC::$CLASSPATH['OCA\AppTemplate\IndexController'] = 'apps/apptemplate/controllers/index.controller.php'; \OC::$CLASSPATH['OCA\AppTemplate\SettingsController'] = 'apps/apptemplate/controllers/settings.controller.php'; @@ -57,7 +59,7 @@ function createDIContainer(){ }); $container['Request'] = $container->share(function($c){ - return new Request($c['API']->getUserId(), $_GET, $_POST); + return new Request($_GET, $_POST); }); diff --git a/apptemplate/appinfo/routes.php b/apptemplate/appinfo/routes.php index 86f173aea..49f020907 100644 --- a/apptemplate/appinfo/routes.php +++ b/apptemplate/appinfo/routes.php @@ -34,20 +34,19 @@ require_once \OC_App::getAppPath('apptemplate') . '/appinfo/bootstrap.php'; * @param array $urlParams: an array with variables extracted from the routes * @param bool $isAjax: if the request is an ajax request * @param bool $disableAdminCheck: disables the check for adminuser rights - * @param bool $disableIsInAdminGroupCheck: disables the check for admin group member */ function callController($controllerName, $methodName, $urlParams, $isAjax=false, - $disableAdminCheck=true, $disableIsInAdminGroupCheck=true){ + $disableAdminCheck=true){ $container = createDIContainer(); // run security checks $security = $container['Security']; - runSecurityChecks($security, $isAjax, $disableAdminCheck, $disableIsInAdminGroupCheck); + runSecurityChecks($security, $isAjax, $disableAdminCheck); // call the controller and render the page $controller = $container[$controllerName]; - $page = $controller->$methodName($urlParams); - $page->printPage(); + $response = $controller->$methodName($urlParams); + echo $response->render(); } @@ -57,18 +56,12 @@ function callController($controllerName, $methodName, $urlParams, $isAjax=false, * @param bool $isAjax: if true, the ajax checks will be run, otherwise the normal * checks * @param bool $disableAdminCheck: disables the check for adminuser rights - * @param bool $disableIsInAdminGroupCheck: disables the check for admin group member */ -function runSecurityChecks($security, $isAjax=false, $disableAdminCheck=true, - $disableIsInAdminGroupCheck=true){ +function runSecurityChecks($security, $isAjax=false, $disableAdminCheck=true){ if($disableAdminCheck){ $security->setIsAdminCheck(false); } - if($disableIsInAdminGroupCheck){ - $security->setIsInAdminGroupCheck(false); - } - if($isAjax){ $security->runAJAXChecks(); } else { @@ -96,4 +89,4 @@ $this->create('apptemplate_ajax_setsystemvalue', '/setsystemvalue')->post()->act function($params){ callController('AjaxController', 'setSystemValue', $params, true); } -);
\ No newline at end of file +); diff --git a/apptemplate/controllers/ajax.controller.php b/apptemplate/controllers/ajax.controller.php index ab6ff54dd..9713af871 100644 --- a/apptemplate/controllers/ajax.controller.php +++ b/apptemplate/controllers/ajax.controller.php @@ -42,8 +42,15 @@ class AjaxController extends Controller { * the routes file */ public function setSystemValue($urlParams=array()){ - $value = $this->params['somesetting']; + $value = $this->params('somesetting'); $this->api->setSystemValue('somesetting', $value); + + $response = new JSONResponse($this->appName); + + $params = array('somesetting' => $value); + $response->setParams($params); + + return $response; } }
\ No newline at end of file diff --git a/apptemplate/controllers/index.controller.php b/apptemplate/controllers/index.controller.php index a7b5912aa..51173f03b 100644 --- a/apptemplate/controllers/index.controller.php +++ b/apptemplate/controllers/index.controller.php @@ -44,13 +44,17 @@ class IndexController extends Controller { * @brief renders the index page * @param array $urlParams: an array with the values, which were matched in * the routes file + * @return an instance of a Response implementation */ public function index($urlParams=array()){ - $template = $this->getTemplate('main'); - $templateVariable = $this->api->getSystemValue('somesetting'); - $template->assign('somesetting', $templateVariable, false); + $response = new TemplateResponse($this->appName, 'main'); + + $params = array( + 'somesetting' => $this->api->getSystemValue('somesetting') + ); + $response->setParams($params); - return $template; + return $response; } }
\ No newline at end of file diff --git a/apptemplate/controllers/settings.controller.php b/apptemplate/controllers/settings.controller.php index 61bc718d6..a1b736bbd 100644 --- a/apptemplate/controllers/settings.controller.php +++ b/apptemplate/controllers/settings.controller.php @@ -43,10 +43,16 @@ class SettingsController extends Controller { */ public function index($urlParams=array()){ $this->api->addScript('admin'); - $template = $this->getTemplate('settings', 'admin'); - $template->assign('url', $this->api->getSystemValue('somesetting')); - return $template; + $response = new TemplateResponse($this->appName, 'settings'); + $response->renderAs('admin'); + + $params = array( + 'url' => $this->api->getSystemValue('somesetting') + ); + $response->setParams($params); + + return $response; } }
\ No newline at end of file diff --git a/apptemplate/lib/controller.php b/apptemplate/lib/controller.php index ad87181d4..1402f8dfd 100644 --- a/apptemplate/lib/controller.php +++ b/apptemplate/lib/controller.php @@ -27,7 +27,9 @@ namespace OCA\AppTemplate; class Controller { protected $api; - protected $request; + protected $appName; + + private $request; /** * @param API $api: an api wrapper instance @@ -36,6 +38,7 @@ class Controller { public function __construct($api, $request){ $this->api = $api; $this->request = $request; + $this->appName = $api->getAppName(); } @@ -57,18 +60,7 @@ class Controller { if($getValue !== null){ return $getValue; } - } - - /** - * @brief Shortcut for creating a template for the current app# - * @param string $templateName: the name of the template in the templates - * directory without the .php suffix - * @param bool $userPage: 'user' renders are normal page, 'admin' an admin page - * @return a new Template instance - */ - protected function getTemplate($templateName, $page='user'){ - return new \OCP\Template($this->api->getAppName(), $templateName, $page); } }
\ No newline at end of file diff --git a/apptemplate/lib/response.php b/apptemplate/lib/response.php new file mode 100644 index 000000000..2038fc27f --- /dev/null +++ b/apptemplate/lib/response.php @@ -0,0 +1,158 @@ +<?php + +/** +* ownCloud - App Template Example +* +* @author Bernhard Posselt +* @copyright 2012 Bernhard Posselt nukeawhale@gmail.com +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE +* License as published by the Free Software Foundation; either +* version 3 of the License, or any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU AFFERO GENERAL PUBLIC LICENSE for more details. +* +* You should have received a copy of the GNU Affero General Public +* License along with this library. If not, see <http://www.gnu.org/licenses/>. +* +*/ + + +namespace OCA\AppTemplate; + +interface Response { + function render(); +} + +/** + * Response for a normal template + */ +class TemplateResponse implements Response { + + private $templateName; + private $params; + private $appName; + private $renderAs; + + /** + * @param string $appName: the name of your app + * @param string $templateName: the name of the template + */ + public function __construct($appName, $templateName) { + $this->templateName = $templateName; + $this->appName = $appName; + $this->params = array(); + $this->renderAs = 'user'; + } + + + /** + * @brief sets template parameters + * @param array $params: an array with key => value structure which sets template + * variables + */ + public function setParams($params){ + $this->params = $params; + } + + + /** + * @brief sets the template page + * @param string $renderAs: admin, user or blank: admin renders the page on + * the admin settings page, user renders a normal + * owncloud page, blank renders the template alone + */ + public function renderAs($renderAs='user'){ + $this->renderAs = $renderAs; + } + + + /** + * Returns the rendered html + * @return the rendered html + */ + public function render(){ + if($this->renderAs === 'blank'){ + $template = new \OCP\Template($this->appName, $this->templateName); + } else { + $template = new \OCP\Template($this->appName, $this->templateName, + $this->renderAs); + } + + foreach($this->params as $key => $value){ + $template->assign($key, $value, false); + } + + return $template->fetchPage(); + } + +} + + +/** + * A renderer for JSON calls + */ +class JSONResponse implements Response { + + private $name; + private $data; + private $appName; + + /** + * @param string $appName: the name of your app + */ + public function __construct($appName) { + $this->appName = $appName; + $this->data = array(); + $this->error = false; + } + + /** + * @brief sets values in the data json array + * @param array $params: an array with key => value structure which will be + * transformed to JSON + */ + public function setParams($params){ + $this->data['data'] = $params; + } + + + /** + * @brief in case we want to render an error message, also logs into the + * owncloud log + * @param string $message: the error message + * @param string $file: the file where the error occured, use __FILE__ in + * the file where you call it + */ + public function setErrorMessage($msg, $file){ + $this->error = true; + $this->data['msg'] = $msg; + \OCP\Util::writeLog($this->appName, $file . ': ' . $msg, \OCP\Util::ERROR); + } + + + /** + * Returns the rendered json + * @return the rendered json + */ + public function render(){ + + ob_start(); + + if($this->error){ + \OCP\JSON::error($this->data); + } else { + \OCP\JSON::success($this->data); + } + + $result = ob_get_contents(); + ob_end_clean(); + + return $result; + } + +}
\ No newline at end of file diff --git a/apptemplate/lib/security.php b/apptemplate/lib/security.php index 2e54dc357..c16702450 100644 --- a/apptemplate/lib/security.php +++ b/apptemplate/lib/security.php @@ -35,7 +35,6 @@ class Security { private $loggedInCheck; private $appEnabledCheck; private $isAdminCheck; - private $isInAdminGroupCheck; private $appName; /** @@ -49,7 +48,6 @@ class Security { $this->loggedInCheck = true; $this->appEnabledCheck = true; $this->isAdminCheck = true; - $this->isInAdminGroupCheck = true; } @@ -69,10 +67,6 @@ class Security { $this->isAdminCheck = $isAdminCheck; } - public function setIsInAdminGroupCheck($isInAdminGroupCheck){ - $this->isInAdminGroupCheck = $isInAdminGroupCheck; - } - /** * Runs all security checks @@ -91,10 +85,6 @@ class Security { \OCP\JSON::checkAdminUser(); } - if($this->isInAdminGroupCheck){ - \OCP\JSON::checkSubAdminUser(); - } - } @@ -118,10 +108,6 @@ class Security { \OCP\JSON::checkAdminUser(); } - if($this->isInAdminGroupCheck){ - \OCP\JSON::checkSubAdminUser(); - } - } |