Skip to content

Commit 643e17b

Browse files
authored
Merge pull request #608 from cakephp/fix-606
Store only the original data in the impersonation session
2 parents 422a55b + 4b5cc23 commit 643e17b

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/Controller/Component/AuthenticationComponent.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
namespace Authentication\Controller\Component;
1818

1919
use ArrayAccess;
20+
use ArrayObject;
2021
use Authentication\AuthenticationServiceInterface;
2122
use Authentication\Authenticator\ImpersonationInterface;
2223
use Authentication\Authenticator\PersistenceInterface;
@@ -368,12 +369,16 @@ public function impersonate(ArrayAccess $impersonated)
368369
if (!$identity) {
369370
throw new UnauthenticatedException('You must be logged in before impersonating a user.');
370371
}
372+
$impersonator = $identity->getOriginalData();
373+
if (!($impersonator instanceof ArrayAccess)) {
374+
$impersonator = new ArrayObject($impersonator);
375+
}
371376
$controller = $this->getController();
372377
/** @psalm-var array{request: \Cake\Http\ServerRequest, response: \Cake\Http\Response} $result */
373378
$result = $service->impersonate(
374379
$controller->getRequest(),
375380
$controller->getResponse(),
376-
$identity,
381+
$impersonator,
377382
$impersonated
378383
);
379384

tests/TestCase/Controller/Component/AuthenticationComponentTest.php

+37-1
Original file line numberDiff line numberDiff line change
@@ -565,11 +565,47 @@ public function testImpersonate()
565565
$controller = new Controller($request, $this->response);
566566
$registry = new ComponentRegistry($controller);
567567
$component = new AuthenticationComponent($registry);
568+
568569
$this->assertEquals($impersonator, $controller->getRequest()->getSession()->read('Auth'));
569570
$this->assertNull($controller->getRequest()->getSession()->read('AuthImpersonate'));
571+
570572
$component->impersonate($impersonated);
571573
$this->assertEquals($impersonated, $controller->getRequest()->getSession()->read('Auth'));
572-
$this->assertEquals($identity, $controller->getRequest()->getSession()->read('AuthImpersonate'));
574+
$this->assertEquals($impersonator, $controller->getRequest()->getSession()->read('AuthImpersonate'));
575+
576+
$component->stopImpersonating();
577+
$this->assertNull($controller->getRequest()->getSession()->read('AuthImpersonate'));
578+
}
579+
580+
/**
581+
* test that impersonate() can handle identities with array data within them.
582+
*
583+
* @return void
584+
*/
585+
public function testImpersonateDecoratorIgnored()
586+
{
587+
$impersonator = ['username' => 'mariano'];
588+
$impersonated = new ArrayObject(['username' => 'larry']);
589+
590+
$this->request->getSession()->write('Auth', $impersonator);
591+
$this->service->authenticate($this->request);
592+
$identity = new Identity($impersonator);
593+
$request = $this->request
594+
->withAttribute('identity', $identity)
595+
->withAttribute('authentication', $this->service);
596+
$controller = new Controller($request, $this->response);
597+
$registry = new ComponentRegistry($controller);
598+
$component = new AuthenticationComponent($registry);
599+
600+
$this->assertEquals($impersonator, $controller->getRequest()->getSession()->read('Auth'));
601+
$this->assertNull($controller->getRequest()->getSession()->read('AuthImpersonate'));
602+
603+
$component->impersonate($impersonated);
604+
$this->assertEquals($impersonated, $controller->getRequest()->getSession()->read('Auth'));
605+
$this->assertEquals(new ArrayObject($impersonator), $controller->getRequest()->getSession()->read('AuthImpersonate'));
606+
607+
$component->stopImpersonating();
608+
$this->assertNull($controller->getRequest()->getSession()->read('AuthImpersonate'));
573609
}
574610

575611
/**

0 commit comments

Comments
 (0)