Skip to content

Commit 7a9920f

Browse files
committed
Fix PHP error when checking for impersonation without authentication.
1 parent 5d4885b commit 7a9920f

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

Diff for: src/Controller/Component/AuthenticationComponent.php

+4
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,10 @@ public function stopImpersonating()
445445
*/
446446
public function isImpersonating(): bool
447447
{
448+
if (!$this->getIdentity()) {
449+
return false;
450+
}
451+
448452
$service = $this->getImpersonationAuthenticationService();
449453
$controller = $this->getController();
450454

Diff for: tests/TestCase/Controller/Component/AuthenticationComponentTest.php

+12-5
Original file line numberDiff line numberDiff line change
@@ -731,13 +731,17 @@ public function testIsImpersonating()
731731
$this->request->getSession()->write('AuthImpersonate', $impersonator);
732732
$this->service->authenticate($this->request);
733733
$request = $this->request
734-
->withAttribute('authentication', $this->service);
734+
->withAttribute('authentication', $this->service)
735+
->withAttribute('identity', new Identity($impersonated));
735736
$controller = new Controller($request, $this->response);
736737
$registry = new ComponentRegistry($controller);
737738
$component = new AuthenticationComponent($registry);
738739

739740
$result = $component->isImpersonating();
740741
$this->assertTrue($result);
742+
743+
$component->logout();
744+
$this->assertFalse($component->isImpersonating());
741745
}
742746

743747
/**
@@ -749,10 +753,13 @@ public function testGetImpersonationAuthenticationServiceFailure()
749753
{
750754
$service = $this->getMockBuilder(AuthenticationServiceInterface::class)->getMock();
751755

752-
$component = $this->createPartialMock(AuthenticationComponent::class, ['getAuthenticationService']);
753-
$component->expects($this->once())
754-
->method('getAuthenticationService')
755-
->willReturn($service);
756+
$user = new ArrayObject(['username' => 'mariano']);
757+
$request = $this->request
758+
->withAttribute('authentication', $service)
759+
->withAttribute('identity', new Identity($user));
760+
$controller = new Controller($request, $this->response);
761+
$registry = new ComponentRegistry($controller);
762+
$component = new AuthenticationComponent($registry);
756763

757764
$this->expectException(InvalidArgumentException::class);
758765
$classname = get_class($service);

0 commit comments

Comments
 (0)