Skip to content

Commit f58ee8c

Browse files
authored
Merge pull request #670 from cakephp/3.1-getid
Add convenience method to get the identifier.
2 parents 8308710 + 110b35c commit f58ee8c

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/Controller/Component/AuthenticationComponent.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,16 @@ public function getResult(): ?ResultInterface
240240
return $this->getAuthenticationService()->getResult();
241241
}
242242

243+
/**
244+
* Get the identifier (primary key) of the identity.
245+
*
246+
* @return array|string|int|null
247+
*/
248+
public function getIdentifier(): array|string|int|null
249+
{
250+
return $this->getIdentity()?->getIdentifier();
251+
}
252+
243253
/**
244254
* Returns the identity used in the authentication attempt.
245255
*

tests/TestCase/Controller/Component/AuthenticationComponentTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,22 @@ public function testGetAuthenticationServiceInvalidServiceObject()
143143
$component->getAuthenticationService();
144144
}
145145

146+
public function testGetId(): void
147+
{
148+
$component = new AuthenticationComponent(new ComponentRegistry(new Controller($this->request)));
149+
$this->assertNull($component->getIdentifier());
150+
151+
$request = $this->request
152+
->withAttribute('identity', $this->identity)
153+
->withAttribute('authentication', $this->service);
154+
155+
$controller = new Controller($request);
156+
$registry = new ComponentRegistry($controller);
157+
$component = new AuthenticationComponent($registry);
158+
159+
$this->assertSame($component->getIdentifier(), $this->identity->getIdentifier());
160+
}
161+
146162
/**
147163
* testGetIdentity
148164
*

0 commit comments

Comments
 (0)