Skip to content

Commit

Permalink
Fixing issues with FormAuthenticate and plugin models.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Feb 4, 2011
1 parent 6860f7c commit ced832b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
18 changes: 10 additions & 8 deletions cake/libs/controller/components/auth/form_authenticate.php
Expand Up @@ -72,19 +72,21 @@ public function __construct($settings) {
*/
public function authenticate(CakeRequest $request) {
$userModel = $this->settings['userModel'];
list($plugin, $model) = pluginSplit($userModel);

$fields = $this->settings['fields'];
if (empty($request->data[$userModel])) {
if (empty($request->data[$model])) {
return false;
}
if (
empty($request->data[$userModel][$fields['username']]) ||
empty($request->data[$userModel][$fields['password']])
empty($request->data[$model][$fields['username']]) ||
empty($request->data[$model][$fields['password']])
) {
return false;
}
$conditions = array(
$userModel . '.' . $fields['username'] => $request->data[$userModel][$fields['username']],
$userModel . '.' . $fields['password'] => $request->data[$userModel][$fields['password']],
$model . '.' . $fields['username'] => $request->data[$model][$fields['username']],
$model . '.' . $fields['password'] => $request->data[$model][$fields['password']],
);
if (!empty($this->settings['scope'])) {
$conditions = array_merge($conditions, $this->settings['scope']);
Expand All @@ -93,10 +95,10 @@ public function authenticate(CakeRequest $request) {
'conditions' => $conditions,
'recursive' => 0
));
if (empty($result) || empty($result[$userModel])) {
if (empty($result) || empty($result[$model])) {
return false;
}
unset($result[$userModel][$fields['password']]);
return $result[$userModel];
unset($result[$model][$fields['password']]);
return $result[$model];
}
}
Expand Up @@ -28,7 +28,7 @@
*/
class FormAuthenticateTest extends CakeTestCase {

public $fixtures = array('core.user');
public $fixtures = array('core.user', 'core.auth_user');

/**
* setup
Expand Down Expand Up @@ -144,4 +144,41 @@ function testAuthenticateScopeFail() {
$this->assertFalse($this->auth->authenticate($request));
}

/**
* test a model in a plugin.
*
* @return void
*/
function testPluginModel() {
Cache::delete('object_map', '_cake_core_');
App::build(array(
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS),
), true);
App::objects('plugin', null, false);

$PluginModel = ClassRegistry::init('TestPlugin.TestPluginAuthUser');
$user['id'] = 1;
$user['username'] = 'gwoo';
$user['password'] = Security::hash(Configure::read('Security.salt') . 'cake');
$PluginModel->save($user, false);

$this->auth->settings['userModel'] = 'TestPlugin.TestPluginAuthUser';
$this->auth->settings['fields']['username'] = 'username';

$request = new CakeRequest('posts/index', false);
$request->data = array('TestPluginAuthUser' => array(
'username' => 'gwoo',
'password' => Security::hash('cake', null, true)
));

$result = $this->auth->authenticate($request);
$expected = array(
'id' => 1,
'username' => 'gwoo',
'created' => '2007-03-17 01:16:23',
'updated' => date('Y-m-d H:i:s')
);
$this->assertEquals($expected, $result);
}

}

0 comments on commit ced832b

Please sign in to comment.