Skip to content

Commit

Permalink
ticket #5017 add userFields setting to BaseAuthenticate
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienbarre committed Oct 31, 2014
1 parent 579b16d commit 2f62ee2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/Cake/Controller/Component/Auth/BaseAuthenticate.php
Expand Up @@ -27,6 +27,7 @@ abstract class BaseAuthenticate {
*
* - `fields` The fields to use to identify a user by.
* - `userModel` The model name of the User, defaults to User.
* - `userFields` Array of fields to retrieve from User model, null to retrieve all. Defaults to null.
* - `scope` Additional conditions to use when looking up and authenticating users,
* i.e. `array('User.is_active' => 1).`
* - `recursive` The value of the recursive key passed to find(). Defaults to 0.
Expand All @@ -43,6 +44,7 @@ abstract class BaseAuthenticate {
'password' => 'password'
),
'userModel' => 'User',
'userFields' => null,
'scope' => array(),
'recursive' => 0,
'contain' => null,
Expand Down Expand Up @@ -105,9 +107,15 @@ protected function _findUser($username, $password = null) {
$conditions = array_merge($conditions, $this->settings['scope']);
}

$userFields = $this->settings['userFields'];
if ($password !== null && $userFields !== null) {
$userFields[] = $fields['password'];
}

$result = ClassRegistry::init($userModel)->find('first', array(
'conditions' => $conditions,
'recursive' => $this->settings['recursive'],
'fields' => $userFields,
'contain' => $this->settings['contain'],
));
if (empty($result[$model])) {
Expand Down
Expand Up @@ -197,6 +197,27 @@ public function testAuthenticateSuccess() {
$this->assertEquals($expected, $result);
}

/**
* test userFields success
*
* @return void
*/
public function testAuthenticateUserFieldsSuccess() {
$this->auth->settings['userFields'] = array('id', 'user');
$request = new CakeRequest('posts/index', false);
$request->addParams(array('pass' => array(), 'named' => array()));

$_SERVER['PHP_AUTH_USER'] = 'mariano';
$_SERVER['PHP_AUTH_PW'] = 'password';

$result = $this->auth->authenticate($request, $this->response);
$expected = array(
'id' => 1,
'user' => 'mariano',
);
$this->assertEquals($expected, $result);
}

/**
* test scope failure.
*
Expand Down

0 comments on commit 2f62ee2

Please sign in to comment.