mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Merge pull request #5027 from sebastienbarre/ticket-5017
ticket #5017 add userFields setting to BaseAuthenticate
This commit is contained in:
commit
fb90362121
3 changed files with 85 additions and 1 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[] = $model . '.' . $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])) {
|
||||
|
|
|
@ -60,6 +60,7 @@ class DigestAuthenticate extends BasicAuthenticate {
|
|||
*
|
||||
* - `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.
|
||||
|
@ -78,6 +79,7 @@ class DigestAuthenticate extends BasicAuthenticate {
|
|||
'password' => 'password'
|
||||
),
|
||||
'userModel' => 'User',
|
||||
'userFields' => null,
|
||||
'scope' => array(),
|
||||
'recursive' => 0,
|
||||
'contain' => null,
|
||||
|
|
|
@ -36,7 +36,7 @@ class BasicAuthenticateTest extends CakeTestCase {
|
|||
*
|
||||
* @var array
|
||||
*/
|
||||
public $fixtures = array('core.user', 'core.auth_user');
|
||||
public $fixtures = array('core.user', 'core.auth_user', 'core.article');
|
||||
|
||||
/**
|
||||
* setup
|
||||
|
@ -197,6 +197,80 @@ class BasicAuthenticateTest extends CakeTestCase {
|
|||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* test contain success
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAuthenticateContainSuccess() {
|
||||
$User = ClassRegistry::init('User');
|
||||
$User->bindModel(array('hasMany' => array('Article')));
|
||||
$User->Behaviors->load('Containable');
|
||||
$this->auth->settings['contain'] = 'Article';
|
||||
$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_id' => 1,
|
||||
'title' => 'First Article',
|
||||
'body' => 'First Article Body',
|
||||
'published' => 'Y',
|
||||
'created' => '2007-03-18 10:39:23',
|
||||
'updated' => '2007-03-18 10:41:31'
|
||||
);
|
||||
$this->assertEquals($expected, $result['Article'][0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 userFields and related models success
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAuthenticateUserFieldsRelatedModelsSuccess() {
|
||||
$User = ClassRegistry::init('User');
|
||||
$User->bindModel(array('hasOne' => array('Article')));
|
||||
$this->auth->settings['recursive'] = 0;
|
||||
$this->auth->settings['userFields'] = array('Article.id', 'Article.title');
|
||||
$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,
|
||||
'title' => 'First Article',
|
||||
);
|
||||
$this->assertEquals($expected, $result['Article']);
|
||||
}
|
||||
|
||||
/**
|
||||
* test scope failure.
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue