Merge pull request #6522 from nextcloud/fix-3365

fix LDAP User deletion (cleanup), fixes #3365
This commit is contained in:
Morris Jobke 2017-09-29 14:05:34 +02:00 committed by GitHub
commit 2840eb0e1d
18 changed files with 121 additions and 82 deletions

View File

@ -44,9 +44,10 @@ if(count($configPrefixes) > 0) {
'name' => $l->t('LDAP user and group backend'), 'name' => $l->t('LDAP user and group backend'),
]; ];
}); });
$userSession = \OC::$server->getUserSession();
$userBackend = new OCA\User_LDAP\User_Proxy( $userBackend = new OCA\User_LDAP\User_Proxy(
$configPrefixes, $ldapWrapper, $ocConfig, $notificationManager $configPrefixes, $ldapWrapper, $ocConfig, $notificationManager, $userSession
); );
$groupBackend = new OCA\User_LDAP\Group_Proxy($configPrefixes, $ldapWrapper); $groupBackend = new OCA\User_LDAP\Group_Proxy($configPrefixes, $ldapWrapper);
// register user backend // register user backend

View File

@ -36,7 +36,8 @@ $uBackend = new User_Proxy(
$helper->getServerConfigurationPrefixes(true), $helper->getServerConfigurationPrefixes(true),
new LDAP(), new LDAP(),
$ocConfig, $ocConfig,
\OC::$server->getNotificationManager() \OC::$server->getNotificationManager(),
\OC::$server->getUserSession()
); );
$deletedUsersIndex = new DeletedUsersIndex( $deletedUsersIndex = new DeletedUsersIndex(
$ocConfig, $dbConnection, $userMapping $ocConfig, $dbConnection, $userMapping

View File

@ -120,7 +120,13 @@ class Search extends Command {
$limit = null; $limit = null;
} }
} else { } else {
$proxy = new User_Proxy($configPrefixes, $ldapWrapper, $this->ocConfig, \OC::$server->getNotificationManager()); $proxy = new User_Proxy(
$configPrefixes,
$ldapWrapper,
$this->ocConfig,
\OC::$server->getNotificationManager(),
\OC::$server->getUserSession()
);
$getMethod = 'getDisplayNames'; $getMethod = 'getDisplayNames';
$printID = true; $printID = true;
} }

View File

@ -294,9 +294,10 @@ class Helper {
$ldapWrapper = new LDAP(); $ldapWrapper = new LDAP();
$ocConfig = \OC::$server->getConfig(); $ocConfig = \OC::$server->getConfig();
$notificationManager = \OC::$server->getNotificationManager(); $notificationManager = \OC::$server->getNotificationManager();
$userSession = \OC::$server->getUserSession();
$userBackend = new User_Proxy( $userBackend = new User_Proxy(
$configPrefixes, $ldapWrapper, $ocConfig, $notificationManager $configPrefixes, $ldapWrapper, $ocConfig, $notificationManager, $userSession
); );
$uid = $userBackend->loginName2UserName($param['uid'] ); $uid = $userBackend->loginName2UserName($param['uid'] );
if($uid !== false) { if($uid !== false) {

View File

@ -99,7 +99,8 @@ class CleanUp extends TimedJob {
$this->ldapHelper->getServerConfigurationPrefixes(true), $this->ldapHelper->getServerConfigurationPrefixes(true),
new LDAP(), new LDAP(),
$this->ocConfig, $this->ocConfig,
\OC::$server->getNotificationManager() \OC::$server->getNotificationManager(),
\OC::$server->getUserSession()
); );
} }

View File

@ -33,6 +33,6 @@ class UUIDFixGroup extends UUIDFix {
public function __construct(GroupMapping $mapper, LDAP $ldap, IConfig $config, Helper $helper) { public function __construct(GroupMapping $mapper, LDAP $ldap, IConfig $config, Helper $helper) {
$this->mapper = $mapper; $this->mapper = $mapper;
$this->proxy = new User_Proxy($helper->getServerConfigurationPrefixes(true), $ldap, $config, $this->proxy = new User_Proxy($helper->getServerConfigurationPrefixes(true), $ldap, $config,
\OC::$server->getNotificationManager()); \OC::$server->getNotificationManager(), \OC::$server->getUserSession());
} }
} }

View File

