Skip to content

Commit 8760443

Browse files
authored
Merge pull request #222 from robertpustulka/add-authentication-getter
Add AuthenticationService getter.
2 parents f295bbb + 9d44c8c commit 8760443

File tree

2 files changed

+49
-20
lines changed

2 files changed

+49
-20
lines changed

src/Controller/Component/AuthenticationComponent.php

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ class AuthenticationComponent extends Component implements EventDispatcherInterf
7474
public function initialize(array $config)
7575
{
7676
$controller = $this->getController();
77-
$this->_authentication = $controller->request->getAttribute('authentication');
7877
$this->setEventManager($controller->getEventManager());
7978
}
8079

@@ -85,15 +84,8 @@ public function initialize(array $config)
8584
*/
8685
public function beforeFilter()
8786
{
88-
if ($this->_authentication === null) {
89-
throw new Exception('The request object does not contain the required `authentication` attribute');
90-
}
91-
92-
if (!($this->_authentication instanceof AuthenticationServiceInterface)) {
93-
throw new Exception('Authentication service does not implement ' . AuthenticationServiceInterface::class);
94-
}
95-
96-
$provider = $this->_authentication->getAuthenticationProvider();
87+
$authentication = $this->getAuthenticationService();
88+
$provider = $authentication->getAuthenticationProvider();
9789

9890
if ($provider === null ||
9991
$provider instanceof PersistenceInterface ||
@@ -105,10 +97,31 @@ public function beforeFilter()
10597
$this->dispatchEvent('Authentication.afterIdentify', [
10698
'provider' => $provider,
10799
'identity' => $this->getIdentity(),
108-
'service' => $this->_authentication
100+
'service' => $authentication
109101
], $this->getController());
110102
}
111103

104+
/**
105+
* Returns authentication service.
106+
*
107+
* @return \Authentication\AuthenticationServiceInterface
108+
* @throws \Exception
109+
*/
110+
public function getAuthenticationService()
111+
{
112+
$controller = $this->getController();
113+
$service = $controller->request->getAttribute('authentication');
114+
if ($service === null) {
115+
throw new Exception('The request object does not contain the required `authentication` attribute');
116+
}
117+
118+
if (!($service instanceof AuthenticationServiceInterface)) {
119+
throw new Exception('Authentication service does not implement ' . AuthenticationServiceInterface::class);
120+
}
121+
122+
return $service;
123+
}
124+
112125
/**
113126
* Start up event handler
114127
*
@@ -181,7 +194,7 @@ public function getUnauthenticatedActions()
181194
*/
182195
public function getResult()
183196
{
184-
return $this->_authentication->getResult();
197+
return $this->getAuthenticationService()->getResult();
185198
}
186199

187200
/**
@@ -225,7 +238,7 @@ public function setIdentity(ArrayAccess $identity)
225238
{
226239
$controller = $this->getController();
227240

228-
$result = $this->_authentication->persistIdentity(
241+
$result = $this->getAuthenticationService()->persistIdentity(
229242
$controller->request,
230243
$controller->response,
231244
$identity
@@ -247,7 +260,7 @@ public function setIdentity(ArrayAccess $identity)
247260
public function logout()
248261
{
249262
$controller = $this->getController();
250-
$result = $this->_authentication->clearIdentity(
263+
$result = $this->getAuthenticationService()->clearIdentity(
251264
$controller->request,
252265
$controller->response
253266
);

tests/TestCase/Controller/Component/AuthenticationComponentTest.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,34 +71,50 @@ public function setUp()
7171
}
7272

7373
/**
74-
* testInitializeMissingServiceAttribute
74+
* testGetAuthenticationService
75+
*
76+
* @return void
77+
*/
78+
public function testGetAuthenticationService()
79+
{
80+
$service = new AuthenticationService();
81+
$request = $this->request->withAttribute('authentication', $service);
82+
$controller = new Controller($request, $this->response);
83+
$registry = new ComponentRegistry($controller);
84+
$component = new AuthenticationComponent($registry);
85+
$result = $component->getAuthenticationService();
86+
$this->assertSame($service, $result);
87+
}
88+
89+
/**
90+
* testGetAuthenticationServiceMissingServiceAttribute
7591
*
7692
* @expectedException \Exception
7793
* @expectedExceptionMessage The request object does not contain the required `authentication` attribute
7894
* @return void
7995
*/
80-
public function testInitializeMissingServiceAttribute()
96+
public function testGetAuthenticationServiceMissingServiceAttribute()
8197
{
8298
$controller = new Controller($this->request, $this->response);
8399
$registry = new ComponentRegistry($controller);
84100
$component = new AuthenticationComponent($registry);
85-
$component->beforeFilter();
101+
$component->getAuthenticationService();
86102
}
87103

88104
/**
89-
* testInitializeInvalidServiceObject
105+
* testGetAuthenticationServiceInvalidServiceObject
90106
*
91107
* @expectedException \Exception
92108
* @expectedExceptionMessage Authentication service does not implement Authentication\AuthenticationServiceInterface
93109
* @return void
94110
*/
95-
public function testInitializeInvalidServiceObject()
111+
public function testGetAuthenticationServiceInvalidServiceObject()
96112
{
97113
$request = $this->request->withAttribute('authentication', new InvalidAuthenticationService());
98114
$controller = new Controller($request, $this->response);
99115
$registry = new ComponentRegistry($controller);
100116
$component = new AuthenticationComponent($registry);
101-
$component->beforeFilter();
117+
$component->getAuthenticationService();
102118
}
103119

104120
/**

0 commit comments

Comments
 (0)