Merge pull request #7884 from nextcloud/cleanup-oc_user

Cleanup OC_User and OCP\User
This commit is contained in:
Morris Jobke 2018-01-17 05:33:07 +01:00 committed by GitHub
commit 0b464e5274
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 42 additions and 171 deletions

View file

@ -148,7 +148,7 @@ class RequestHandlerController extends OCSController {
);
\OCP\Util::writeLog('files_sharing', 'shareWith after, ' . $shareWith, \OCP\Util::DEBUG);
if (!\OCP\User::userExists($shareWith)) {
if (!\OC::$server->getUserManager()->userExists($shareWith)) {
throw new OCSException('User does not exists', 400);
}

View file

@ -66,8 +66,10 @@ class CacheTest extends TestCase {
$this->shareManager = \OC::$server->getShareManager();
\OC_User::setDisplayName(self::TEST_FILES_SHARING_API_USER1, 'User One');
\OC_User::setDisplayName(self::TEST_FILES_SHARING_API_USER2, 'User Two');
$userManager = \OC::$server->getUserManager();
$userManager->get(self::TEST_FILES_SHARING_API_USER1)->setDisplayName('User One');
$userManager->get(self::TEST_FILES_SHARING_API_USER2)->setDisplayName('User Two');
self::loginHelper(self::TEST_FILES_SHARING_API_USER1);

View file

@ -588,7 +588,7 @@ class Access extends LDAPUtility implements IUserTools {
// outside of core user management will still cache the user as non-existing.
$originalTTL = $this->connection->ldapCacheTTL;
$this->connection->setConfiguration(array('ldapCacheTTL' => 0));
if(($isUser && $intName !== '' && !\OCP\User::userExists($intName))
if(($isUser && $intName !== '' && !\OC::$server->getUserManager()->userExists($intName))
|| (!$isUser && !\OC::$server->getGroupManager()->groupExists($intName))) {
if($mapper->map($fdn, $intName, $uuid)) {
$this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL));
@ -721,7 +721,7 @@ class Access extends LDAPUtility implements IUserTools {
//20 attempts, something else is very wrong. Avoids infinite loop.
while($attempts < 20){
$altName = $name . '_' . rand(1000,9999);
if(!\OCP\User::userExists($altName)) {
if(!\OC::$server->getUserManager()->userExists($altName)) {
return $altName;
}
$attempts++;

View file

@ -275,7 +275,11 @@ class User_LDAPTest extends TestCase {
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock());
\OC_User::useBackend($backend);
$result = \OCP\User::checkPassword('roland', 'dt19');
$user = \OC::$server->getUserManager()->checkPassword('roland', 'dt19');
$result = false;
if ($user !== false) {
$result = $user->getUID();
}
$this->assertEquals('gunslinger', $result);
}
@ -285,7 +289,11 @@ class User_LDAPTest extends TestCase {
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock());
\OC_User::useBackend($backend);
$result = \OCP\User::checkPassword('roland', 'wrong');
$user = \OC::$server->getUserManager()->checkPassword('roland', 'wrong');
$result = false;
if ($user !== false) {
$result = $user->getUID();
}
$this->assertFalse($result);
}
@ -295,7 +303,11 @@ class User_LDAPTest extends TestCase {
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock());
\OC_User::useBackend($backend);
$result = \OCP\User::checkPassword('mallory', 'evil');
$user = \OC::$server->getUserManager()->checkPassword('mallory', 'evil');
$result = false;
if ($user !== false) {
$result = $user->getUID();
}
$this->assertFalse($result);
}
@ -629,7 +641,7 @@ class User_LDAPTest extends TestCase {
->willReturn($this->createMock(UserMapping::class));
//test for existing user
$result = \OCP\User::userExists('gunslinger');
$result = \OC::$server->getUserManager()->userExists('gunslinger');
$this->assertTrue($result);
}
@ -656,7 +668,7 @@ class User_LDAPTest extends TestCase {
->willReturn($this->createMock(User::class));
//test for deleted user
\OCP\User::userExists('formerUser');
\OC::$server->getUserManager()->userExists('formerUser');
}
public function testUserExistsPublicAPIForNeverExisting() {
@ -675,7 +687,7 @@ class User_LDAPTest extends TestCase {
}));
//test for never-existing user
$result = \OCP\User::userExists('mallory');
$result = \OC::$server->getUserManager()->userExists('mallory');
$this->assertFalse($result);
}

View file

@ -439,7 +439,7 @@ class Share extends Constants {
\OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName), \OCP\Util::DEBUG);
throw new \Exception($message_t);
}
if (!\OC_User::userExists($shareWith)) {
if (!\OC::$server->getUserManager()->userExists($shareWith)) {
$message = 'Sharing %s failed, because the user %s does not exist';
$message_t = $l->t('Sharing %s failed, because the user %s does not exist', array($itemSourceName, $shareWith));
\OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG);

View file

@ -118,7 +118,7 @@ class OC_JSON{
* @suppress PhanDeprecatedFunction
*/
public static function checkUserExists($user) {
if (!OCP\User::userExists($user)) {
if (!\OC::$server->getUserManager()->userExists($user)) {
$l = \OC::$server->getL10N('lib');
OCP\JSON::error(array('data' => array('message' => $l->t('Unknown user'), 'error' => 'unknown_user' )));
exit;

View file

@ -58,13 +58,6 @@
*/
class OC_User {
/**
* @return \OC\User\Session
*/
public static function getUserSession() {
return OC::$server->getUserSession();
}
private static $_usedBackends = array();
private static $_setupedBackends = array();
@ -176,7 +169,7 @@ class OC_User {
if ($uid) {
if (self::getUser() !== $uid) {
self::setUserId($uid);
$userSession = self::getUserSession();
$userSession = \OC::$server->getUserSession();
$userSession->setLoginName($uid);
$request = OC::$server->getRequest();
$userSession->createSessionToken($request, $uid, $uid);
@ -210,7 +203,7 @@ class OC_User {
//setup extra user backends
self::setupBackends();
self::getUserSession()->unsetMagicInCookie();
\OC::$server->getUserSession()->unsetMagicInCookie();
return self::loginWithApache($backend);
}
@ -234,25 +227,6 @@ class OC_User {
}
}
/**
* Sets user display name for session
*
* @param string $uid
* @param string $displayName
* @return bool Whether the display name could get set
*/
public static function setDisplayName($uid, $displayName = null) {
if (is_null($displayName)) {
$displayName = $uid;
}
$user = \OC::$server->getUserManager()->get($uid);
if ($user) {
return $user->setDisplayName($displayName);
} else {
return false;
}
}
/**
* Check if the user is logged in, considers also the HTTP basic credentials
*
@ -348,7 +322,7 @@ class OC_User {
return $uid;
}
} else {
$user = self::getUserSession()->getUser();
$user = \OC::$server->getUserSession()->getUser();
if ($user) {
return $user->getDisplayName();
} else {
@ -376,25 +350,6 @@ class OC_User {
}
}
/**
* Check if the password is correct
*
* @param string $uid The username
* @param string $password The password
* @return string|false user id a string on success, false otherwise
*
* Check if the password is correct without logging in the user
* returns the user id or false
*/
public static function checkPassword($uid, $password) {
$manager = \OC::$server->getUserManager();
$username = $manager->checkPassword($uid, $password);
if ($username !== false) {
return $username->getUID();
}
return false;
}
/**
* @param string $uid The username
* @return string
@ -450,31 +405,6 @@ class OC_User {
return $displayNames;
}
/**
* check if a user exists
*
* @param string $uid the username
* @return boolean
*/
public static function userExists($uid) {
return \OC::$server->getUserManager()->userExists($uid);
}
/**
* checks if a user is enabled
*
* @param string $uid
* @return bool
*/
public static function isEnabled($uid) {
$user = \OC::$server->getUserManager()->get($uid);
if ($user) {
return $user->isEnabled();
} else {
return false;
}
}
/**
* Returns the first active backend from self::$_usedBackends.
*

View file

@ -274,7 +274,7 @@ class OC_Util {
self::initLocalStorageRootFS();
}
if ($user != '' && !OCP\User::userExists($user)) {
if ($user != '' && !\OC::$server->getUserManager()->userExists($user)) {
\OC::$server->getEventLogger()->end('setup_fs');
return false;
}

View file

@ -115,30 +115,7 @@ class User {
* @since 5.0.0
*/
public static function userExists($uid, $excludingBackend = null) {
return \OC_User::userExists($uid);
}
/**
* Logs the user out including all the session data
* Logout, destroys session
* @deprecated 8.0.0 Use \OC::$server->getUserSession()->logout();
* @since 5.0.0
*/
public static function logout() {
\OC::$server->getUserSession()->logout();
}
/**
* Check if the password is correct
* @param string $uid The username
* @param string $password The password
* @return string|false username on success, false otherwise
*
* Check if the password is correct without logging in the user
* @deprecated 8.0.0 Use \OC::$server->getUserManager()->checkPassword();
* @since 5.0.0
*/
public static function checkPassword( $uid, $password ) {
return \OC_User::checkPassword( $uid, $password );
return \OC::$server->getUserManager()->userExists($uid);
}
/**

View file

@ -62,12 +62,17 @@ if ($config->getSystemValue('sort_groups_by_name', false)) {
}
}
$isAdmin = OC_User::isAdminUser(OC_User::getUser());
$uid = \OC_User::getUser();
$isAdmin = OC_User::isAdminUser($uid);
$isDisabled = !OC_User::isEnabled(OC_User::getUser());
$isDisabled = true;
$user = $userManager->get($uid);
if ($user) {
$isDisabled = !$user->isEnabled();
}
$groupsInfo = new \OC\Group\MetaData(
OC_User::getUser(),
$uid,
$isAdmin,
$groupManager,
\OC::$server->getUserSession()

View file

@ -385,7 +385,7 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase {
$userObject->updateLastLoginTimestamp();
}
\OC_Util::setupFS($user);
if (\OC_User::userExists($user)) {
if (\OC::$server->getUserManager()->userExists($user)) {
\OC::$server->getUserFolder($user);
}
}

View file

@ -50,7 +50,7 @@ trait EncryptionTrait {
\OC_User::setUserId($user);
$this->postLogin();
\OC_Util::setupFS($user);
if (\OC_User::userExists($user)) {
if (\OC::$server->getUserManager()->userExists($user)) {
\OC::$server->getUserFolder($user);
}
}

View file

@ -1,55 +0,0 @@
<?php
/**
* Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace Test;
/**
* Class User
*
* @group DB
*
* @package Test
*/
class UserTest extends TestCase {
/**
* @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
*/
private $backend;
protected function setUp(){
parent::setUp();
$this->backend = $this->createMock(\Test\Util\User\Dummy::class);
$manager = \OC::$server->getUserManager();
$manager->registerBackend($this->backend);
}
public function testCheckPassword() {
$this->backend->expects($this->once())
->method('checkPassword')
->with($this->equalTo('foo'), $this->equalTo('bar'))
->will($this->returnValue('foo'))
;
$this->backend->expects($this->any())
->method('implementsActions')
->will($this->returnCallback(function ($actions) {
if ($actions === \OC\USER\BACKEND::CHECK_PASSWORD) {
return true;
} else {
return false;
}
}));
$uid = \OC_User::checkPassword('foo', 'bar');
$this->assertEquals($uid, 'foo');
}
}