Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
21 changes: 13 additions & 8 deletions src/Identity.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use ArrayAccess;
use BadMethodCallException;
use Cake\Core\InstanceConfigTrait;
use Cake\Utility\Hash;

/**
* Identity object
Expand Down Expand Up @@ -90,23 +91,27 @@ public function __isset(string $field): bool
}

/**
* Get data from the identity
* Get data from the identity.
*
* @param string $field Field in the user data.
* 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
*/
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);
}

/**
Expand Down
18 changes: 18 additions & 0 deletions tests/TestCase/IdentityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use ArrayObject;
use Authentication\Identity;
use BadMethodCallException;
use Cake\ORM\Entity;
use Cake\TestSuite\TestCase;

class IdentityTest extends TestCase
Expand All @@ -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
*
Expand Down