| 1 |
|
<?php |
| 2 |
|
/** |
| 3 |
|
* Xyster Framework |
| 4 |
|
* |
| 5 |
|
* This source file is subject to the new BSD license that is bundled |
| 6 |
|
* with this package in the file LICENSE.txt. |
| 7 |
|
* It is also available through the world-wide-web at this URL: |
| 8 |
|
* http://www.opensource.org/licenses/bsd-license.php |
| 9 |
|
* |
| 10 |
|
* @category Xyster |
| 11 |
|
* @package Xyster_Controller |
| 12 |
|
* @subpackage Plugins |
| 13 |
|
* @copyright Copyright (c) 2007-2008 Irrational Logic (http://irrationallogic.net) |
| 14 |
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
| 15 |
|
* @version $Id: Auth.php 202 2008-01-20 16:20:09Z doublecompile $ |
| 16 |
|
*/ |
| 17 |
|
/** |
| 18 |
|
* Zend_Auth |
| 19 |
|
*/ |
| 20 |
1 |
require_once 'Zend/Auth.php'; |
| 21 |
|
/** |
| 22 |
|
* Zend_Controller_Plugin_Abstract |
| 23 |
|
*/ |
| 24 |
1 |
require_once 'Zend/Controller/Plugin/Abstract.php'; |
| 25 |
|
/** |
| 26 |
|
* Authentication plugin |
| 27 |
|
* |
| 28 |
|
* @category Xyster |
| 29 |
|
* @package Xyster_Controller |
| 30 |
|
* @subpackage Plugins |
| 31 |
|
* @copyright Copyright (c) 2007-2008 Irrational Logic (http://irrationallogic.net) |
| 32 |
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
| 33 |
|
*/ |
| 34 |
|
class Xyster_Controller_Plugin_Auth extends Zend_Controller_Plugin_Abstract |
| 35 |
|
{ |
| 36 |
|
/** |
| 37 |
|
* The acl |
| 38 |
|
* |
| 39 |
|
* @var Zend_Acl |
| 40 |
|
*/ |
| 41 |
|
protected $_acl; |
| 42 |
|
|
| 43 |
|
/** |
| 44 |
|
* The Auth adapter |
| 45 |
|
* |
| 46 |
|
* @var Zend_Auth_Adapter_Interface |
| 47 |
|
*/ |
| 48 |
|
protected $_adapter; |
| 49 |
|
|
| 50 |
|
/** |
| 51 |
|
* The dispatch action for authentication failure |
| 52 |
|
* |
| 53 |
|
* @var string |
| 54 |
|
*/ |
| 55 |
|
protected $_failAction = 'index'; |
| 56 |
|
|
| 57 |
|
/** |
| 58 |
|
* The dispatch controller for authentication failure |
| 59 |
|
* |
| 60 |
|
* @var string |
| 61 |
|
*/ |
| 62 |
|
protected $_failController = 'login'; |
| 63 |
|
|
| 64 |
|
/** |
| 65 |
|
* The dispatch module for authentication failure |
| 66 |
|
* |
| 67 |
|
* @var string |
| 68 |
|
*/ |
| 69 |
|
protected $_failModule; |
| 70 |
|
|
| 71 |
|
/** |
| 72 |
|
* The role provider |
| 73 |
|
* |
| 74 |
|
* @var Xyster_Acl_Role_Provider_Interface |
| 75 |
|
*/ |
| 76 |
|
protected $_provider; |
| 77 |
|
|
| 78 |
|
/** |
| 79 |
|
* The current authenticated role |
| 80 |
|
* |
| 81 |
|
* @var Zend_Acl_Role_Interface |
| 82 |
|
*/ |
| 83 |
|
protected $_role; |
| 84 |
|
|
| 85 |
|
/** |
| 86 |
|
* Whether the 'routeStartup' method has already been called |
| 87 |
|
* |
| 88 |
|
* @var boolean |
| 89 |
|
*/ |
| 90 |
|
protected $_started = false; |
| 91 |
|
|
| 92 |
|
/** |
| 93 |
|
* The dispatch action for authentication success |
| 94 |
|
* |
| 95 |
|
* @var string |
| 96 |
|
*/ |
| 97 |
|
protected $_successAction = 'success'; |
| 98 |
|
|
| 99 |
|
/** |
| 100 |
|
* The dispatch controller for authentication success |
| 101 |
|
* |
| 102 |
|
* @var string |
| 103 |
|
*/ |
| 104 |
|
protected $_successController = 'login'; |
| 105 |
|
|
| 106 |
|
/** |
| 107 |
|
* The dispatch module for authentication success |
| 108 |
|
* |
| 109 |
|
* @var string |
| 110 |
|
*/ |
| 111 |
|
protected $_successModule; |
| 112 |
|
|
| 113 |
|
/** |
| 114 |
|
* Called before Zend_Controller_Front determines the dispatch route |
| 115 |
|
* |
| 116 |
|
* @param Zend_Controller_Request_Abstract $request |
| 117 |
|
*/ |
| 118 |
|
public function routeStartup(Zend_Controller_Request_Abstract $request) |
| 119 |
|
{ |
| 120 |
4 |
$this->_started = true; |
| 121 |
|
|
| 122 |
4 |
$this->_authenticate(); |
| 123 |
|
} |
| 124 |
|
|
| 125 |
|
/** |
| 126 |
|
* Gets the ACL assigned to the plugin |
| 127 |
|
* |
| 128 |
|
* @return Zend_Acl |
| 129 |
|
*/ |
| 130 |
|
public function getAcl() |
| 131 |
|
{ |
| 132 |
1 |
return $this->_acl; |
| 133 |
|
} |
| 134 |
|
|
| 135 |
|
/** |
| 136 |
|
* Gets the dispatch action for authentication failure |
| 137 |
|
* |
| 138 |
|
* @return string |
| 139 |
|
*/ |
| 140 |
|
public function getFailAction() |
| 141 |
|
{ |
| 142 |
2 |
return $this->_failAction; |
| 143 |
|
} |
| 144 |
|
|
| 145 |
|
/** |
| 146 |
|
* Gets the dispatch controller for authentication failure |
| 147 |
|
* |
| 148 |
|
* @return string |
| 149 |
|
*/ |
| 150 |
|
public function getFailController() |
| 151 |
|
{ |
| 152 |
2 |
return $this->_failController; |
| 153 |
|
} |
| 154 |
|
|
| 155 |
|
/** |
| 156 |
|
* Gets the dispatch module for authentication failure |
| 157 |
|
* |
| 158 |
|
* @return string |
| 159 |
|
*/ |
| 160 |
|
public function getFailModule() |
| 161 |
|
{ |
| 162 |
2 |
if ($this->_failModule === null) { |
| 163 |
1 |
require_once 'Zend/Controller/Front.php'; |
| 164 |
1 |
$this->_failModule = Zend_Controller_Front::getInstance()->getDispatcher()->getDefaultModule(); |
| 165 |
1 |
} |
| 166 |
2 |
return $this->_failModule; |
| 167 |
|
} |
| 168 |
|
|
| 169 |
|
/** |
| 170 |
|
* Gets the authenticated role |
| 171 |
|
* |
| 172 |
|
* @return Zend_Acl_Role_Interface |
| 173 |
|
*/ |
| 174 |
|
public function getRole() |
| 175 |
|
{ |
| 176 |
3 |
$auth = Zend_Auth::getInstance(); |
| 177 |
3 |
if ( !$this->_role && $auth->hasIdentity() ) { |
| 178 |
3 |
$identity = $auth->getIdentity(); |
| 179 |
3 |
$this->_role = $this->getRoleProvider()->getRole($identity); |
| 180 |
3 |
} |
| 181 |
|
|
| 182 |
3 |
return $this->_role; |
| 183 |
|
} |
| 184 |
|
|
| 185 |
|
/** |
| 186 |
|
* Gets the role provider used to translate the identity into a role |
| 187 |
|
* |
| 188 |
|
* @return Xyster_Acl_Role_Provider_Interface |
| 189 |
|
*/ |
| 190 |
|
public function getRoleProvider() |
| 191 |
|
{ |
| 192 |
5 |
if ( !$this->_provider ) { |
| 193 |
4 |
require_once 'Xyster/Acl/Role/Provider.php'; |
| 194 |
4 |
$this->_provider = new Xyster_Acl_Role_Provider(); |
| 195 |
4 |
} |
| 196 |
5 |
return $this->_provider; |
| 197 |
|
} |
| 198 |
|
|
| 199 |
|
/** |
| 200 |
|
* Gets the dispatch action for authentication success |
| 201 |
|
* |
| 202 |
|
* @return string |
| 203 |
|
*/ |
| 204 |
|
public function getSuccessAction() |
| 205 |
|
{ |
| 206 |
3 |
return $this->_successAction; |
| 207 |
|
} |
| 208 |
|
|
| 209 |
|
/** |
| 210 |
|
* Gets the dispatch controller for authentication success |
| 211 |
|
* |
| 212 |
|
* @return string |
| 213 |
|
*/ |
| 214 |
|
public function getSuccessController() |
| 215 |
|
{ |
| 216 |
3 |
return $this->_successController; |
| 217 |
|
} |
| 218 |
|
|
| 219 |
|
/** |
| 220 |
|
* Gets the dispatch module for authentication success |
| 221 |
|
* |
| 222 |
|
* @return string |
| 223 |
|
*/ |
| 224 |
|
public function getSuccessModule() |
| 225 |
|
{ |
| 226 |
3 |
if ($this->_successModule === null) { |
| 227 |
2 |
require_once 'Zend/Controller/Front.php'; |
| 228 |
2 |
$this->_successModule = Zend_Controller_Front::getInstance()->getDispatcher()->getDefaultModule(); |
| 229 |
2 |
} |
| 230 |
3 |
return $this->_successModule; |
| 231 |
|
} |
| 232 |
|
|
| 233 |
|
/** |
| 234 |
|
* Sets the ACL to which the authenticated role will be added |
| 235 |
|
* |
| 236 |
|
* @param Zend_Acl $acl |
| 237 |
|
* @return Xyster_Controller_Plugin_Auth provides a fluent interface |
| 238 |
|
*/ |
| 239 |
|
public function setAcl( Zend_Acl $acl ) |
| 240 |
|
{ |
| 241 |
10 |
$this->_acl = $acl; |
| 242 |
10 |
return $this; |
| 243 |
|
} |
| 244 |
|
|
| 245 |
|
/** |
| 246 |
|
* Sets the authentication adapter |
| 247 |
|
* |
| 248 |
|
* @param Zend_Auth_Adapter_Interface $adapter |
| 249 |
|
* @return Xyster_Controller_Plugin_Auth provides a fluent interface |
| 250 |
|
*/ |
| 251 |
|
public function setAuthAdapter( Zend_Auth_Adapter_Interface $adapter ) |
| 252 |
|
{ |
| 253 |
3 |
$this->_adapter = $adapter; |
| 254 |
3 |
if ( $this->_started ) { |
| 255 |
|
// if the plugin already tried to authenticate, use this new adapter |
| 256 |
1 |
$this->_authenticate(); |
| 257 |
1 |
} |
| 258 |
3 |
return $this; |
| 259 |
|
} |
| 260 |
|
|
| 261 |
|
/** |
| 262 |
|
* Sets the dispatch location for a failed authentication |
| 263 |
|
* |
| 264 |
|
* @param string $module The dispatch module |
| 265 |
|
* @param string $controller The dispatch controller |
| 266 |
|
* @param string $action The dispatch action |
| 267 |
|
* @return Xyster_Controller_Plugin_Auth provides a fluent interface |
| 268 |
|
*/ |
| 269 |
|
public function setFailure( $module, $controller, $action ) |
| 270 |
|
{ |
| 271 |
1 |
$this->_failModule = $module; |
| 272 |
1 |
$this->_failController = $controller; |
| 273 |
1 |
$this->_failAction = $action; |
| 274 |
|
|
| 275 |
1 |
return $this; |
| 276 |
|
} |
| 277 |
|
|
| 278 |
|
/** |
| 279 |
|
* Sets the role provider used to translate the identity into a role |
| 280 |
|
* |
| 281 |
|
* @param Xyster_Acl_Role_Provider_Interface $provider |
| 282 |
|
* @return Xyster_Controller_Plugin_Auth provides a fluent interface |
| 283 |
|
*/ |
| 284 |
|
public function setRoleProvider( Xyster_Acl_Role_Provider_Interface $provider ) |
| 285 |
|
{ |
| 286 |
1 |
$this->_provider = $provider; |
| 287 |
1 |
return $this; |
| 288 |
|
} |
| 289 |
|
|
| 290 |
|
/** |
| 291 |
|
* Sets the dispatch location for a successful authentication |
| 292 |
|
* |
| 293 |
|
* @param string $module The dispatch module |
| 294 |
|
* @param string $controller The dispatch controller |
| 295 |
|
* @param string $action The dispatch action |
| 296 |
|
* @return Xyster_Controller_Plugin_Auth provides a fluent interface |
| 297 |
|
*/ |
| 298 |
|
public function setSuccess( $module, $controller, $action ) |
| 299 |
|
{ |
| 300 |
1 |
$this->_successModule = $module; |
| 301 |
1 |
$this->_successController = $controller; |
| 302 |
1 |
$this->_successAction = $action; |
| 303 |
|
|
| 304 |
1 |
return $this; |
| 305 |
|
} |
| 306 |
|
|
| 307 |
|
/** |
| 308 |
|
* Does the actual auth work |
| 309 |
|
* |
| 310 |
|
*/ |
| 311 |
|
protected function _authenticate() |
| 312 |
|
{ |
| 313 |
4 |
$auth = Zend_Auth::getInstance(); |
| 314 |
4 |
if ( !$auth->hasIdentity() ) { |
| 315 |
|
// no need to call the adapter if the user is authenticated |
| 316 |
4 |
if ( !$this->_adapter ) { |
| 317 |
|
// if we don't have an adapter, there's nothing to do |
| 318 |
2 |
return; |
| 319 |
0 |
} else { |
| 320 |
3 |
$result = $auth->authenticate($this->_adapter); |
| 321 |
3 |
$request = $this->getRequest(); |
| 322 |
3 |
if ( $result->isValid() ) { |
| 323 |
|
// if the authentication is valid send to the success action |
| 324 |
2 |
$request->setModuleName($this->getSuccessModule()) |
| 325 |
2 |
->setControllerName($this->getSuccessController()) |
| 326 |
2 |
->setActionName($this->getSuccessAction()) |
| 327 |
2 |
->setDispatched(false); |
| 328 |
2 |
} else { |
| 329 |
|
// if the authentication fails send to the failure action |
| 330 |
1 |
$request->setModuleName($this->getFailModule()) |
| 331 |
1 |
->setControllerName($this->getFailController()) |
| 332 |
1 |
->setActionName($this->getFailAction()) |
| 333 |
1 |
->setParam('result', $result) |
| 334 |
1 |
->setDispatched(false); |
| 335 |
1 |
return; |
| 336 |
|
} |
| 337 |
|
} |
| 338 |
2 |
} |
| 339 |
|
|
| 340 |
2 |
$role = $this->getRole(); |
| 341 |
|
|
| 342 |
2 |
if ( $role instanceof Zend_Acl_Role_Interface && $this->_acl && |
| 343 |
2 |
!$this->_acl->hasRole($role) ) { |
| 344 |
2 |
$this->_acl->addRole($role, $this->getRoleProvider()->getRoleParents($role)); |
| 345 |
2 |
} |
| 346 |
|
} |
| 347 |
|
} |