Merge pull request #5088 from sebastienbarre/ticket-5041

Ticket #5041: have Auth::login() send Auth.afterIdentify event
This commit is contained in:
Mark Story 2014-12-18 00:12:22 -05:00
commit 1651e7742c
3 changed files with 124 additions and 3 deletions

View file

@ -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
*

View file

@ -26,6 +26,7 @@ App::uses('Hash', 'Utility');
App::uses('CakeSession', 'Model/Datasource');
App::uses('BaseAuthorize', 'Controller/Component/Auth');
App::uses('BaseAuthenticate', 'Controller/Component/Auth');
App::uses('CakeEvent', 'Event');
/**
* Authentication control component class
@ -608,6 +609,8 @@ class AuthComponent extends Component {
if ($user) {
$this->Session->renew();
$this->Session->write(self::$sessionKey, $user);
$event = new CakeEvent('Auth.afterIdentify', $this, array('user' => $user));
$this->_Collection->getController()->getEventManager()->dispatch($event);
}
return (bool)$this->user();
}
@ -796,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;
}

View file

@ -19,7 +19,54 @@
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');
App::uses('CakeEvent', 'Event');
/**
* 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
@ -39,13 +86,24 @@ class TestAuthComponent extends AuthComponent {
* Helper method to add/set an authenticate object instance
*
* @param int $index The index at which to add/set the object
* @param Object $object The object to add/set
* @param object $object The object to add/set
* @return void
*/
public function setAuthenticateObject($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
*
@ -266,6 +324,27 @@ class AjaxAuthController extends Controller {
}
/**
* Mock class used to test event dispatching
*
* @package Cake.Test.Case.Event
*/
class AuthEventTestListener {
public $callStack = array();
/**
* Test function to be used in event dispatching
*
* @return void
*/
public function listenerFunction() {
$this->callStack[] = __FUNCTION__;
}
}
/**
* AuthComponentTest class
*
@ -411,6 +490,33 @@ class AuthComponentTest extends CakeTestCase {
$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');
$auth->afterIdentifyCallable = array($listener, 'listenerFunction');
$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());
}
/**
* testRedirectVarClearing method
*