From 6c9b2a1feceda55ef10e2a2421ac825ee89aa5a5 Mon Sep 17 00:00:00 2001 From: Ceeram Date: Thu, 31 May 2012 14:40:23 +0200 Subject: [PATCH] Fix user() return value for nested data --- .../Controller/Component/AuthComponent.php | 4 +-- .../Component/AuthComponentTest.php | 33 +++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Controller/Component/AuthComponent.php b/lib/Cake/Controller/Component/AuthComponent.php index 72f21b0de..35b7ccc7f 100644 --- a/lib/Cake/Controller/Component/AuthComponent.php +++ b/lib/Cake/Controller/Component/AuthComponent.php @@ -569,8 +569,8 @@ class AuthComponent extends Component { if ($key === null) { return $user; } - if (isset($user[$key])) { - return $user[$key]; + if ($value = Hash::get($user, $key)) { + return $value; } return null; } diff --git a/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php b/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php index 189e8ceb4..11546e133 100644 --- a/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php +++ b/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php @@ -1254,4 +1254,37 @@ class AuthComponentTest extends CakeTestCase { $this->assertEquals($expected, $result); } +/** + * testUser method + * + * @return void + */ + public function testUser() { + $data = array( + 'User' => array( + 'id' => '2', + 'username' => 'mark', + 'group_id' => 1, + 'Group' => array( + 'id' => '1', + 'name' => 'Members' + ), + )); + $this->Auth->Session->write('Auth', $data); + + $result = $this->Auth->user(); + $this->assertEquals($data['User'], $result); + + $result = $this->Auth->user('username'); + $this->assertEquals($data['User']['username'], $result); + + $result = $this->Auth->user('Group.name'); + $this->assertEquals($data['User']['Group']['name'], $result); + + $result = $this->Auth->user('invalid'); + $this->assertEquals(null, $result); + + $result = $this->Auth->user('Company.invalid'); + $this->assertEquals(null, $result); + } }