Skip to content

Commit

Permalink
Have BaseAuthenticate implement CakeEventListener instead
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienbarre committed Nov 5, 2014
1 parent f78e6c0 commit ee73c17
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 5 deletions.
12 changes: 11 additions & 1 deletion lib/Cake/Controller/Component/Auth/BaseAuthenticate.php
Expand Up @@ -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.
Expand Down Expand Up @@ -65,6 +66,15 @@ abstract class BaseAuthenticate {
*/
protected $_passwordHasher;

/**
* Implemented events
*
* @return array of events => callbacks.
*/
public function implementedEvents() {
return array();
}

/**
* Constructor
*
Expand Down
4 changes: 3 additions & 1 deletion lib/Cake/Controller/Component/AuthComponent.php
Expand Up @@ -799,7 +799,9 @@ public function constructAuthenticate() {
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;
}
Expand Down
83 changes: 80 additions & 3 deletions lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php
Expand Up @@ -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
*
Expand All @@ -46,6 +91,17 @@ 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
*
Expand Down Expand Up @@ -425,16 +481,37 @@ public function testLogin() {
$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());
}
Expand Down

0 comments on commit ee73c17

Please sign in to comment.