diff --git a/src/Controller/Component/AuthenticationComponent.php b/src/Controller/Component/AuthenticationComponent.php index d01f93b8..b3acbc7e 100644 --- a/src/Controller/Component/AuthenticationComponent.php +++ b/src/Controller/Component/AuthenticationComponent.php @@ -240,6 +240,16 @@ public function getResult(): ?ResultInterface return $this->getAuthenticationService()->getResult(); } + /** + * Get the identifier (primary key) of the identity. + * + * @return array|string|int|null + */ + public function getId(): array|string|int|null + { + return $this->getIdentity()?->getIdentifier(); + } + /** * Returns the identity used in the authentication attempt. * diff --git a/tests/TestCase/Controller/Component/AuthenticationComponentTest.php b/tests/TestCase/Controller/Component/AuthenticationComponentTest.php index 45d6b4c6..a7d0d007 100644 --- a/tests/TestCase/Controller/Component/AuthenticationComponentTest.php +++ b/tests/TestCase/Controller/Component/AuthenticationComponentTest.php @@ -143,6 +143,22 @@ public function testGetAuthenticationServiceInvalidServiceObject() $component->getAuthenticationService(); } + public function testGetId(): void + { + $component = new AuthenticationComponent(new ComponentRegistry(new Controller($this->request))); + $this->assertNull($component->getId()); + + $request = $this->request + ->withAttribute('identity', $this->identity) + ->withAttribute('authentication', $this->service); + + $controller = new Controller($request); + $registry = new ComponentRegistry($controller); + $component = new AuthenticationComponent($registry); + + $this->assertSame($component->getId(), $this->identity->getIdentifier()); + } + /** * testGetIdentity *