@ -42,6 +42,7 @@ use OCA\User_LDAP\User\OfflineUser;
use OCA\User_LDAP\User\User; use OCA\User_LDAP\User\User;
use OCP\IConfig; use OCP\IConfig;
use OCP\IUser; use OCP\IUser;
use OCP\IUserSession;
use OCP\Notification\IManager as INotificationManager; use OCP\Notification\IManager as INotificationManager;
use OCP\Util; use OCP\Util;
@ -59,24 +60,21 @@ class User_LDAP extends BackendUtility implements \OCP\IUserBackend, \OCP\UserIn
* @param Access $access * @param Access $access
* @param \OCP\IConfig $ocConfig * @param \OCP\IConfig $ocConfig
* @param \OCP\Notification\IManager $notificationManager * @param \OCP\Notification\IManager $notificationManager
* @param IUserSession $userSession
*/ */
public function __construct(Access $access, IConfig $ocConfig, INotificationManager $notificationManager) { public function __construct(Access $access, IConfig $ocConfig, INotificationManager $notificationManager, IUserSession $userSession) {
parent::__construct($access); parent::__construct($access);
$this->ocConfig = $ocConfig; $this->ocConfig = $ocConfig;
$this->notificationManager = $notificationManager; $this->notificationManager = $notificationManager;
$this->registerHooks(); $this->registerHooks($userSession);
} }
protected function registerHooks() { protected function registerHooks(IUserSession $userSession) {
Util::connectHook('OC_User','pre_deleteUser', $this, 'preDeleteUser'); $userSession->listen('\OC\User', 'preDelete', [$this, 'preDeleteUser']);
Util::connectHook('OC_User','post_deleteUser', $this, 'postDeleteUser'); $userSession->listen('\OC\User', 'postDelete', [$this, 'postDeleteUser']);
} }
public function preDeleteUser(array $param) { public function preDeleteUser(IUser $user) {
$user = $param[0];
if(!$user instanceof IUser) {
throw new \RuntimeException('IUser expected');
}
$this->currentUserInDeletionProcess = $user->getUID(); $this->currentUserInDeletionProcess = $user->getUID();
} }
@ -376,8 +374,6 @@ class User_LDAP extends BackendUtility implements \OCP\IUserBackend, \OCP\UserIn
\OC::$server->getLogger()->info('Cleaning up after user ' . $uid, \OC::$server->getLogger()->info('Cleaning up after user ' . $uid,
array('app' => 'user_ldap')); array('app' => 'user_ldap'));
//Get Home Directory out of user preferences so we can return it later,
//necessary for removing directories as done by OC_User.
$this->access->getUserMapper()->unmap($uid); $this->access->getUserMapper()->unmap($uid);
$this->access->userManager->invalidate($uid); $this->access->userManager->invalidate($uid);
return true; return true;
@ -406,7 +402,9 @@ class User_LDAP extends BackendUtility implements \OCP\IUserBackend, \OCP\UserIn
// early return path if it is a deleted user // early return path if it is a deleted user
$user = $this->access->userManager->get($uid); $user = $this->access->userManager->get($uid);
if($user instanceof OfflineUser) { if($user instanceof OfflineUser) {
if($this->currentUserInDeletionProcess === $user->getUID()) { if($this->currentUserInDeletionProcess !== null
&& $this->currentUserInDeletionProcess === $user->getOCName()
) {
return $user->getHomePath(); return $user->getHomePath();
} else { } else {
throw new NoUserException($uid . ' is not a valid user anymore'); throw new NoUserException($uid . ' is not a valid user anymore');

View File

@ -31,6 +31,7 @@ namespace OCA\User_LDAP;
use OCA\User_LDAP\User\User; use OCA\User_LDAP\User\User;
use OCP\IConfig; use OCP\IConfig;
use OCP\IUserSession;
use OCP\Notification\IManager as INotificationManager; use OCP\Notification\IManager as INotificationManager;
class User_Proxy extends Proxy implements \OCP\IUserBackend, \OCP\UserInterface, IUserLDAP { class User_Proxy extends Proxy implements \OCP\IUserBackend, \OCP\UserInterface, IUserLDAP {
@ -39,14 +40,19 @@ class User_Proxy extends Proxy implements \OCP\IUserBackend, \OCP\UserInterface,
/** /**
* Constructor * Constructor
*
* @param array $serverConfigPrefixes array containing the config Prefixes * @param array $serverConfigPrefixes array containing the config Prefixes
* @param ILDAPWrapper $ldap
* @param IConfig $ocConfig
* @param INotificationManager $notificationManager
* @param IUserSession $userSession
*/ */
public function __construct(array $serverConfigPrefixes, ILDAPWrapper $ldap, IConfig $ocConfig, public function __construct(array $serverConfigPrefixes, ILDAPWrapper $ldap, IConfig $ocConfig,
INotificationManager $notificationManager) { INotificationManager $notificationManager, IUserSession $userSession) {
parent::__construct($ldap); parent::__construct($ldap);
foreach($serverConfigPrefixes as $configPrefix) { foreach($serverConfigPrefixes as $configPrefix) {
$this->backends[$configPrefix] = $this->backends[$configPrefix] =
new User_LDAP($this->getAccess($configPrefix), $ocConfig, $notificationManager); new User_LDAP($this->getAccess($configPrefix), $ocConfig, $notificationManager, $userSession);
if(is_null($this->refBackend)) { if(is_null($this->refBackend)) {
$this->refBackend = &$this->backends[$configPrefix]; $this->refBackend = &$this->backends[$configPrefix];
} }

View File

@ -144,11 +144,17 @@ abstract class AbstractIntegrationTest {
foreach($methods as $method) { foreach($methods as $method) {
if(strpos($method, 'case') === 0) { if(strpos($method, 'case') === 0) {
print("running $method " . PHP_EOL); print("running $method " . PHP_EOL);
if(!$this->$method()) { try {
print(PHP_EOL . '>>> !!! Test ' . $method . ' FAILED !!! <<<' . PHP_EOL . PHP_EOL); if(!$this->$method()) {
print(PHP_EOL . '>>> !!! Test ' . $method . ' FAILED !!! <<<' . PHP_EOL . PHP_EOL);
exit(1);
}
$atLeastOneCaseRan = true;
} catch(\Exception $e) {
print(PHP_EOL . '>>> !!! Test ' . $method . ' RAISED AN EXCEPTION !!! <<<' . PHP_EOL);
print($e->getMessage() . PHP_EOL . PHP_EOL);
exit(1); exit(1);
} }
$atLeastOneCaseRan = true;
} }
} }
if($atLeastOneCaseRan) { if($atLeastOneCaseRan) {

View File

@ -49,7 +49,7 @@ class IntegrationTestAttributeDetection extends AbstractIntegrationTest {
$groupMapper->clear(); $groupMapper->clear();
$this->access->setGroupMapper($groupMapper); $this->access->setGroupMapper($groupMapper);
$userBackend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager()); $userBackend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager(), \OC::$server->getUserSession());
$userManager = \OC::$server->getUserManager(); $userManager = \OC::$server->getUserManager();
$userManager->clearBackends(); $userManager->clearBackends();
$userManager->registerBackend($userBackend); $userManager->registerBackend($userBackend);

View File

@ -47,7 +47,7 @@ class IntegrationTestFetchUsersByLoginName extends AbstractIntegrationTest {
$this->mapping = new UserMapping(\OC::$server->getDatabaseConnection()); $this->mapping = new UserMapping(\OC::$server->getDatabaseConnection());
$this->mapping->clear(); $this->mapping->clear();
$this->access->setUserMapper($this->mapping); $this->access->setUserMapper($this->mapping);
$this->backend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager()); $this->backend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager(), \OC::$server->getUserSession());
} }
/** /**

View File

@ -47,7 +47,7 @@ class IntegrationTestPaging extends AbstractIntegrationTest {
require(__DIR__ . '/../setup-scripts/createExplicitUsers.php'); require(__DIR__ . '/../setup-scripts/createExplicitUsers.php');
parent::init(); parent::init();
$this->backend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager()); $this->backend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager(), \OC::$server->getUserSession());
} }
public function initConnection() { public function initConnection() {

View File

@ -51,7 +51,7 @@ class IntegrationTestUserHome extends AbstractIntegrationTest {
$this->mapping = new UserMapping(\OC::$server->getDatabaseConnection()); $this->mapping = new UserMapping(\OC::$server->getDatabaseConnection());
$this->mapping->clear(); $this->mapping->clear();
$this->access->setUserMapper($this->mapping); $this->access->setUserMapper($this->mapping);
$this->backend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager()); $this->backend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager(), \OC::$server->getUserSession());
} }
/** /**

View File

@ -50,7 +50,7 @@ class IntegrationTestUserAvatar extends AbstractIntegrationTest {
$this->mapping = new UserMapping(\OC::$server->getDatabaseConnection()); $this->mapping = new UserMapping(\OC::$server->getDatabaseConnection());
$this->mapping->clear(); $this->mapping->clear();
$this->access->setUserMapper($this->mapping); $this->access->setUserMapper($this->mapping);
$userBackend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager()); $userBackend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager(), \OC::$server->getUserSession());
\OC_User::useBackend($userBackend); \OC_User::useBackend($userBackend);
} }

View File

@ -46,7 +46,7 @@ class IntegrationTestUserCleanUp extends AbstractIntegrationTest {
$this->mapping->clear(); $this->mapping->clear();
$this->access->setUserMapper($this->mapping); $this->access->setUserMapper($this->mapping);
$userBackend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager()); $userBackend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager(), \OC::$server->getUserSession());
\OC_User::useBackend($userBackend); \OC_User::useBackend($userBackend);
} }
@ -76,16 +76,18 @@ class IntegrationTestUserCleanUp extends AbstractIntegrationTest {
$dn = 'uid=alice,ou=Users,' . $this->base; $dn = 'uid=alice,ou=Users,' . $this->base;
$this->prepareUser($dn, $username); $this->prepareUser($dn, $username);
$user = \OC::$server->getUserManager()->get($username);
if($user === null) {
return false;
}
$this->deleteUserFromLDAP($dn); $this->deleteUserFromLDAP($dn);
$job = new CleanUp(); $job = new CleanUp();
$job->run([]); $job->run([]);
// user instance must not be requested from global user manager, before
// it is deleted from the LDAP server. The instance will be returned
// from cache and may false-positively confirm the correctness.
$user = \OC::$server->getUserManager()->get($username);
if($user === null) {
return false;
}
$user->delete(); $user->delete();
return null === \OC::$server->getUserManager()->get($username); return null === \OC::$server->getUserManager()->get($username);

View File

@ -43,7 +43,7 @@ class IntegrationTestUserDisplayName extends AbstractIntegrationTest {
$this->mapping = new UserMapping(\OC::$server->getDatabaseConnection()); $this->mapping = new UserMapping(\OC::$server->getDatabaseConnection());
$this->mapping->clear(); $this->mapping->clear();
$this->access->setUserMapper($this->mapping); $this->access->setUserMapper($this->mapping);
$userBackend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager()); $userBackend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager(), \OC::$server->getUserSession());
\OC_User::useBackend($userBackend); \OC_User::useBackend($userBackend);
} }

View File

@ -29,6 +29,7 @@
namespace OCA\User_LDAP\Tests; namespace OCA\User_LDAP\Tests;
use OC\User\Session;
use OCA\User_LDAP\Access; use OCA\User_LDAP\Access;
use OCA\User_LDAP\Connection; use OCA\User_LDAP\Connection;
use OCA\User_LDAP\FilesystemHelper; use OCA\User_LDAP\FilesystemHelper;
@ -46,6 +47,7 @@ use OCP\IAvatarManager;
use OCP\IConfig; use OCP\IConfig;
use OCP\IDBConnection; use OCP\IDBConnection;
use OCP\Image; use OCP\Image;
use OCP\IUser;
use OCP\IUserManager; use OCP\IUserManager;
use Test\TestCase; use Test\TestCase;
use OCP\Notification\IManager as INotificationManager; use OCP\Notification\IManager as INotificationManager;
@ -205,7 +207,7 @@ class User_LDAPTest extends TestCase {
$access = $this->getAccessMock(); $access = $this->getAccessMock();
$this->prepareAccessForCheckPassword($access); $this->prepareAccessForCheckPassword($access);
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class));
\OC_User::useBackend($backend); \OC_User::useBackend($backend);
$result = $backend->checkPassword('roland', 'dt19'); $result = $backend->checkPassword('roland', 'dt19');
@ -216,7 +218,7 @@ class User_LDAPTest extends TestCase {
$access = $this->getAccessMock(); $access = $this->getAccessMock();
$this->prepareAccessForCheckPassword($access); $this->prepareAccessForCheckPassword($access);
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class));
\OC_User::useBackend($backend); \OC_User::useBackend($backend);
$result = $backend->checkPassword('roland', 'wrong'); $result = $backend->checkPassword('roland', 'wrong');
@ -227,7 +229,7 @@ class User_LDAPTest extends TestCase {
$access = $this->getAccessMock(); $access = $this->getAccessMock();
$this->prepareAccessForCheckPassword($access); $this->prepareAccessForCheckPassword($access);
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class));
\OC_User::useBackend($backend); \OC_User::useBackend($backend);
$result = $backend->checkPassword('mallory', 'evil'); $result = $backend->checkPassword('mallory', 'evil');
@ -242,7 +244,7 @@ class User_LDAPTest extends TestCase {
->method('username2dn') ->method('username2dn')
->will($this->returnValue(false)); ->will($this->returnValue(false));
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class));
\OC_User::useBackend($backend); \OC_User::useBackend($backend);
$result = $backend->checkPassword('roland', 'dt19'); $result = $backend->checkPassword('roland', 'dt19');
@ -252,7 +254,7 @@ class User_LDAPTest extends TestCase {
public function testCheckPasswordPublicAPI() { public function testCheckPasswordPublicAPI() {
$access = $this->getAccessMock(); $access = $this->getAccessMock();
$this->prepareAccessForCheckPassword($access); $this->prepareAccessForCheckPassword($access);
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class));
\OC_User::useBackend($backend); \OC_User::useBackend($backend);
$result = \OCP\User::checkPassword('roland', 'dt19'); $result = \OCP\User::checkPassword('roland', 'dt19');
@ -262,7 +264,7 @@ class User_LDAPTest extends TestCase {
public function testCheckPasswordPublicAPIWrongPassword() { public function testCheckPasswordPublicAPIWrongPassword() {
$access = $this->getAccessMock(); $access = $this->getAccessMock();
$this->prepareAccessForCheckPassword($access); $this->prepareAccessForCheckPassword($access);
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class));
\OC_User::useBackend($backend); \OC_User::useBackend($backend);
$result = \OCP\User::checkPassword('roland', 'wrong'); $result = \OCP\User::checkPassword('roland', 'wrong');
@ -272,7 +274,7 @@ class User_LDAPTest extends TestCase {
public function testCheckPasswordPublicAPIWrongUser() { public function testCheckPasswordPublicAPIWrongUser() {
$access = $this->getAccessMock(); $access = $this->getAccessMock();
$this->prepareAccessForCheckPassword($access); $this->prepareAccessForCheckPassword($access);
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class));
\OC_User::useBackend($backend); \OC_User::useBackend($backend);
$result = \OCP\User::checkPassword('mallory', 'evil'); $result = \OCP\User::checkPassword('mallory', 'evil');
@ -281,7 +283,7 @@ class User_LDAPTest extends TestCase {
public function testDeleteUserCancel() { public function testDeleteUserCancel() {
$access = $this->getAccessMock(); $access = $this->getAccessMock();
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class));
$result = $backend->deleteUser('notme'); $result = $backend->deleteUser('notme');
$this->assertFalse($result); $this->assertFalse($result);
} }
@ -307,9 +309,18 @@ class User_LDAPTest extends TestCase {
$this->offlineUser->expects($this->once()) $this->offlineUser->expects($this->once())
->method('getHomePath') ->method('getHomePath')
->willReturn($home); ->willReturn($home);
$this->offlineUser->expects($this->once())
->method('getOCName')
->willReturn($uid);
$backend = new UserLDAP($access, $this->configMock, $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->configMock, $this->createMock(INotificationManager::class), $this->createMock(Session::class));
$user = $this->createMock(IUser::class);
$user->expects($this->once())
->method('getUID')
->willReturn($uid);
$backend->preDeleteUser($user);
$result = $backend->deleteUser($uid); $result = $backend->deleteUser($uid);
$this->assertTrue($result); $this->assertTrue($result);
$this->assertSame($backend->getHome($uid), $home); $this->assertSame($backend->getHome($uid), $home);
@ -370,7 +381,7 @@ class User_LDAPTest extends TestCase {
public function testGetUsersNoParam() { public function testGetUsersNoParam() {
$access = $this->getAccessMock(); $access = $this->getAccessMock();
$this->prepareAccessForGetUsers($access); $this->prepareAccessForGetUsers($access);
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class));
$result = $backend->getUsers(); $result = $backend->getUsers();
$this->assertEquals(3, count($result)); $this->assertEquals(3, count($result));
@ -379,7 +390,7 @@ class User_LDAPTest extends TestCase {
public function testGetUsersLimitOffset() { public function testGetUsersLimitOffset() {
$access = $this->getAccessMock(); $access = $this->getAccessMock();
$this->prepareAccessForGetUsers($access); $this->prepareAccessForGetUsers($access);
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class));
$result = $backend->getUsers('', 1, 2); $result = $backend->getUsers('', 1, 2);
$this->assertEquals(1, count($result)); $this->assertEquals(1, count($result));
@ -388,7 +399,7 @@ class User_LDAPTest extends TestCase {
public function testGetUsersLimitOffset2() { public function testGetUsersLimitOffset2() {
$access = $this->getAccessMock(); $access = $this->getAccessMock();
$this->prepareAccessForGetUsers($access); $this->prepareAccessForGetUsers($access);
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class));
$result = $backend->getUsers('', 2, 1); $result = $backend->getUsers('', 2, 1);
$this->assertEquals(2, count($result)); $this->assertEquals(2, count($result));
@ -397,7 +408,7 @@ class User_LDAPTest extends TestCase {
public function testGetUsersSearchWithResult() { public function testGetUsersSearchWithResult() {
$access = $this->getAccessMock(); $access = $this->getAccessMock();
$this->prepareAccessForGetUsers($access); $this->prepareAccessForGetUsers($access);
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class));
$result = $backend->getUsers('yo'); $result = $backend->getUsers('yo');
$this->assertEquals(2, count($result)); $this->assertEquals(2, count($result));
@ -406,7 +417,7 @@ class User_LDAPTest extends TestCase {
public function testGetUsersSearchEmptyResult() { public function testGetUsersSearchEmptyResult() {
$access = $this->getAccessMock(); $access = $this->getAccessMock();
$this->prepareAccessForGetUsers($access); $this->prepareAccessForGetUsers($access);
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class));
$result = $backend->getUsers('nix'); $result = $backend->getUsers('nix');
$this->assertEquals(0, count($result)); $this->assertEquals(0, count($result));
@ -415,7 +426,7 @@ class User_LDAPTest extends TestCase {
public function testGetUsersViaAPINoParam() { public function testGetUsersViaAPINoParam() {
$access = $this->getAccessMock(); $access = $this->getAccessMock();
$this->prepareAccessForGetUsers($access); $this->prepareAccessForGetUsers($access);
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class));
\OC_User::useBackend($backend); \OC_User::useBackend($backend);
$result = \OCP\User::getUsers(); $result = \OCP\User::getUsers();
@ -425,7 +436,7 @@ class User_LDAPTest extends TestCase {
public function testGetUsersViaAPILimitOffset() { public function testGetUsersViaAPILimitOffset() {
$access = $this->getAccessMock(); $access = $this->getAccessMock();
$this->prepareAccessForGetUsers($access); $this->prepareAccessForGetUsers($access);
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class));
\OC_User::useBackend($backend); \OC_User::useBackend($backend);
$result = \OCP\User::getUsers('', 1, 2); $result = \OCP\User::getUsers('', 1, 2);
@ -435,7 +446,7 @@ class User_LDAPTest extends TestCase {
public function testGetUsersViaAPILimitOffset2() { public function testGetUsersViaAPILimitOffset2() {
$access = $this->getAccessMock(); $access = $this->getAccessMock();
$this->prepareAccessForGetUsers($access); $this->prepareAccessForGetUsers($access);
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class));
\OC_User::useBackend($backend); \OC_User::useBackend($backend);
$result = \OCP\User::getUsers('', 2, 1); $result = \OCP\User::getUsers('', 2, 1);
@ -445,7 +456,7 @@ class User_LDAPTest extends TestCase {
public function testGetUsersViaAPISearchWithResult() { public function testGetUsersViaAPISearchWithResult() {
$access = $this->getAccessMock(); $access = $this->getAccessMock();
$this->prepareAccessForGetUsers($access); $this->prepareAccessForGetUsers($access);
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class));
\OC_User::useBackend($backend); \OC_User::useBackend($backend);
$result = \OCP\User::getUsers('yo'); $result = \OCP\User::getUsers('yo');
@ -455,7 +466,7 @@ class User_LDAPTest extends TestCase {
public function testGetUsersViaAPISearchEmptyResult() { public function testGetUsersViaAPISearchEmptyResult() {
$access = $this->getAccessMock(); $access = $this->getAccessMock();
$this->prepareAccessForGetUsers($access); $this->prepareAccessForGetUsers($access);
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class));
\OC_User::useBackend($backend); \OC_User::useBackend($backend);
$result = \OCP\User::getUsers('nix'); $result = \OCP\User::getUsers('nix');
@ -464,7 +475,7 @@ class User_LDAPTest extends TestCase {
public function testUserExists() { public function testUserExists() {
$access = $this->getAccessMock(); $access = $this->getAccessMock();
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class));
$this->prepareMockForUserExists($access); $this->prepareMockForUserExists($access);
$access->expects($this->any()) $access->expects($this->any())
@ -486,7 +497,7 @@ class User_LDAPTest extends TestCase {
*/ */
public function testUserExistsForDeleted() { public function testUserExistsForDeleted() {
$access = $this->getAccessMock(); $access = $this->getAccessMock();
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class));
$this->prepareMockForUserExists($access); $this->prepareMockForUserExists($access);
$access->expects($this->any()) $access->expects($this->any())
@ -504,7 +515,7 @@ class User_LDAPTest extends TestCase {
public function testUserExistsForNeverExisting() { public function testUserExistsForNeverExisting() {
$access = $this->getAccessMock(); $access = $this->getAccessMock();
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class));
$this->prepareMockForUserExists($access); $this->prepareMockForUserExists($access);
$access->expects($this->any()) $access->expects($this->any())
@ -523,7 +534,7 @@ class User_LDAPTest extends TestCase {
public function testUserExistsPublicAPI() { public function testUserExistsPublicAPI() {
$access = $this->getAccessMock(); $access = $this->getAccessMock();
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class));
$this->prepareMockForUserExists($access); $this->prepareMockForUserExists($access);
\OC_User::useBackend($backend); \OC_User::useBackend($backend);
@ -546,7 +557,7 @@ class User_LDAPTest extends TestCase {
*/ */
public function testUserExistsPublicAPIForDeleted() { public function testUserExistsPublicAPIForDeleted() {
$access = $this->getAccessMock(); $access = $this->getAccessMock();
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class));
$this->prepareMockForUserExists($access); $this->prepareMockForUserExists($access);
\OC_User::useBackend($backend); \OC_User::useBackend($backend);
@ -565,7 +576,7 @@ class User_LDAPTest extends TestCase {
public function testUserExistsPublicAPIForNeverExisting() { public function testUserExistsPublicAPIForNeverExisting() {
$access = $this->getAccessMock(); $access = $this->getAccessMock();
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class));
$this->prepareMockForUserExists($access); $this->prepareMockForUserExists($access);
\OC_User::useBackend($backend); \OC_User::useBackend($backend);
@ -585,7 +596,7 @@ class User_LDAPTest extends TestCase {
public function testDeleteUserExisting() { public function testDeleteUserExisting() {
$access = $this->getAccessMock(); $access = $this->getAccessMock();
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class));
//we do not support deleting existing users at all //we do not support deleting existing users at all
$result = $backend->deleteUser('gunslinger'); $result = $backend->deleteUser('gunslinger');
@ -596,7 +607,7 @@ class User_LDAPTest extends TestCase {
$access = $this->getAccessMock(); $access = $this->getAccessMock();
$config = $this->createMock(IConfig::class); $config = $this->createMock(IConfig::class);
$noti = $this->createMock(INotificationManager::class); $noti = $this->createMock(INotificationManager::class);
$backend = new UserLDAP($access, $config, $noti); $backend = new UserLDAP($access, $config, $noti, $this->createMock(Session::class));
$this->prepareMockForUserExists($access); $this->prepareMockForUserExists($access);
$access->connection->expects($this->any()) $access->connection->expects($this->any())
@ -632,7 +643,7 @@ class User_LDAPTest extends TestCase {
$access = $this->getAccessMock(); $access = $this->getAccessMock();
$config = $this->createMock(IConfig::class); $config = $this->createMock(IConfig::class);
$noti = $this->createMock(INotificationManager::class); $noti = $this->createMock(INotificationManager::class);
$backend = new UserLDAP($access, $config, $noti); $backend = new UserLDAP($access, $config, $noti, $this->createMock(Session::class));
$this->prepareMockForUserExists($access); $this->prepareMockForUserExists($access);
$dataDir = \OC::$server->getConfig()->getSystemValue( $dataDir = \OC::$server->getConfig()->getSystemValue(
@ -675,7 +686,7 @@ class User_LDAPTest extends TestCase {
*/ */
public function testGetHomeNoPath() { public function testGetHomeNoPath() {
$access = $this->getAccessMock(); $access = $this->getAccessMock();
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class));
$this->prepareMockForUserExists($access); $this->prepareMockForUserExists($access);
$access->connection->expects($this->any()) $access->connection->expects($this->any())
@ -708,7 +719,7 @@ class User_LDAPTest extends TestCase {
$uid = 'newyorker'; $uid = 'newyorker';
$access = $this->getAccessMock(); $access = $this->getAccessMock();
$backend = new UserLDAP($access, $this->configMock, $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->configMock, $this->createMock(INotificationManager::class), $this->createMock(Session::class));
$this->prepareMockForUserExists($access); $this->prepareMockForUserExists($access);
$access->connection->expects($this->any()) $access->connection->expects($this->any())
@ -736,9 +747,6 @@ class User_LDAPTest extends TestCase {
$this->offlineUser->expects($this->never()) $this->offlineUser->expects($this->never())
->method('getHomePath'); ->method('getHomePath');
$this->offlineUser->expects($this->once())
->method('getUID')
->willReturn($uid);
$backend->getHome($uid); $backend->getHome($uid);
} }
@ -784,7 +792,7 @@ class User_LDAPTest extends TestCase {
public function testGetDisplayName() { public function testGetDisplayName() {
$access = $this->getAccessMock(); $access = $this->getAccessMock();
$this->prepareAccessForGetDisplayName($access); $this->prepareAccessForGetDisplayName($access);
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class));
$this->prepareMockForUserExists($access); $this->prepareMockForUserExists($access);
$access->connection->expects($this->any()) $access->connection->expects($this->any())
@ -825,7 +833,7 @@ class User_LDAPTest extends TestCase {
} }
})); }));
$this->prepareAccessForGetDisplayName($access); $this->prepareAccessForGetDisplayName($access);
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class));
$this->prepareMockForUserExists($access); $this->prepareMockForUserExists($access);
$access->connection->expects($this->any()) $access->connection->expects($this->any())
@ -855,7 +863,7 @@ class User_LDAPTest extends TestCase {
->method('countUsers') ->method('countUsers')
->will($this->returnValue(5)); ->will($this->returnValue(5));
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class));
$result = $backend->countUsers(); $result = $backend->countUsers();
$this->assertEquals(5, $result); $this->assertEquals(5, $result);
@ -868,7 +876,7 @@ class User_LDAPTest extends TestCase {
->method('countUsers') ->method('countUsers')
->will($this->returnValue(false)); ->will($this->returnValue(false));
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class));
$result = $backend->countUsers(); $result = $backend->countUsers();
$this->assertFalse($result); $this->assertFalse($result);
@ -901,7 +909,7 @@ class User_LDAPTest extends TestCase {
->method('writeToCache') ->method('writeToCache')
->with($this->equalTo('loginName2UserName-'.$loginName), $this->equalTo($username)); ->with($this->equalTo('loginName2UserName-'.$loginName), $this->equalTo($username));
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class));
$name = $backend->loginName2UserName($loginName); $name = $backend->loginName2UserName($loginName);
$this->assertSame($username, $name); $this->assertSame($username, $name);
@ -930,7 +938,7 @@ class User_LDAPTest extends TestCase {
->method('writeToCache') ->method('writeToCache')
->with($this->equalTo('loginName2UserName-'.$loginName), false); ->with($this->equalTo('loginName2UserName-'.$loginName), false);
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class));
$name = $backend->loginName2UserName($loginName); $name = $backend->loginName2UserName($loginName);
$this->assertSame(false, $name); $this->assertSame(false, $name);
@ -977,7 +985,7 @@ class User_LDAPTest extends TestCase {
->method('getUserValue') ->method('getUserValue')
->willReturn(1); ->willReturn(1);
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class));
$name = $backend->loginName2UserName($loginName); $name = $backend->loginName2UserName($loginName);
$this->assertSame(false, $name); $this->assertSame(false, $name);
@ -1058,7 +1066,7 @@ class User_LDAPTest extends TestCase {
$access = $this->getAccessMock(); $access = $this->getAccessMock();
$this->prepareAccessForSetPassword($access); $this->prepareAccessForSetPassword($access);
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class));
\OC_User::useBackend($backend); \OC_User::useBackend($backend);
$this->assertTrue(\OC_User::setPassword('roland', 'dt')); $this->assertTrue(\OC_User::setPassword('roland', 'dt'));
@ -1068,7 +1076,7 @@ class User_LDAPTest extends TestCase {
$access = $this->getAccessMock(); $access = $this->getAccessMock();
$this->prepareAccessForSetPassword($access); $this->prepareAccessForSetPassword($access);
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class));
\OC_User::useBackend($backend); \OC_User::useBackend($backend);
$this->assertTrue(\OC_User::setPassword('roland', 'dt12234$')); $this->assertTrue(\OC_User::setPassword('roland', 'dt12234$'));
@ -1078,7 +1086,7 @@ class User_LDAPTest extends TestCase {
$access = $this->getAccessMock(); $access = $this->getAccessMock();
$this->prepareAccessForSetPassword($access, false); $this->prepareAccessForSetPassword($access, false);
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class)); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class));
\OC_User::useBackend($backend); \OC_User::useBackend($backend);
$this->assertFalse(\OC_User::setPassword('roland', 'dt12234$')); $this->assertFalse(\OC_User::setPassword('roland', 'dt12234$'));
@ -1098,10 +1106,12 @@ class User_LDAPTest extends TestCase {
->willReturn(null); ->willReturn(null);
$config = $this->createMock(IConfig::class); $config = $this->createMock(IConfig::class);
$noti = $this->createMock(INotificationManager::class); $noti = $this->createMock(INotificationManager::class);
$userSession = $this->createMock(Session::class);
$ldap = new User_LDAP( $ldap = new User_LDAP(
$access, $access,
$config, $config,
$noti $noti,
$userSession
); );
$ldap->setPassword('NotExistingUser', 'Password'); $ldap->setPassword('NotExistingUser', 'Password');
} }
@ -1121,10 +1131,12 @@ class User_LDAPTest extends TestCase {
->willReturn($user); ->willReturn($user);
$config = $this->createMock(IConfig::class); $config = $this->createMock(IConfig::class);
$noti = $this->createMock(INotificationManager::class); $noti = $this->createMock(INotificationManager::class);
$userSession = $this->createMock(Session::class);
$ldap = new User_LDAP( $ldap = new User_LDAP(
$access, $access,
$config, $config,
$noti $noti,
$userSession
); );
$this->assertFalse($ldap->setPassword('NotExistingUser', 'Password')); $this->assertFalse($ldap->setPassword('NotExistingUser', 'Password'));
} }

View File

@ -24,6 +24,7 @@ namespace OCA\User_LDAP\Tests;
use OCA\User_LDAP\ILDAPWrapper; use OCA\User_LDAP\ILDAPWrapper;
use OCA\User_LDAP\User_Proxy; use OCA\User_LDAP\User_Proxy;
use OCP\IConfig; use OCP\IConfig;
use OCP\IUserSession;
use OCP\Notification\IManager as INotificationManager; use OCP\Notification\IManager as INotificationManager;
use Test\TestCase; use Test\TestCase;
@ -32,8 +33,10 @@ class User_ProxyTest extends TestCase {
private $ldapWrapper; private $ldapWrapper;
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
private $config; private $config;
/** @var IManager|\PHPUnit_Framework_MockObject_MockObject */ /** @var INotificationManager|\PHPUnit_Framework_MockObject_MockObject */
private $notificationManager; private $notificationManager;
/** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
private $userSession;
/** @var User_Proxy|\PHPUnit_Framework_MockObject_MockObject */ /** @var User_Proxy|\PHPUnit_Framework_MockObject_MockObject */
private $proxy; private $proxy;
@ -43,12 +46,14 @@ class User_ProxyTest extends TestCase {
$this->ldapWrapper = $this->createMock(ILDAPWrapper::class); $this->ldapWrapper = $this->createMock(ILDAPWrapper::class);
$this->config = $this->createMock(IConfig::class); $this->config = $this->createMock(IConfig::class);
$this->notificationManager = $this->createMock(INotificationManager::class); $this->notificationManager = $this->createMock(INotificationManager::class);
$this->userSession = $this->createMock(IUserSession::class);
$this->proxy = $this->getMockBuilder(User_Proxy::class) $this->proxy = $this->getMockBuilder(User_Proxy::class)
->setConstructorArgs([ ->setConstructorArgs([
[], [],
$this->ldapWrapper, $this->ldapWrapper,
$this->config, $this->config,
$this->notificationManager, $this->notificationManager,
$this->userSession,
]) ])
->setMethods(['handleRequest']) ->setMethods(['handleRequest'])
->getMock(); ->getMock();