diff --git a/lib/Cake/Controller/Component/Auth/BaseAuthenticate.php b/lib/Cake/Controller/Component/Auth/BaseAuthenticate.php index 77629ee39..df9d3c06e 100644 --- a/lib/Cake/Controller/Component/Auth/BaseAuthenticate.php +++ b/lib/Cake/Controller/Component/Auth/BaseAuthenticate.php @@ -14,13 +14,14 @@ App::uses('Security', 'Utility'); App::uses('Hash', 'Utility'); +App::uses('CakeEventListener', 'Event'); /** * Base Authentication class with common methods and properties. * * @package Cake.Controller.Component.Auth */ -abstract class BaseAuthenticate { +abstract class BaseAuthenticate implements CakeEventListener { /** * Settings for this object. @@ -65,6 +66,15 @@ abstract class BaseAuthenticate { */ protected $_passwordHasher; +/** + * Implemented events + * + * @return array of events => callbacks. + */ + public function implementedEvents() { + return array(); + } + /** * Constructor * diff --git a/lib/Cake/Controller/Component/AuthComponent.php b/lib/Cake/Controller/Component/AuthComponent.php index 1d9915662..3d117f0ac 100644 --- a/lib/Cake/Controller/Component/AuthComponent.php +++ b/lib/Cake/Controller/Component/AuthComponent.php @@ -799,7 +799,9 @@ class AuthComponent extends Component { throw new CakeException(__d('cake_dev', 'Authentication objects must implement an %s method.', 'authenticate()')); } $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; } diff --git a/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php b/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php index e607cdd8f..adcf27775 100644 --- a/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php +++ b/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php @@ -19,8 +19,53 @@ App::uses('Controller', 'Controller'); App::uses('AuthComponent', 'Controller/Component'); App::uses('AclComponent', 'Controller/Component'); +App::uses('BaseAuthenticate', '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 * @@ -46,6 +91,17 @@ class TestAuthComponent extends AuthComponent { $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 * @@ -425,16 +481,37 @@ class AuthComponentTest extends CakeTestCase { $this->Auth->Session->expects($this->once()) ->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'); - $manager->attach(array($listener, 'listenerFunction'), 'Auth.afterIdentify'); + $auth->afterIdentifyCallable = array($listener, 'listenerFunction'); App::uses('CakeEvent', 'Event'); $event = new CakeEvent('Auth.afterIdentify', $this->Auth, array('user' => $user)); $listener->expects($this->once())->method('listenerFunction')->with($event); $result = $this->Auth->login(); $this->assertTrue($result); - $this->assertTrue($this->Auth->loggedIn()); $this->assertEquals($user, $this->Auth->user()); }