Skip to content

Commit e559c80

Browse files
authored
Merge pull request #767 from cakephp/identity-helper
Add IdentityHelper::getIdentity()
2 parents 95215e4 + 79c784a commit e559c80

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/View/Helper/IdentityHelper.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,11 @@ public function is(int|string $id, string $field = 'id'): bool
103103
}
104104

105105
/**
106-
* Gets user data
106+
* Get data from the identity.
107+
*
108+
* You can use dot notation to fetch nested data.
109+
* Calling the method without any argument will return
110+
* the entire data array/object (same as `IdentityInterface::getOriginalData()`).
107111
*
108112
* @param string|null $key Key of something you want to get from the identity data
109113
* @return mixed
@@ -120,4 +124,14 @@ public function get(?string $key = null): mixed
120124

121125
return Hash::get($this->_identity, $key);
122126
}
127+
128+
/**
129+
* Returns the identity instance.
130+
*
131+
* @return \Authentication\IdentityInterface|null
132+
*/
133+
public function getIdentity(): ?IdentityInterface
134+
{
135+
return $this->_identity;
136+
}
123137
}

tests/TestCase/View/Helper/IdentityHelperTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,19 @@ public function testWithOutIdentity()
9090
$this->assertNull($helper->getId());
9191

9292
$this->assertFalse($helper->is(1));
93+
94+
$this->assertNull($helper->getIdentity());
95+
}
96+
97+
public function testGetIdentity()
98+
{
99+
$identity = new Identity([
100+
'id' => 1,
101+
]);
102+
$request = (new ServerRequest())->withAttribute('identity', $identity);
103+
$view = new View($request);
104+
105+
$helper = new IdentityHelper($view);
106+
$this->assertSame($identity, $helper->getIdentity());
93107
}
94108
}

0 commit comments

Comments
 (0)