updating Auth component and tests

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5423 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2007-07-09 05:27:04 +00:00
parent 59ef6530b7
commit a6782f0b72
2 changed files with 63 additions and 35 deletions

View file

@ -267,7 +267,6 @@ class AuthComponent extends Object {
} }
if ($this->_normalizeURL($this->loginAction) == $this->_normalizeURL($url)) { if ($this->_normalizeURL($this->loginAction) == $this->_normalizeURL($url)) {
// We're already at the login action
if (empty($controller->data) || !isset($controller->data[$this->userModel])) { if (empty($controller->data) || !isset($controller->data[$this->userModel])) {
if (!$this->Session->check('Auth.redirect')) { if (!$this->Session->check('Auth.redirect')) {
$this->Session->write('Auth.redirect', $controller->referer()); $this->Session->write('Auth.redirect', $controller->referer());
@ -302,11 +301,17 @@ class AuthComponent extends Object {
} }
if($this->authorize) { if($this->authorize) {
extract($this->__authType($this->authorize)); extract($this->__authType());
if($type !== 'controller') { if(in_array($type, array('crud', 'actions'))) {
if(isset($controller->Acl)) { if(isset($controller->Acl)) {
$this->Acl =& $controller->Acl; $this->Acl =& $controller->Acl;
if($type == 'model') { if ($this->isAuthorized($type)) {
return true;
}
} else {
trigger_error(__('Could not find AclComponent. Please include Acl in Controller::$components.', true), E_USER_WARNING);
}
} else if($type == 'model') {
if(!isset($object)) { if(!isset($object)) {
if (isset($controller->{$controller->modelClass}) && is_object($controller->{$controller->modelClass})) { if (isset($controller->{$controller->modelClass}) && is_object($controller->{$controller->modelClass})) {
$object = $controller->modelClass; $object = $controller->modelClass;
@ -316,25 +321,20 @@ class AuthComponent extends Object {
$object = $this->objectModel; $object = $this->objectModel;
} }
} }
}
if ($this->isAuthorized($type, null, $object)) { if ($this->isAuthorized($type, null, $object)) {
return true; return true;
} }
} else { } else if($type == 'controller'){
trigger_error(__('Could not find AclComponent. Please include Acl in Controller::$components.', true), E_USER_WARNING);
}
} else {
if (method_exists($controller, 'isAuthorized')) {
if($controller->isAuthorized()) { if($controller->isAuthorized()) {
return true; return true;
} }
} }
}
}
$this->Session->setFlash($this->authError); $this->Session->setFlash($this->authError);
$controller->redirect($controller->referer(), null, true); $controller->redirect($controller->referer(), null, true);
return false; return false;
} else {
return true;
}
} }
/** /**
* Attempts to introspect the correct values for object properties including * Attempts to introspect the correct values for object properties including
@ -377,7 +377,7 @@ class AuthComponent extends Object {
$user = $this->user(); $user = $this->user();
} }
extract($this->__authType($type)); extract($this->__authType(array($type => $object)));
if(!$object) { if(!$object) {
$object = $this->objectModel; $object = $this->objectModel;
@ -391,23 +391,23 @@ class AuthComponent extends Object {
case 'crud': case 'crud':
$this->mapActions(); $this->mapActions();
if (!isset($this->actionMap[$this->params['action']])) { if (!isset($this->actionMap[$this->params['action']])) {
trigger_error('Auth::startup() - Attempted access of un-mapped action "' . $this->params['action'] . '" in controller "' . $this->params['controller'] . '"', E_USER_WARNING); trigger_error(__(sprintf('Auth::startup() - Attempted access of un-mapped action "%s" in controller "%s"', $this->params['action'], $this->params['controller']), true), E_USER_WARNING);
} else { } else {
$valid = $this->Acl->check($user, $this->action(':controller'), $this->actionMap[$this->params['action']]); $valid = $this->Acl->check($user, $this->action(':controller'), $this->actionMap[$this->params['action']]);
} }
break; break;
case 'model': case 'model':
if(empty($object)) { if(empty($object)) {
trigger_error(__('Could not find $this->objectModel. Please set AuthComponent::$objectModel in beforeFilter().', true), E_USER_WARNING); trigger_error(__(sprintf('Could not find %s. Set AuthComponent::$objectModel in beforeFilter() or pass object name.', $this->objectModel), true), E_USER_WARNING);
return; return;
} }
$model = $this->getModel($object); $model = $this->getModel($object);
if (method_exists($model, 'isAuthorized')) { if (method_exists($model, 'isAuthorized')) {
if($model->isAuthorized()) { if($model->isAuthorized($user)) {
return true; return true;
} }
} else { } else if($model){
trigger_error(__($object.'::isAuthorized() is not defined.', true), E_USER_WARNING); trigger_error(__(sprintf('%s::isAuthorized() is not defined.', $model), true), E_USER_WARNING);
} }
break; break;
case null: case null:
@ -638,8 +638,8 @@ class AuthComponent extends Object {
} }
if (!ClassRegistry::isKeySet($name)) { if (!ClassRegistry::isKeySet($name)) {
if (!loadModel(Inflector::underscore($name))) { if (!loadModel(Inflector::underscore($name))) {
trigger_error(__('Auth::getModel() - $userModel is not set or could not be found', true) . $name, E_USER_WARNING); trigger_error(__(sprintf('Auth::getModel() - %s is not set or could not be found', $name), true), E_USER_WARNING);
return null; return $model;
} else { } else {
$model = new $name(); $model = new $name();
} }
@ -654,7 +654,7 @@ class AuthComponent extends Object {
} }
if (empty($model)) { if (empty($model)) {
trigger_error(__('Auth::getModel() - $name is not set or could not be found', true) . $name, E_USER_WARNING); trigger_error(__(sprintf('Auth::getModel() - %s is not set or could not be found', $name), true) . $name, E_USER_WARNING);
return null; return null;
} }

View file

@ -38,6 +38,13 @@ class AuthUser extends CakeTestModel {
function bindNode($object) { function bindNode($object) {
return 'Roles/Admin'; return 'Roles/Admin';
} }
function isAuthorized($user) {
if(!empty($user)) {
return true;
}
return false;
}
} }
class AuthTestController extends Controller { class AuthTestController extends Controller {
@ -119,6 +126,16 @@ class AuthTest extends CakeTestCase {
$this->Controller->Session->del('Auth'); $this->Controller->Session->del('Auth');
} }
function testAuthFalse() {
$this->AuthUser =& new AuthUser();
$user = $this->AuthUser->find();
$this->Controller->Session->write('Auth', $user);
$this->Controller->Auth->userModel = 'AuthUser';
$this->Controller->Auth->authorize = false;
$result = $this->Controller->Auth->startup($this->Controller);
$this->assertTrue($result);
}
function testAuthController(){ function testAuthController(){
$this->AuthUser =& new AuthUser(); $this->AuthUser =& new AuthUser();
$user = $this->AuthUser->find(); $user = $this->AuthUser->find();
@ -130,6 +147,17 @@ class AuthTest extends CakeTestCase {
$this->Controller->Session->del('Auth'); $this->Controller->Session->del('Auth');
} }
function testAuthorizeModel() {
$this->AuthUser =& new AuthUser();
$user = $this->AuthUser->find();
$this->Controller->Session->write('Auth', $user);
$this->Controller->Auth->userModel = 'AuthUser';
$this->Controller->Auth->initialize($this->Controller);
$this->Controller->Auth->authorize = array('model'=>'AuthUser');
$result = $this->Controller->Auth->startup($this->Controller);
$this->assertTrue($result);
}
function testAuthWithDB_ACL() { function testAuthWithDB_ACL() {
$this->AuthUser =& new AuthUser(); $this->AuthUser =& new AuthUser();
$user = $this->AuthUser->find(); $user = $this->AuthUser->find();
@ -172,12 +200,12 @@ class AuthTest extends CakeTestCase {
$this->Controller->Session->del('Auth'); $this->Controller->Session->del('Auth');
$this->Controller->Acl->Aro->execute('truncate users;');
$this->Controller->Acl->Aro->execute('truncate aros;'); $this->Controller->Acl->Aro->execute('truncate aros;');
$this->Controller->Acl->Aro->execute('truncate acos;'); $this->Controller->Acl->Aro->execute('truncate acos;');
$this->Controller->Acl->Aro->execute('truncate aros_acos;'); $this->Controller->Acl->Aro->execute('truncate aros_acos;');
} }
function tearDown() { function tearDown() {
unset($this->Controller, $this->AuthUser); unset($this->Controller, $this->AuthUser);
} }