Skip to content

Commit

Permalink
Merge pull request #676 from cakephp/bugfix/is-impersonating
Browse files Browse the repository at this point in the history
Fix PHP error when checking for impersonation without authentication.
  • Loading branch information
markstory authored Oct 7, 2024
2 parents 5d4885b + 7a9920f commit 33b4fa3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/Controller/Component/AuthenticationComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,10 @@ public function stopImpersonating()
*/
public function isImpersonating(): bool
{
if (!$this->getIdentity()) {
return false;
}

$service = $this->getImpersonationAuthenticationService();
$controller = $this->getController();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -731,13 +731,17 @@ public function testIsImpersonating()
$this->request->getSession()->write('AuthImpersonate', $impersonator);
$this->service->authenticate($this->request);
$request = $this->request
->withAttribute('authentication', $this->service);
->withAttribute('authentication', $this->service)
->withAttribute('identity', new Identity($impersonated));
$controller = new Controller($request, $this->response);
$registry = new ComponentRegistry($controller);
$component = new AuthenticationComponent($registry);

$result = $component->isImpersonating();
$this->assertTrue($result);

$component->logout();
$this->assertFalse($component->isImpersonating());
}

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

$component = $this->createPartialMock(AuthenticationComponent::class, ['getAuthenticationService']);
$component->expects($this->once())
->method('getAuthenticationService')
->willReturn($service);
$user = new ArrayObject(['username' => 'mariano']);
$request = $this->request
->withAttribute('authentication', $service)
->withAttribute('identity', new Identity($user));
$controller = new Controller($request, $this->response);
$registry = new ComponentRegistry($controller);
$component = new AuthenticationComponent($registry);

$this->expectException(InvalidArgumentException::class);
$classname = get_class($service);
Expand Down

0 comments on commit 33b4fa3

Please sign in to comment.