From 4058e7f48c54c8d4040cae097c483b76e5f6ee29 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 2 Jan 2011 14:23:43 -0500 Subject: [PATCH] Extracting a base class, as it will be needed. Moving AuthComponent::action() into the authorize object base as that's where its used. --- .../components/auth/base_authorize.php | 90 +++++++++++++++++++ .../components/auth/controller_authorize.php | 43 +++------ 2 files changed, 102 insertions(+), 31 deletions(-) create mode 100644 cake/libs/controller/components/auth/base_authorize.php diff --git a/cake/libs/controller/components/auth/base_authorize.php b/cake/libs/controller/components/auth/base_authorize.php new file mode 100644 index 000000000..a7828eae5 --- /dev/null +++ b/cake/libs/controller/components/auth/base_authorize.php @@ -0,0 +1,90 @@ +action(); + * + * @var string + */ + public $actionPath = null; + +/** + * Constructor + * + * @param Controller $controller The controller for this request. + * @param string $settings An array of settings. This class does not use any settings. + */ + public function __construct(Controller $controller, $settings = array()) { + $this->controller($controller); + } + +/** + * Checks user authorization. + * + * @param array $user Active user data + * @param CakeRequest $request + * @return boolean + */ + abstract public function authorize($user, CakeRequest $request); + +/** + * Accessor to the controller object. + * + * @param mixed $controller null to get, a controller to set. + * @return mixed. + */ + public function controller($controller = null) { + if ($controller) { + if (!$controller instanceof Controller) { + throw new CakeException(__('$controller needs to be an instance of Controller')); + } + $this->_controller = $controller; + return true; + } + return $this->_controller; + } + +/** + * Get the action path for a given request. Primarily used by authorize objects + * that need to get information about the plugin, controller, and action being invoked. + * + * @param CakeRequest $request The request a path is needed for. + * @return string the action path for the given request. + */ + public function action($request, $path = '/:plugin/:controller/:action') { + $plugin = empty($request['plugin']) ? null : Inflector::camelize($request['plugin']) . '/'; + return str_replace( + array(':controller', ':action', ':plugin/'), + array(Inflector::camelize($request['controller']), $request['action'], $plugin), + $this->actionPath . $path + ); + } +} \ No newline at end of file diff --git a/cake/libs/controller/components/auth/controller_authorize.php b/cake/libs/controller/components/auth/controller_authorize.php index eea082111..720242e0b 100644 --- a/cake/libs/controller/components/auth/controller_authorize.php +++ b/cake/libs/controller/components/auth/controller_authorize.php @@ -12,6 +12,7 @@ * @link http://cakephp.org CakePHP(tm) Project * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ +App::import('Component', 'auth/base_authorize'); /** * An authorization adapter for AuthComponent. Provides the ability to authorize using a controller callback. @@ -33,22 +34,21 @@ * @since 2.0 * @see AuthComponent::$authenticate */ -class ControllerAuthorize { -/** - * Controller for the request. - * - * @var Controller - */ - protected $_controller = null; +class ControllerAuthorize extends BaseAuthorize { /** - * Constructor + * Get/set the controller this authorize object will be working with. Also checks that isAuthorized is implemented. * - * @param Controller $controller The controller for this request. - * @param string $settings An array of settings. This class does not use any settings. + * @param mixed $controller null to get, a controller to set. + * @return mixed. */ - public function __construct(Controller $controller, $settings = array()) { - $this->controller($controller); + public function controller($controller = null) { + if ($controller) { + if (!method_exists($controller, 'isAuthorized')) { + throw new CakeException(__('$controller does not implement an isAuthorized() method.')); + } + } + return parent::controller($controller); } /** @@ -62,23 +62,4 @@ class ControllerAuthorize { return (bool) $this->_controller->isAuthorized($user); } -/** - * Accessor to the controller object. - * - * @param mixed $controller null to get, a controller to set. - * @return mixed. - */ - public function controller($controller = null) { - if ($controller) { - if (!$controller instanceof Controller) { - throw new CakeException(__('$controller needs to be an instance of Controller')); - } - if (!method_exists($controller, 'isAuthorized')) { - throw new CakeException(__('$controller does not implement an isAuthorized() method.')); - } - $this->_controller = $controller; - return true; - } - return $this->_controller; - } } \ No newline at end of file