Skip to content

Commit 462ff6a

Browse files
committed
Add IdentityHelper::getIdentity()
1 parent 1c8bd69 commit 462ff6a

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

src/View/Helper/IdentityHelper.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,16 @@ public function get(?string $key = null): mixed
120120

121121
return Hash::get($this->_identity, $key);
122122
}
123+
124+
/**
125+
* Returns the identity instance.
126+
*
127+
* @return \Authentication\IdentityInterface|null
128+
*/
129+
public function getIdentity(): ?IdentityInterface
130+
{
131+
return $this->_View
132+
->getRequest()
133+
->getAttribute($this->getConfig('identityAttribute'));
134+
}
123135
}

tests/TestCase/View/Helper/IdentityHelperTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,16 @@ public function testWithOutIdentity()
9191

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

0 commit comments

Comments
 (0)