]> source.dussan.org Git - nextcloud-server.git/commitdiff
User: move checkPassword from User to Manager to not break API
authorArthur Schiwon <blizzz@owncloud.com>
Mon, 16 Sep 2013 12:15:35 +0000 (14:15 +0200)
committerArthur Schiwon <blizzz@owncloud.com>
Tue, 24 Sep 2013 11:46:30 +0000 (13:46 +0200)
lib/public/user.php
lib/user.php
lib/user/http.php
lib/user/manager.php
lib/user/session.php
lib/user/user.php
tests/lib/user/session.php

index 23ff991642dfb7e9c457bb34c5c5698c9eaeceb0..576a64d70489d0f5deb2f573d645a316e3968a8c 100644 (file)
@@ -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
         */
index 0f6f40aec9ae414fc0318f2d33ef1fa2bcb3de9f..8868428ce245ea8237828d64b8e601d261dad30e 100644 (file)
@@ -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;
        }
 
        /**
index 1e044ed4188ae99a930110fbf05ad2f29ae6d4cb..ea14cb57c938ea528779672c9c072238bb5a2fa9 100644 (file)
@@ -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;
        }
 
        /**
index 8dc9bfe27297a31cad756eeceaf3f74cd25ef095..2de694a3d9fc2ae9924cba98c05e901cf937209e 100644 (file)
@@ -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
         *
index 9a6c669e935a8010c8c593c93a59f561c8a6df48..b5e9385234d3750cc1cd010564caef96bf76e34a 100644 (file)
@@ -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;
index 8115c43198cde309baac391334dca7ab1ef3ba56..e5f842944f105c021c6cd29643c34d50dba1ae7b 100644 (file)
@@ -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
         *
index 274e9e2831eea8bce10721625458fb5d9f85a442..e457a7bda30218463cec7fb1bfdf56ed0e5e8e97 100644 (file)
@@ -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');