From d101ff42f16ef7288b40666eba20c69621481ea4 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Mon, 16 Sep 2013 14:15:35 +0200 Subject: [PATCH] User: move checkPassword from User to Manager to not break API --- lib/public/user.php | 2 +- lib/user.php | 14 +++++--------- lib/user/http.php | 6 +++++- lib/user/manager.php | 17 +++++++++++++++++ lib/user/session.php | 19 ++++++++++--------- lib/user/user.php | 18 ------------------ tests/lib/user/session.php | 32 ++++++++++---------------------- 7 files changed, 48 insertions(+), 60 deletions(-) diff --git a/lib/public/user.php b/lib/public/user.php index 23ff991642d..576a64d7048 100644 --- a/lib/public/user.php +++ b/lib/public/user.php @@ -102,7 +102,7 @@ class User { * @brief Check if the password is correct * @param $uid The username * @param $password The password - * @returns true/false + * @returns mixed username on success, false otherwise * * Check if the password is correct without logging in the user */ diff --git a/lib/user.php b/lib/user.php index 0f6f40aec9a..8868428ce24 100644 --- a/lib/user.php +++ b/lib/user.php @@ -416,16 +416,12 @@ class OC_User { * returns the user id or false */ public static function checkPassword($uid, $password) { - $user = self::getManager()->get($uid); - if ($user) { - if ($user->checkPassword($password)) { - return $user->getUID(); - } else { - return false; - } - } else { - return false; + $manager = self::getManager(); + $username = $manager->checkPassword($uid, $password); + if ($username !== false) { + return $manger->get($username); } + return false; } /** diff --git a/lib/user/http.php b/lib/user/http.php index 1e044ed4188..ea14cb57c93 100644 --- a/lib/user/http.php +++ b/lib/user/http.php @@ -79,7 +79,11 @@ class OC_User_HTTP extends OC_User_Backend { curl_close($ch); - return $status==200; + if($status == 200) { + return $uid; + } + + return false; } /** diff --git a/lib/user/manager.php b/lib/user/manager.php index 8dc9bfe2729..2de694a3d9f 100644 --- a/lib/user/manager.php +++ b/lib/user/manager.php @@ -118,6 +118,23 @@ class Manager extends PublicEmitter { return ($user !== null); } + /** + * Check if the password is valid for the user + * + * @param $loginname + * @param $password + * @return mixed the User object on success, false otherwise + */ + public function checkPassword($loginname, $password) { + foreach ($this->backends as $backend) { + $uid = $backend->checkPassword($loginname, $password); + if ($uid !== false) { + return $this->getUserObject($uid, $backend); + } + } + return null; + } + /** * search by user id * diff --git a/lib/user/session.php b/lib/user/session.php index 9a6c669e935..b5e9385234d 100644 --- a/lib/user/session.php +++ b/lib/user/session.php @@ -121,15 +121,16 @@ class Session implements Emitter { */ public function login($uid, $password) { $this->manager->emit('\OC\User', 'preLogin', array($uid, $password)); - $user = $this->manager->get($uid); - if ($user) { - $result = $user->checkPassword($password); - if ($result and $user->isEnabled()) { - $this->setUser($user); - $this->manager->emit('\OC\User', 'postLogin', array($user, $password)); - return true; - } else { - return false; + $user = $this->manager->checkPassword($uid, $password); + if($user !== false) { + if (!is_null($user)) { + if ($user->isEnabled()) { + $this->setUser($user); + $this->manager->emit('\OC\User', 'postLogin', array($user, $password)); + return true; + } else { + return false; + } } } else { return false; diff --git a/lib/user/user.php b/lib/user/user.php index 8115c43198c..e5f842944f1 100644 --- a/lib/user/user.php +++ b/lib/user/user.php @@ -105,24 +105,6 @@ class User { return !($result === false); } - /** - * Check if the password is valid for the user - * - * @param $password - * @return bool - */ - public function checkPassword($password) { - if ($this->backend->implementsActions(\OC_USER_BACKEND_CHECK_PASSWORD)) { - $result = $this->backend->checkPassword($this->uid, $password); - if ($result !== false) { - $this->uid = $result; - } - return !($result === false); - } else { - return false; - } - } - /** * Set the password of the user * diff --git a/tests/lib/user/session.php b/tests/lib/user/session.php index 274e9e2831e..e457a7bda30 100644 --- a/tests/lib/user/session.php +++ b/tests/lib/user/session.php @@ -61,10 +61,6 @@ class Session extends \PHPUnit_Framework_TestCase { $backend = $this->getMock('OC_User_Dummy'); $user = $this->getMock('\OC\User\User', array(), array('foo', $backend)); - $user->expects($this->once()) - ->method('checkPassword') - ->with('bar') - ->will($this->returnValue(true)); $user->expects($this->once()) ->method('isEnabled') ->will($this->returnValue(true)); @@ -73,8 +69,8 @@ class Session extends \PHPUnit_Framework_TestCase { ->will($this->returnValue('foo')); $manager->expects($this->once()) - ->method('get') - ->with('foo') + ->method('checkPassword') + ->with('foo', 'bar') ->will($this->returnValue($user)); $userSession = new \OC\User\Session($manager, $session); @@ -92,17 +88,13 @@ class Session extends \PHPUnit_Framework_TestCase { $backend = $this->getMock('OC_User_Dummy'); $user = $this->getMock('\OC\User\User', array(), array('foo', $backend)); - $user->expects($this->once()) - ->method('checkPassword') - ->with('bar') - ->will($this->returnValue(true)); $user->expects($this->once()) ->method('isEnabled') ->will($this->returnValue(false)); $manager->expects($this->once()) - ->method('get') - ->with('foo') + ->method('checkPassword') + ->with('foo', 'bar') ->will($this->returnValue($user)); $userSession = new \OC\User\Session($manager, $session); @@ -119,17 +111,13 @@ class Session extends \PHPUnit_Framework_TestCase { $backend = $this->getMock('OC_User_Dummy'); $user = $this->getMock('\OC\User\User', array(), array('foo', $backend)); - $user->expects($this->once()) - ->method('checkPassword') - ->with('bar') - ->will($this->returnValue(false)); $user->expects($this->never()) ->method('isEnabled'); $manager->expects($this->once()) - ->method('get') - ->with('foo') - ->will($this->returnValue($user)); + ->method('checkPassword') + ->with('foo', 'bar') + ->will($this->returnValue(false)); $userSession = new \OC\User\Session($manager, $session); $userSession->login('foo', 'bar'); @@ -145,9 +133,9 @@ class Session extends \PHPUnit_Framework_TestCase { $backend = $this->getMock('OC_User_Dummy'); $manager->expects($this->once()) - ->method('get') - ->with('foo') - ->will($this->returnValue(null)); + ->method('checkPassword') + ->with('foo', 'bar') + ->will($this->returnValue(false)); $userSession = new \OC\User\Session($manager, $session); $userSession->login('foo', 'bar'); -- 2.39.5