| 1 |
|
<?php |
| 2 |
|
/** |
| 3 |
|
* Xyster Framework |
| 4 |
|
* |
| 5 |
|
* LICENSE |
| 6 |
|
* |
| 7 |
|
* This source file is subject to the new BSD license that is bundled |
| 8 |
|
* with this package in the file LICENSE.txt. |
| 9 |
|
* It is also available through the world-wide-web at this URL: |
| 10 |
|
* http://www.opensource.org/licenses/bsd-license.php |
| 11 |
|
* If you did not receive a copy of the license and are unable to |
| 12 |
|
* obtain it through the world-wide-web, please send an email |
| 13 |
|
* to xyster@devweblog.org so we can send you a copy immediately. |
| 14 |
|
* |
| 15 |
|
* @category Xyster |
| 16 |
|
* @package Xyster_Controller |
| 17 |
|
* @subpackage Plugins |
| 18 |
|
* @copyright Copyright (c) 2007 Irrational Logic (http://devweblog.org) |
| 19 |
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
| 20 |
|
* @version $Id: Acl.php 93 2007-09-19 21:15:19Z doublecompile $ |
| 21 |
|
*/ |
| 22 |
|
/** |
| 23 |
|
* Zend_Controller_Plugin_Abstract |
| 24 |
|
*/ |
| 25 |
1 |
require_once 'Zend/Controller/Plugin/Abstract.php'; |
| 26 |
|
/** |
| 27 |
|
* @see Xyster_Controller_Request_Resource |
| 28 |
|
*/ |
| 29 |
1 |
require_once 'Xyster/Controller/Request/Resource.php'; |
| 30 |
|
/** |
| 31 |
|
* Authorization plugin |
| 32 |
|
* |
| 33 |
|
* @category Xyster |
| 34 |
|
* @package Xyster_Controller |
| 35 |
|
* @subpackage Plugins |
| 36 |
|
* @copyright Copyright (c) 2007 Irrational Logic (http://devweblog.org) |
| 37 |
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
| 38 |
|
*/ |
| 39 |
|
class Xyster_Controller_Plugin_Acl extends Zend_Controller_Plugin_Abstract |
| 40 |
|
{ |
| 41 |
|
/** |
| 42 |
|
* The acl |
| 43 |
|
* |
| 44 |
|
* @var Zend_Acl |
| 45 |
|
*/ |
| 46 |
|
protected $_acl; |
| 47 |
|
|
| 48 |
|
/** |
| 49 |
|
* Module to use for errors; defaults to default module in dispatcher |
| 50 |
|
* @var string |
| 51 |
|
*/ |
| 52 |
|
protected $_errorModule; |
| 53 |
|
|
| 54 |
|
/** |
| 55 |
|
* Controller to use for errors; defaults to 'error' |
| 56 |
|
* @var string |
| 57 |
|
*/ |
| 58 |
|
protected $_errorController = 'error'; |
| 59 |
|
|
| 60 |
|
/** |
| 61 |
|
* Action to use for errors; defaults to 'error' |
| 62 |
|
* @var string |
| 63 |
|
*/ |
| 64 |
|
protected $_errorAction = 'error'; |
| 65 |
|
|
| 66 |
|
/** |
| 67 |
|
* Creates a new acl plugin |
| 68 |
|
* |
| 69 |
|
* Options may include: |
| 70 |
|
* - module |
| 71 |
|
* - controller |
| 72 |
|
* - action |
| 73 |
|
* |
| 74 |
|
* @param Zend_Acl $acl |
| 75 |
|
* @param array $options |
| 76 |
|
*/ |
| 77 |
|
public function __construct( Zend_Acl $acl, array $options = array()) |
| 78 |
|
{ |
| 79 |
6 |
$this->_acl = $acl; |
| 80 |
6 |
$this->setAccessDenied($options); |
| 81 |
|
} |
| 82 |
|
|
| 83 |
|
/** |
| 84 |
|
* Allows access to an action by a role |
| 85 |
|
* |
| 86 |
|
* Passing null for the role will allow all users to access the action. |
| 87 |
|
* |
| 88 |
|
* Passing null for the module will allow the role access to all actions in |
| 89 |
|
* all controllers in all modules. Specifying a module but leaving |
| 90 |
|
* controller and action null will allow access to all actions in all |
| 91 |
|
* controllers in the specified module. Specifying a module and a |
| 92 |
|
* controller but leaving action null will allow access to all actions in |
| 93 |
|
* the specified controller. |
| 94 |
|
* |
| 95 |
|
* @param string $name The controller action name |
| 96 |
|
* @param Zend_Acl_Role_Interface|string $role |
| 97 |
|
* @return Xyster_Controller_Action_Helper_Acl provides a fluent interface |
| 98 |
|
*/ |
| 99 |
|
public function allow( $role, $module, $controller = null, $action = null ) |
| 100 |
|
{ |
| 101 |
1 |
$resource = $this->_getResource($module, $controller, $action); |
| 102 |
1 |
$this->_acl->allow($role, $resource); |
| 103 |
1 |
return $this; |
| 104 |
|
} |
| 105 |
|
|
| 106 |
|
/** |
| 107 |
|
* Retrieve the current acl plugin action |
| 108 |
|
* |
| 109 |
|
* @return string |
| 110 |
|
*/ |
| 111 |
|
public function getAccessDeniedAction() |
| 112 |
|
{ |
| 113 |
4 |
return $this->_errorAction; |
| 114 |
|
} |
| 115 |
|
|
| 116 |
|
/** |
| 117 |
|
* Retrieve the current acl plugin controller |
| 118 |
|
* |
| 119 |
|
* @return string |
| 120 |
|
*/ |
| 121 |
|
public function getAccessDeniedController() |
| 122 |
|
{ |
| 123 |
4 |
return $this->_errorController; |
| 124 |
|
} |
| 125 |
|
|
| 126 |
|
/** |
| 127 |
|
* Retrieve the current acl plugin module |
| 128 |
|
* |
| 129 |
|
* @return string |
| 130 |
|
*/ |
| 131 |
|
public function getAccessDeniedModule() |
| 132 |
|
{ |
| 133 |
4 |
if (null === $this->_errorModule) { |
| 134 |
2 |
require_once 'Zend/Controller/Front.php'; |
| 135 |
2 |
$this->_errorModule = Zend_Controller_Front::getInstance()->getDispatcher()->getDefaultModule(); |
| 136 |
2 |
} |
| 137 |
4 |
return $this->_errorModule; |
| 138 |
|
} |
| 139 |
|
|
| 140 |
|
/** |
| 141 |
|
* Called before an action is dispatched by Zend_Controller_Dispatcher. |
| 142 |
|
* |
| 143 |
|
* @param Zend_Controller_Request_Abstract $request |
| 144 |
|
*/ |
| 145 |
|
public function preDispatch(Zend_Controller_Request_Abstract $request) |
| 146 |
|
{ |
| 147 |
|
// they should be allowed access to the error display screen, duh |
| 148 |
2 |
$this->_acl->allow(null, |
| 149 |
2 |
$this->_getResource($this->getAccessDeniedModule(), |
| 150 |
2 |
$this->getAccessDeniedController(), |
| 151 |
2 |
$this->getAccessDeniedAction())); |
| 152 |
|
|
| 153 |
2 |
$request = $this->getRequest(); |
| 154 |
2 |
$role = Zend_Auth::getInstance()->getIdentity(); |
| 155 |
2 |
$resource = $this->_getResource($request->getModuleName(), |
| 156 |
2 |
$request->getControllerName(), $request->getActionName()); |
| 157 |
|
|
| 158 |
|
try { |
| 159 |
2 |
if ( !$this->_acl->isAllowed($role, $resource) ) { |
| 160 |
1 |
$msg = 'Insufficient permissions: '; |
| 161 |
1 |
$msg .= $role . ' -> ' . $resource->getResourceId(); |
| 162 |
1 |
require_once 'Zend/Acl/Exception.php'; |
| 163 |
1 |
throw new Zend_Acl_Exception($msg); |
| 164 |
0 |
} |
| 165 |
2 |
} catch ( Zend_Acl_Exception $thrown ) { |
| 166 |
1 |
$error = new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS); |
| 167 |
1 |
$error->exception = $thrown; |
| 168 |
1 |
$error->type = 'EXCEPTION_OTHER'; |
| 169 |
|
|
| 170 |
|
// Keep a copy of the original request |
| 171 |
1 |
$error->request = clone $request; |
| 172 |
|
|
| 173 |
|
// Forward to the error handler |
| 174 |
1 |
$request->setParam('error_handler', $error) |
| 175 |
1 |
->setModuleName($this->getAccessDeniedModule()) |
| 176 |
1 |
->setControllerName($this->getAccessDeniedController()) |
| 177 |
1 |
->setActionName($this->getAccessDeniedAction()) |
| 178 |
1 |
->setDispatched(false); |
| 179 |
|
} |
| 180 |
|
} |
| 181 |
|
|
| 182 |
|
/** |
| 183 |
|
* Setup the error handling options |
| 184 |
|
* |
| 185 |
|
* @param array $options |
| 186 |
|
* @return Xyster_Controller_Plugin_Acl |
| 187 |
|
*/ |
| 188 |
|
public function setAccessDenied(array $options = array()) |
| 189 |
|
{ |
| 190 |
6 |
if (isset($options['module'])) { |
| 191 |
1 |
$this->setAccessDeniedModule($options['module']); |
| 192 |
1 |
} |
| 193 |
6 |
if (isset($options['controller'])) { |
| 194 |
1 |
$this->setAccessDeniedController($options['controller']); |
| 195 |
1 |
} |
| 196 |
6 |
if (isset($options['action'])) { |
| 197 |
1 |
$this->setAccessDeniedAction($options['action']); |
| 198 |
1 |
} |
| 199 |
6 |
return $this; |
| 200 |
|
} |
| 201 |
|
|
| 202 |
|
/** |
| 203 |
|
* Set the action name for the acl plugin |
| 204 |
|
* |
| 205 |
|
* @param string $action |
| 206 |
|
* @return Xyster_Controller_Plugin_Acl |
| 207 |
|
*/ |
| 208 |
|
public function setAccessDeniedAction($action) |
| 209 |
|
{ |
| 210 |
2 |
$this->_errorAction = (string) $action; |
| 211 |
2 |
return $this; |
| 212 |
|
} |
| 213 |
|
|
| 214 |
|
/** |
| 215 |
|
* Set the controller name for the acl plugin |
| 216 |
|
* |
| 217 |
|
* @param string $controller |
| 218 |
|
* @return Xyster_Controller_Plugin_Acl |
| 219 |
|
*/ |
| 220 |
|
public function setAccessDeniedController($controller) |
| 221 |
|
{ |
| 222 |
2 |
$this->_errorController = (string) $controller; |
| 223 |
2 |
return $this; |
| 224 |
|
} |
| 225 |
|
|
| 226 |
|
/** |
| 227 |
|
* Set the module name for the acl plugin |
| 228 |
|
* |
| 229 |
|
* @param string $module |
| 230 |
|
* @return Xyster_Controller_Plugin_Acl |
| 231 |
|
*/ |
| 232 |
|
public function setAccessDeniedModule($module) |
| 233 |
|
{ |
| 234 |
2 |
$this->_errorModule = (string) $module; |
| 235 |
2 |
return $this; |
| 236 |
|
} |
| 237 |
|
|
| 238 |
|
/** |
| 239 |
|
* Gets the resource object |
| 240 |
|
* |
| 241 |
|
* @param string $module |
| 242 |
|
* @param string $controller |
| 243 |
|
* @param string $action |
| 244 |
|
* @return Xyster_Controller_Request_Resource |
| 245 |
|
*/ |
| 246 |
|
protected function _getResource( $module, $controller, $action ) |
| 247 |
|
{ |
| 248 |
2 |
$resource = null; |
| 249 |
|
|
| 250 |
2 |
if ( $module ) { |
| 251 |
2 |
$moduleResource = new Xyster_Controller_Request_Resource($module); |
| 252 |
2 |
if ( !$this->_acl->has($moduleResource) ) { |
| 253 |
2 |
$this->_acl->add($moduleResource); |
| 254 |
2 |
} |
| 255 |
2 |
$resource = $moduleResource; |
| 256 |
2 |
} |
| 257 |
2 |
if ( $module && $controller ) { |
| 258 |
2 |
$controllerResource = new Xyster_Controller_Request_Resource($module, $controller); |
| 259 |
2 |
if ( !$this->_acl->has($controllerResource) ) { |
| 260 |
2 |
$this->_acl->add($controllerResource, $moduleResource); |
| 261 |
2 |
} |
| 262 |
2 |
$resource = $controllerResource; |
| 263 |
2 |
} |
| 264 |
2 |
if ( $module && $controller && $action ) { |
| 265 |
2 |
$actionResource = new Xyster_Controller_Request_Resource($module, $controller, $action); |
| 266 |
2 |
if ( !$this->_acl->has($actionResource) ) { |
| 267 |
2 |
$this->_acl->add($actionResource, $controllerResource); |
| 268 |
2 |
} |
| 269 |
2 |
$resource = $actionResource; |
| 270 |
2 |
} |
| 271 |
|
|
| 272 |
2 |
return $resource; |
| 273 |
|
} |
| 274 |
|
} |