Fixing issues with FormAuthenticate and plugin models.

This commit is contained in:
mark_story 2011-01-03 18:17:27 -05:00
parent 6860f7cc03
commit ced832ba62
2 changed files with 48 additions and 9 deletions

View file

@ -72,19 +72,21 @@ class FormAuthenticate {
*/
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']);
@ -93,10 +95,10 @@ class FormAuthenticate {
'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];
}
}

View file

@ -28,7 +28,7 @@ require_once CAKE_TESTS . 'cases' . DS . 'libs' . DS . 'model' . DS . 'models.p
*/
class FormAuthenticateTest extends CakeTestCase {
public $fixtures = array('core.user');
public $fixtures = array('core.user', 'core.auth_user');
/**
* setup
@ -144,4 +144,41 @@ class FormAuthenticateTest extends CakeTestCase {
$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);
}
}