Skip to content

Commit 2f62ee2

Browse files
ticket #5017 add userFields setting to BaseAuthenticate
1 parent 579b16d commit 2f62ee2

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

lib/Cake/Controller/Component/Auth/BaseAuthenticate.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ abstract class BaseAuthenticate {
2727
*
2828
* - `fields` The fields to use to identify a user by.
2929
* - `userModel` The model name of the User, defaults to User.
30+
* - `userFields` Array of fields to retrieve from User model, null to retrieve all. Defaults to null.
3031
* - `scope` Additional conditions to use when looking up and authenticating users,
3132
* i.e. `array('User.is_active' => 1).`
3233
* - `recursive` The value of the recursive key passed to find(). Defaults to 0.
@@ -43,6 +44,7 @@ abstract class BaseAuthenticate {
4344
'password' => 'password'
4445
),
4546
'userModel' => 'User',
47+
'userFields' => null,
4648
'scope' => array(),
4749
'recursive' => 0,
4850
'contain' => null,
@@ -105,9 +107,15 @@ protected function _findUser($username, $password = null) {
105107
$conditions = array_merge($conditions, $this->settings['scope']);
106108
}
107109

110+
$userFields = $this->settings['userFields'];
111+
if ($password !== null && $userFields !== null) {
112+
$userFields[] = $fields['password'];
113+
}
114+
108115
$result = ClassRegistry::init($userModel)->find('first', array(
109116
'conditions' => $conditions,
110117
'recursive' => $this->settings['recursive'],
118+
'fields' => $userFields,
111119
'contain' => $this->settings['contain'],
112120
));
113121
if (empty($result[$model])) {

lib/Cake/Test/Case/Controller/Component/Auth/BasicAuthenticateTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,27 @@ public function testAuthenticateSuccess() {
197197
$this->assertEquals($expected, $result);
198198
}
199199

200+
/**
201+
* test userFields success
202+
*
203+
* @return void
204+
*/
205+
public function testAuthenticateUserFieldsSuccess() {
206+
$this->auth->settings['userFields'] = array('id', 'user');
207+
$request = new CakeRequest('posts/index', false);
208+
$request->addParams(array('pass' => array(), 'named' => array()));
209+
210+
$_SERVER['PHP_AUTH_USER'] = 'mariano';
211+
$_SERVER['PHP_AUTH_PW'] = 'password';
212+
213+
$result = $this->auth->authenticate($request, $this->response);
214+
$expected = array(
215+
'id' => 1,
216+
'user' => 'mariano',
217+
);
218+
$this->assertEquals($expected, $result);
219+
}
220+
200221
/**
201222
* test scope failure.
202223
*

0 commit comments

Comments
 (0)