mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
ticket #5017 add userFields setting to BaseAuthenticate
This commit is contained in:
parent
579b16d90b
commit
2f62ee2cde
2 changed files with 29 additions and 0 deletions
|
@ -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.
|
||||
|
@ -43,6 +44,7 @@ abstract class BaseAuthenticate {
|
|||
'password' => 'password'
|
||||
),
|
||||
'userModel' => 'User',
|
||||
'userFields' => null,
|
||||
'scope' => array(),
|
||||
'recursive' => 0,
|
||||
'contain' => null,
|
||||
|
@ -105,9 +107,15 @@ abstract class BaseAuthenticate {
|
|||
$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])) {
|
||||
|
|
|
@ -197,6 +197,27 @@ class BasicAuthenticateTest extends CakeTestCase {
|
|||
$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.
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue