Skip to content

Commit

Permalink
Trigger Auth.afterIdentify even for stateless authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Dec 2, 2015
1 parent f1f7c3f commit 754f4a2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Controller/Component/AuthComponent.php
Expand Up @@ -697,6 +697,10 @@ protected function _getUser()
$result = $auth->getUser($this->request);
if (!empty($result) && is_array($result)) {
$this->_authenticationProvider = $auth;
$event = $this->dispatchEvent('Auth.afterIdentify', [$result]);
if ($event->result !== null) {
$result = $event->result;
}
$this->storage()->write($result);
return true;
}
Expand Down
31 changes: 31 additions & 0 deletions tests/TestCase/Controller/Component/AuthComponentTest.php
Expand Up @@ -20,6 +20,7 @@
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Event\Event;
use Cake\Event\EventManager;
use Cake\Network\Exception\ForbiddenException;
use Cake\Network\Exception\UnauthorizedException;
use Cake\Network\Request;
Expand Down Expand Up @@ -1163,6 +1164,36 @@ public function testEventTriggering()
$this->assertEquals($expected, $user);
}

/**
* testAfterIdentifyForStatelessAuthentication
*
* @return void
* @triggers Controller.startup $this->Controller
*/
public function testAfterIdentifyForStatelessAuthentication()
{
$event = new Event('Controller.startup', $this->Controller);
$url = '/auth_test/add';
$this->Auth->request->addParams(Router::parse($url));
$this->Auth->request->env('PHP_AUTH_USER', 'mariano');
$this->Auth->request->env('PHP_AUTH_PW', 'cake');

$this->Auth->config('authenticate', [
'Basic' => ['userModel' => 'AuthUsers']
]);
$this->Auth->config('storage', 'Memory');

EventManager::instance()->on('Auth.afterIdentify', function ($event) {
$user = $event->data[0];
$user['from_callback'] = true;
return $user;
});

$this->Auth->startup($event);
$this->assertEquals('mariano', $this->Auth->user('username'));
$this->assertTrue($this->Auth->user('from_callback'));
}

/**
* test setting user info to session.
*
Expand Down

0 comments on commit 754f4a2

Please sign in to comment.