Have BaseAuthenticate implement CakeEventListener instead

This commit is contained in:
Sebastien Barre 2014-11-05 18:03:26 -05:00
parent 50e5b5e8fe
commit 7da48669c8
3 changed files with 94 additions and 5 deletions

View file

@ -14,13 +14,14 @@
App::uses('Security', 'Utility'); App::uses('Security', 'Utility');
App::uses('Hash', 'Utility'); App::uses('Hash', 'Utility');
App::uses('CakeEventListener', 'Event');
/** /**
* Base Authentication class with common methods and properties. * Base Authentication class with common methods and properties.
* *
* @package Cake.Controller.Component.Auth * @package Cake.Controller.Component.Auth
*/ */
abstract class BaseAuthenticate { abstract class BaseAuthenticate implements CakeEventListener {
/** /**
* Settings for this object. * Settings for this object.
@ -65,6 +66,15 @@ abstract class BaseAuthenticate {
*/ */
protected $_passwordHasher; protected $_passwordHasher;
/**
* Implemented events
*
* @return array of events => callbacks.
*/
public function implementedEvents() {
return array();
}
/** /**
* Constructor * Constructor
* *

View file

@ -799,7 +799,9 @@ class AuthComponent extends Component {
throw new CakeException(__d('cake_dev', 'Authentication objects must implement an %s method.', 'authenticate()')); throw new CakeException(__d('cake_dev', 'Authentication objects must implement an %s method.', 'authenticate()'));
} }
$settings = array_merge($global, (array)$settings); $settings = array_merge($global, (array)$settings);
$this->_authenticateObjects[] = new $className($this->_Collection, $settings); $auth = new $className($this->_Collection, $settings);
$this->_Collection->getController()->getEventManager()->attach($auth);
$this->_authenticateObjects[] = $auth;
} }
return $this->_authenticateObjects; return $this->_authenticateObjects;
} }

View file

@ -19,8 +19,53 @@
App::uses('Controller', 'Controller'); App::uses('Controller', 'Controller');
App::uses('AuthComponent', 'Controller/Component'); App::uses('AuthComponent', 'Controller/Component');
App::uses('AclComponent', 'Controller/Component'); App::uses('AclComponent', 'Controller/Component');
App::uses('BaseAuthenticate', 'Controller/Component/Auth');
App::uses('FormAuthenticate', 'Controller/Component/Auth'); App::uses('FormAuthenticate', 'Controller/Component/Auth');
/**
* TestFormAuthenticate class
*
* @package Cake.Test.Case.Controller.Component
*/
class TestBaseAuthenticate extends BaseAuthenticate {
/**
* Implemented events
*
* @return array of events => callbacks.
*/
public function implementedEvents() {
return array(
'Auth.afterIdentify' => 'afterIdentify'
);
}
public $afterIdentifyCallable = null;
/**
* Test function to be used in event dispatching
*
* @return void
*/
public function afterIdentify($event) {
call_user_func($this->afterIdentifyCallable, $event);
}
/**
* Authenticate a user based on the request information.
*
* @param CakeRequest $request Request to get authentication information from.
* @param CakeResponse $response A response object that can have headers added.
* @return mixed Either false on failure, or an array of user data on success.
*/
public function authenticate(CakeRequest $request, CakeResponse $response) {
return array(
'id' => 1,
'username' => 'mark'
);
}
}
/** /**
* TestAuthComponent class * TestAuthComponent class
* *
@ -46,6 +91,17 @@ class TestAuthComponent extends AuthComponent {
$this->_authenticateObjects[$index] = $object; $this->_authenticateObjects[$index] = $object;
} }
/**
* Helper method to get an authenticate object instance
*
* @param int $index The index at which to get the object
* @return Object $object
*/
public function getAuthenticateObject($index) {
$this->constructAuthenticate();
return isset($this->_authenticateObjects[$index]) ? $this->_authenticateObjects[$index] : null;
}
/** /**
* Helper method to add/set an authorize object instance * Helper method to add/set an authorize object instance
* *
@ -425,16 +481,37 @@ class AuthComponentTest extends CakeTestCase {
$this->Auth->Session->expects($this->once()) $this->Auth->Session->expects($this->once())
->method('renew'); ->method('renew');
$manager = $this->Controller->getEventManager(); $result = $this->Auth->login();
$this->assertTrue($result);
$this->assertTrue($this->Auth->loggedIn());
$this->assertEquals($user, $this->Auth->user());
}
/**
* testLogin afterIdentify event method
*
* @return void
*/
public function testLoginAfterIdentify() {
$this->Auth->authenticate = array(
'TestBase',
);
$user = array(
'id' => 1,
'username' => 'mark'
);
$auth = $this->Auth->getAuthenticateObject(0);
$listener = $this->getMock('AuthEventTestListener'); $listener = $this->getMock('AuthEventTestListener');
$manager->attach(array($listener, 'listenerFunction'), 'Auth.afterIdentify'); $auth->afterIdentifyCallable = array($listener, 'listenerFunction');
App::uses('CakeEvent', 'Event'); App::uses('CakeEvent', 'Event');
$event = new CakeEvent('Auth.afterIdentify', $this->Auth, array('user' => $user)); $event = new CakeEvent('Auth.afterIdentify', $this->Auth, array('user' => $user));
$listener->expects($this->once())->method('listenerFunction')->with($event); $listener->expects($this->once())->method('listenerFunction')->with($event);
$result = $this->Auth->login(); $result = $this->Auth->login();
$this->assertTrue($result); $this->assertTrue($result);
$this->assertTrue($this->Auth->loggedIn()); $this->assertTrue($this->Auth->loggedIn());
$this->assertEquals($user, $this->Auth->user()); $this->assertEquals($user, $this->Auth->user());
} }