From 7ea914938fc0715a0cef6c5f878336d4998bd57f Mon Sep 17 00:00:00 2001 From: mark_story Date: Tue, 4 Jan 2011 23:27:16 -0500 Subject: [PATCH] Changing AuthComponent::login() so you can provide an array of user data to manually login a user. Leaving $user blank will attempt to identify the user using the request. --- cake/libs/controller/components/auth.php | 26 ++++++------------- .../libs/controller/components/auth.test.php | 19 ++++++++++++++ 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/cake/libs/controller/components/auth.php b/cake/libs/controller/components/auth.php index 244035d48..2ce617878 100644 --- a/cake/libs/controller/components/auth.php +++ b/cake/libs/controller/components/auth.php @@ -264,13 +264,6 @@ class AuthComponent extends Component { */ public $params = array(); -/** - * AclComponent instance if using Acl + Auth - * - * @var AclComponent - */ - public $Acl; - /** * Method list for bound controller * @@ -576,25 +569,22 @@ class AuthComponent extends Component { } /** - * Manually log-in a user with the given parameter data. The $data provided can be any data - * structure used to identify a user in AuthComponent::identify(). If $data is empty or not - * specified, POST data from Controller::$data will be used automatically. + * Log a user in. If a $user is provided that data will be stored as the logged in user. If `$user` is empty or not + * specified, POST data from the current request will be used to identify a user. If the login was successful, + * the user record is written to the session key specified in AuthComponent::$sessionKey. * - * After (if) login is successful, the user record is written to the session key specified in - * AuthComponent::$sessionKey. - * - * @param mixed $data User object + * @param mixed $user Either an array of user data, or null to identify a user using the current request. * @return boolean True on login success, false on failure * @link http://book.cakephp.org/view/1261/login */ - public function login($request = null) { + public function login($user = null) { $this->__setDefaults(); $this->_loggedIn = false; - if (empty($request)) { - $request = $this->request; + if (empty($user)) { + $user = $this->identify($this->request); } - if ($user = $this->identify($request)) { + if ($user) { $this->Session->write($this->sessionKey, $user); $this->_loggedIn = true; } diff --git a/cake/tests/cases/libs/controller/components/auth.test.php b/cake/tests/cases/libs/controller/components/auth.test.php index 4381c3a2d..97115e5b6 100644 --- a/cake/tests/cases/libs/controller/components/auth.test.php +++ b/cake/tests/cases/libs/controller/components/auth.test.php @@ -1486,4 +1486,23 @@ class AuthTest extends CakeTestCase { $this->Controller->Auth->mapActions(array('create' => array('my_action'))); } + +/** + * test login() with user data + * + * @return void + */ + function testLoginWithUserData() { + $this->assertFalse($this->Controller->Auth->loggedIn()); + + $user = array( + 'username' => 'mariano', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:16:23', + 'updated' => '2007-03-17 01:18:31' + ); + $this->assertTrue($this->Controller->Auth->login($user)); + $this->assertTrue($this->Controller->Auth->loggedIn()); + $this->assertEquals($user['username'], $this->Controller->Auth->user('username')); + } }