From f49d9bac6c9ffadd7a4ea2b4ab60efa78ea2753f Mon Sep 17 00:00:00 2001 From: ADmad Date: Thu, 29 Jan 2026 21:33:30 +0530 Subject: [PATCH 1/2] Allow using dot separated field names for Identity::get() --- composer.json | 1 + src/Identity.php | 15 ++++++++------- tests/TestCase/IdentityTest.php | 18 ++++++++++++++++++ 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index ca993987..f1cb210e 100644 --- a/composer.json +++ b/composer.json @@ -25,6 +25,7 @@ "require": { "php": ">=8.1", "cakephp/http": "^5.0", + "cakephp/utility": "^5.0", "laminas/laminas-diactoros": "^3.0", "psr/http-client": "^1.0", "psr/http-message": "^1.1 || ^2.0", diff --git a/src/Identity.php b/src/Identity.php index 0a699fce..9e119dd1 100644 --- a/src/Identity.php +++ b/src/Identity.php @@ -19,6 +19,7 @@ use ArrayAccess; use BadMethodCallException; use Cake\Core\InstanceConfigTrait; +use Cake\Utility\Hash; /** * Identity object @@ -92,21 +93,21 @@ public function __isset(string $field): bool /** * Get data from the identity * - * @param string $field Field in the user data. + * @param string|null $field Field in the user data. * @return mixed */ - public function get(string $field): mixed + public function get(?string $field = null): mixed { + if ($field === null) { + return $this->data; + } + $map = $this->_config['fieldMap']; if (isset($map[$field])) { $field = $map[$field]; } - if (isset($this->data[$field])) { - return $this->data[$field]; - } - - return null; + return Hash::get($this->data, $field); } /** diff --git a/tests/TestCase/IdentityTest.php b/tests/TestCase/IdentityTest.php index 731c0c51..4f8ba363 100644 --- a/tests/TestCase/IdentityTest.php +++ b/tests/TestCase/IdentityTest.php @@ -19,6 +19,7 @@ use ArrayObject; use Authentication\Identity; use BadMethodCallException; +use Cake\ORM\Entity; use Cake\TestSuite\TestCase; class IdentityTest extends TestCase @@ -43,6 +44,23 @@ public function testGetIdentifier() $this->assertSame('florian', $identity->username); } + public function testGet(): void + { + $data = new Entity([ + 'id' => 1, + 'username' => 'florian', + 'account' => new Entity(['id' => 2, 'role' => 'admin']), + ]); + + $identity = new Identity($data); + + $this->assertSame(1, $identity->get('id')); + $this->assertSame('florian', $identity->get('username')); + $this->assertSame('admin', $identity->get('account.role')); + $this->assertNull($identity->get('missing')); + $this->assertSame($data, $identity->get()); + } + /** * Test mapping fields * From 19df42a74bdcb99c1b34dee2f19f66e7a533fb3d Mon Sep 17 00:00:00 2001 From: ADmad Date: Fri, 30 Jan 2026 12:57:24 +0530 Subject: [PATCH 2/2] Update docblock --- src/Identity.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Identity.php b/src/Identity.php index 9e119dd1..18d5281b 100644 --- a/src/Identity.php +++ b/src/Identity.php @@ -91,7 +91,11 @@ public function __isset(string $field): bool } /** - * Get data from the identity + * Get data from the identity. + * + * You can use dot notation to fetch nested data. + * Calling the method without any argument will return + * the entire data array/object (same as `getOriginalData()`). * * @param string|null $field Field in the user data. * @return mixed