User: move checkPassword from User to Manager to not break API

remotes/origin/stable6
Arthur Schiwon 11 years ago
parent 40871bab88
commit d101ff42f1
  1. 2
      lib/public/user.php
  2. 14
      lib/user.php
  3. 6
      lib/user/http.php
  4. 17
      lib/user/manager.php
  5. 19
      lib/user/session.php
  6. 18
      lib/user/user.php
  7. 32
      tests/lib/user/session.php

@ -102,7 +102,7 @@ class User {
* @brief Check if the password is correct * @brief Check if the password is correct
* @param $uid The username * @param $uid The username
* @param $password The password * @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 * Check if the password is correct without logging in the user
*/ */

@ -416,16 +416,12 @@ class OC_User {
* returns the user id or false * returns the user id or false
*/ */
public static function checkPassword($uid, $password) { public static function checkPassword($uid, $password) {
$user = self::getManager()->get($uid); $manager = self::getManager();
if ($user) { $username = $manager->checkPassword($uid, $password);
if ($user->checkPassword($password)) { if ($username !== false) {
return $user->getUID(); return $manger->get($username);
} else {
return false;
}
} else {
return false;
} }
return false;
} }
/** /**

@ -79,7 +79,11 @@ class OC_User_HTTP extends OC_User_Backend {
curl_close($ch); curl_close($ch);
return $status==200; if($status == 200) {
return $uid;
}
return false;
} }
/** /**

@ -118,6 +118,23 @@ class Manager extends PublicEmitter {
return ($user !== null); 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 * search by user id
* *

@ -121,15 +121,16 @@ class Session implements Emitter {
*/ */
public function login($uid, $password) { public function login($uid, $password) {
$this->manager->emit('\OC\User', 'preLogin', array($uid, $password)); $this->manager->emit('\OC\User', 'preLogin', array($uid, $password));
$user = $this->manager->get($uid); $user = $this->manager->checkPassword($uid, $password);
if ($user) { if($user !== false) {
$result = $user->checkPassword($password); if (!is_null($user)) {
if ($result and $user->isEnabled()) { if ($user->isEnabled()) {
$this->setUser($user); $this->setUser($user);
$this->manager->emit('\OC\User', 'postLogin', array($user, $password)); $this->manager->emit('\OC\User', 'postLogin', array($user, $password));
return true; return true;
} else { } else {
return false; return false;
}
} }
} else { } else {
return false; return false;

@ -105,24 +105,6 @@ class User {
return !($result === false); 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 * Set the password of the user
* *

@ -61,10 +61,6 @@ class Session extends \PHPUnit_Framework_TestCase {
$backend = $this->getMock('OC_User_Dummy'); $backend = $this->getMock('OC_User_Dummy');
$user = $this->getMock('\OC\User\User', array(), array('foo', $backend)); $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()) $user->expects($this->once())
->method('isEnabled') ->method('isEnabled')
->will($this->returnValue(true)); ->will($this->returnValue(true));
@ -73,8 +69,8 @@ class Session extends \PHPUnit_Framework_TestCase {
->will($this->returnValue('foo')); ->will($this->returnValue('foo'));
$manager->expects($this->once()) $manager->expects($this->once())
->method('get') ->method('checkPassword')
->with('foo') ->with('foo', 'bar')
->will($this->returnValue($user)); ->will($this->returnValue($user));
$userSession = new \OC\User\Session($manager, $session); $userSession = new \OC\User\Session($manager, $session);
@ -92,17 +88,13 @@ class Session extends \PHPUnit_Framework_TestCase {
$backend = $this->getMock('OC_User_Dummy'); $backend = $this->getMock('OC_User_Dummy');
$user = $this->getMock('\OC\User\User', array(), array('foo', $backend)); $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()) $user->expects($this->once())
->method('isEnabled') ->method('isEnabled')
->will($this->returnValue(false)); ->will($this->returnValue(false));
$manager->expects($this->once()) $manager->expects($this->once())
->method('get') ->method('checkPassword')
->with('foo') ->with('foo', 'bar')
->will($this->returnValue($user)); ->will($this->returnValue($user));
$userSession = new \OC\User\Session($manager, $session); $userSession = new \OC\User\Session($manager, $session);
@ -119,17 +111,13 @@ class Session extends \PHPUnit_Framework_TestCase {
$backend = $this->getMock('OC_User_Dummy'); $backend = $this->getMock('OC_User_Dummy');
$user = $this->getMock('\OC\User\User', array(), array('foo', $backend)); $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()) $user->expects($this->never())
->method('isEnabled'); ->method('isEnabled');
$manager->expects($this->once()) $manager->expects($this->once())
->method('get') ->method('checkPassword')
->with('foo') ->with('foo', 'bar')
->will($this->returnValue($user)); ->will($this->returnValue(false));
$userSession = new \OC\User\Session($manager, $session); $userSession = new \OC\User\Session($manager, $session);
$userSession->login('foo', 'bar'); $userSession->login('foo', 'bar');
@ -145,9 +133,9 @@ class Session extends \PHPUnit_Framework_TestCase {
$backend = $this->getMock('OC_User_Dummy'); $backend = $this->getMock('OC_User_Dummy');
$manager->expects($this->once()) $manager->expects($this->once())
->method('get') ->method('checkPassword')
->with('foo') ->with('foo', 'bar')
->will($this->returnValue(null)); ->will($this->returnValue(false));
$userSession = new \OC\User\Session($manager, $session); $userSession = new \OC\User\Session($manager, $session);
$userSession->login('foo', 'bar'); $userSession->login('foo', 'bar');

Loading…
Cancel
Save