Skip to content

Commit

Permalink
Merge pull request #222 from robertpustulka/add-authentication-getter
Browse files Browse the repository at this point in the history
Add AuthenticationService getter.
  • Loading branch information
burzum authored Sep 12, 2018
2 parents f295bbb + 9d44c8c commit 8760443
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 20 deletions.
41 changes: 27 additions & 14 deletions src/Controller/Component/AuthenticationComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ class AuthenticationComponent extends Component implements EventDispatcherInterf
public function initialize(array $config)
{
$controller = $this->getController();
$this->_authentication = $controller->request->getAttribute('authentication');
$this->setEventManager($controller->getEventManager());
}

Expand All @@ -85,15 +84,8 @@ public function initialize(array $config)
*/
public function beforeFilter()
{
if ($this->_authentication === null) {
throw new Exception('The request object does not contain the required `authentication` attribute');
}

if (!($this->_authentication instanceof AuthenticationServiceInterface)) {
throw new Exception('Authentication service does not implement ' . AuthenticationServiceInterface::class);
}

$provider = $this->_authentication->getAuthenticationProvider();
$authentication = $this->getAuthenticationService();
$provider = $authentication->getAuthenticationProvider();

if ($provider === null ||
$provider instanceof PersistenceInterface ||
Expand All @@ -105,10 +97,31 @@ public function beforeFilter()
$this->dispatchEvent('Authentication.afterIdentify', [
'provider' => $provider,
'identity' => $this->getIdentity(),
'service' => $this->_authentication
'service' => $authentication
], $this->getController());
}

/**
* Returns authentication service.
*
* @return \Authentication\AuthenticationServiceInterface
* @throws \Exception
*/
public function getAuthenticationService()
{
$controller = $this->getController();
$service = $controller->request->getAttribute('authentication');
if ($service === null) {
throw new Exception('The request object does not contain the required `authentication` attribute');
}

if (!($service instanceof AuthenticationServiceInterface)) {
throw new Exception('Authentication service does not implement ' . AuthenticationServiceInterface::class);
}

return $service;
}

/**
* Start up event handler
*
Expand Down Expand Up @@ -181,7 +194,7 @@ public function getUnauthenticatedActions()
*/
public function getResult()
{
return $this->_authentication->getResult();
return $this->getAuthenticationService()->getResult();
}

/**
Expand Down Expand Up @@ -225,7 +238,7 @@ public function setIdentity(ArrayAccess $identity)
{
$controller = $this->getController();

$result = $this->_authentication->persistIdentity(
$result = $this->getAuthenticationService()->persistIdentity(
$controller->request,
$controller->response,
$identity
Expand All @@ -247,7 +260,7 @@ public function setIdentity(ArrayAccess $identity)
public function logout()
{
$controller = $this->getController();
$result = $this->_authentication->clearIdentity(
$result = $this->getAuthenticationService()->clearIdentity(
$controller->request,
$controller->response
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,34 +71,50 @@ public function setUp()
}

/**
* testInitializeMissingServiceAttribute
* testGetAuthenticationService
*
* @return void
*/
public function testGetAuthenticationService()
{
$service = new AuthenticationService();
$request = $this->request->withAttribute('authentication', $service);
$controller = new Controller($request, $this->response);
$registry = new ComponentRegistry($controller);
$component = new AuthenticationComponent($registry);
$result = $component->getAuthenticationService();
$this->assertSame($service, $result);
}

/**
* testGetAuthenticationServiceMissingServiceAttribute
*
* @expectedException \Exception
* @expectedExceptionMessage The request object does not contain the required `authentication` attribute
* @return void
*/
public function testInitializeMissingServiceAttribute()
public function testGetAuthenticationServiceMissingServiceAttribute()
{
$controller = new Controller($this->request, $this->response);
$registry = new ComponentRegistry($controller);
$component = new AuthenticationComponent($registry);
$component->beforeFilter();
$component->getAuthenticationService();
}

/**
* testInitializeInvalidServiceObject
* testGetAuthenticationServiceInvalidServiceObject
*
* @expectedException \Exception
* @expectedExceptionMessage Authentication service does not implement Authentication\AuthenticationServiceInterface
* @return void
*/
public function testInitializeInvalidServiceObject()
public function testGetAuthenticationServiceInvalidServiceObject()
{
$request = $this->request->withAttribute('authentication', new InvalidAuthenticationService());
$controller = new Controller($request, $this->response);
$registry = new ComponentRegistry($controller);
$component = new AuthenticationComponent($registry);
$component->beforeFilter();
$component->getAuthenticationService();
}

/**
Expand Down

0 comments on commit 8760443

Please sign in to comment.