Skip to content

Commit

Permalink
Adding support for plugin models used in Auth::$userModel
Browse files Browse the repository at this point in the history
  • Loading branch information
renan committed Aug 13, 2009
1 parent 2893b29 commit 8d6135a
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 31 deletions.
62 changes: 31 additions & 31 deletions cake/libs/controller/components/auth.php
Expand Up @@ -330,23 +330,24 @@ function startup(&$controller) {
}

if ($loginAction == $url) {
if (empty($controller->data) || !isset($controller->data[$this->userModel])) {
$model =& $this->getModel();
if (empty($controller->data) || !isset($controller->data[$model->alias])) {
if (!$this->Session->check('Auth.redirect') && env('HTTP_REFERER')) {
$this->Session->write('Auth.redirect', $controller->referer(null, true));
}
return false;
}

$isValid = !empty($controller->data[$this->userModel][$this->fields['username']]) &&
!empty($controller->data[$this->userModel][$this->fields['password']]);
$isValid = !empty($controller->data[$model->alias][$this->fields['username']]) &&
!empty($controller->data[$model->alias][$this->fields['password']]);

if ($isValid) {
$username = $controller->data[$this->userModel][$this->fields['username']];
$password = $controller->data[$this->userModel][$this->fields['password']];
$username = $controller->data[$model->alias][$this->fields['username']];
$password = $controller->data[$model->alias][$this->fields['password']];

$data = array(
$this->userModel . '.' . $this->fields['username'] => $username,
$this->userModel . '.' . $this->fields['password'] => $password
$model->alias . '.' . $this->fields['username'] => $username,
$model->alias . '.' . $this->fields['password'] => $password
);

if ($this->login($data)) {
Expand All @@ -358,7 +359,7 @@ function startup(&$controller) {
}

$this->Session->setFlash($this->loginError, 'default', array(), 'auth');
$controller->data[$this->userModel][$this->fields['password']] = null;
$controller->data[$model->alias][$this->fields['password']] = null;
return false;
} else {
if (!$this->user()) {
Expand Down Expand Up @@ -698,7 +699,8 @@ function user($key = null) {
}

if ($key == null) {
return array($this->userModel => $this->Session->read($this->sessionKey));
$model =& $this->getModel();
return array($model->alias => $this->Session->read($this->sessionKey));
} else {
$user = $this->Session->read($this->sessionKey);
if (isset($user[$key])) {
Expand Down Expand Up @@ -817,6 +819,7 @@ function identify($user = null, $conditions = null) {
} else {
$conditions = $this->userScope;
}
$model =& $this->getModel();
if (empty($user)) {
$user = $this->user();
if (empty($user)) {
Expand All @@ -827,51 +830,47 @@ function identify($user = null, $conditions = null) {
return null;
}
$user = $user->read();
$user = $user[$this->userModel];
} elseif (is_array($user) && isset($user[$this->userModel])) {
$user = $user[$this->userModel];
$user = $user[$model->alias];
} elseif (is_array($user) && isset($user[$model->alias])) {
$user = $user[$model->alias];
}

if (is_array($user) && (isset($user[$this->fields['username']]) || isset($user[$this->userModel . '.' . $this->fields['username']]))) {

if (is_array($user) && (isset($user[$this->fields['username']]) || isset($user[$model->alias . '.' . $this->fields['username']]))) {
if (isset($user[$this->fields['username']]) && !empty($user[$this->fields['username']]) && !empty($user[$this->fields['password']])) {
if (trim($user[$this->fields['username']]) == '=' || trim($user[$this->fields['password']]) == '=') {
return false;
}
$find = array(
$this->userModel.'.'.$this->fields['username'] => $user[$this->fields['username']],
$this->userModel.'.'.$this->fields['password'] => $user[$this->fields['password']]
$model->alias.'.'.$this->fields['username'] => $user[$this->fields['username']],
$model->alias.'.'.$this->fields['password'] => $user[$this->fields['password']]
);
} elseif (isset($user[$this->userModel . '.' . $this->fields['username']]) && !empty($user[$this->userModel . '.' . $this->fields['username']])) {
if (trim($user[$this->userModel . '.' . $this->fields['username']]) == '=' || trim($user[$this->userModel . '.' . $this->fields['password']]) == '=') {
} elseif (isset($user[$model->alias . '.' . $this->fields['username']]) && !empty($user[$model->alias . '.' . $this->fields['username']])) {
if (trim($user[$model->alias . '.' . $this->fields['username']]) == '=' || trim($user[$model->alias . '.' . $this->fields['password']]) == '=') {
return false;
}
$find = array(
$this->userModel.'.'.$this->fields['username'] => $user[$this->userModel . '.' . $this->fields['username']],
$this->userModel.'.'.$this->fields['password'] => $user[$this->userModel . '.' . $this->fields['password']]
$model->alias.'.'.$this->fields['username'] => $user[$model->alias . '.' . $this->fields['username']],
$model->alias.'.'.$this->fields['password'] => $user[$model->alias . '.' . $this->fields['password']]
);
} else {
return false;
}
$model =& $this->getModel();
$data = $model->find(array_merge($find, $conditions), null, null, 0);
if (empty($data) || empty($data[$this->userModel])) {
if (empty($data) || empty($data[$model->alias])) {
return null;
}
} elseif (!empty($user) && is_string($user)) {
$model =& $this->getModel();
$data = $model->find(array_merge(array($model->escapeField() => $user), $conditions));

if (empty($data) || empty($data[$this->userModel])) {
if (empty($data) || empty($data[$model->alias])) {
return null;
}
}

if (!empty($data)) {
if (!empty($data[$this->userModel][$this->fields['password']])) {
unset($data[$this->userModel][$this->fields['password']]);
if (!empty($data[$model->alias][$this->fields['password']])) {
unset($data[$model->alias][$this->fields['password']]);
}
return $data[$this->userModel];
return $data[$model->alias];
}
return null;
}
Expand All @@ -888,9 +887,10 @@ function hashPasswords($data) {
return $this->authenticate->hashPasswords($data);
}

if (is_array($data) && isset($data[$this->userModel])) {
if (isset($data[$this->userModel][$this->fields['username']]) && isset($data[$this->userModel][$this->fields['password']])) {
$data[$this->userModel][$this->fields['password']] = $this->password($data[$this->userModel][$this->fields['password']]);
$model =& $this->getModel();
if (is_array($data) && isset($data[$model->alias])) {
if (isset($data[$model->alias][$this->fields['username']]) && isset($data[$model->alias][$this->fields['password']])) {
$data[$model->alias][$this->fields['password']] = $this->password($data[$model->alias][$this->fields['password']]);
}
}
return $data;
Expand Down
50 changes: 50 additions & 0 deletions cake/tests/cases/libs/controller/components/auth.test.php
Expand Up @@ -272,6 +272,7 @@ function __construct() {
* @return void
*/
function beforeFilter() {
$this->Auth->userModel = 'AuthUser';
}

/**
Expand Down Expand Up @@ -495,6 +496,8 @@ function startTest() {

$this->Controller =& new AuthTestController();
$this->Controller->Component->init($this->Controller);
$this->Controller->Component->initialize($this->Controller);
$this->Controller->beforeFilter();

ClassRegistry::addObject('view', new View($this->Controller));

Expand Down Expand Up @@ -1260,6 +1263,53 @@ function testAdminRoute() {
Configure::write('Routing.admin', $admin);
}

/**
* testPluginModel method
*
* @access public
* @return void
*/
function testPluginModel() {
// Adding plugins
Cache::delete('object_map', '_cake_core_');
App::build(array(
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS),
'models' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'models' . 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);

$authUser = $PluginModel->find();

$this->Controller->data['TestPluginAuthUser']['username'] = $authUser['TestPluginAuthUser']['username'];
$this->Controller->data['TestPluginAuthUser']['password'] = 'cake';

$this->Controller->params = Router::parse('auth_test/login');
$this->Controller->params['url']['url'] = 'auth_test/login';

$this->Controller->Auth->initialize($this->Controller);

$this->Controller->Auth->loginAction = 'auth_test/login';
$this->Controller->Auth->userModel = 'TestPlugin.TestPluginAuthUser';

$this->Controller->Auth->startup($this->Controller);
$user = $this->Controller->Auth->user();
$expected = array('TestPluginAuthUser' => array(
'id' => 1, 'username' => 'gwoo', 'created' => '2007-03-17 01:16:23', 'updated' => date('Y-m-d H:i:s')
));
$this->assertEqual($user, $expected);

// Reverting changes
Cache::delete('object_map', '_cake_core_');
App::build();
App::objects('plugin', null, false);
}

/**
* testAjaxLogin method
*
Expand Down
@@ -0,0 +1,52 @@
<?php
/* SVN FILE: $Id$ */

/**
* Test Plugin Auth User Model
*
*
*
* PHP versions 4 and 5
*
* CakePHP : Rapid Development Framework (http://www.cakephp.org)
* Copyright 2006-2008, Cake Software Foundation, Inc.
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2006-2008, Cake Software Foundation, Inc.
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
* @package cake
* @subpackage cake.cake.tests.test_app.plugins.test_plugin
* @since CakePHP v 1.2.0.4487
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/

class TestPluginAuthUser extends TestPluginAppModel {

/**
* Name property
*
* @var string
*/
var $name = 'TestPluginAuthUser';

/**
* useTable property
*
* @var string
*/
var $useTable = 'auth_users';

/**
* useDbConfig property
*
* @var string 'test_suite'
* @access public
*/
var $useDbConfig = 'test_suite';
}

0 comments on commit 8d6135a

Please sign in to comment.