diff --git a/apps/user_ldap/appinfo/app.php b/apps/user_ldap/appinfo/app.php index 70e505c18af80..61b79fb516d3b 100644 --- a/apps/user_ldap/appinfo/app.php +++ b/apps/user_ldap/appinfo/app.php @@ -44,9 +44,10 @@ 'name' => $l->t('LDAP user and group backend'), ]; }); + $userSession = \OC::$server->getUserSession(); $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); // register user backend diff --git a/apps/user_ldap/appinfo/register_command.php b/apps/user_ldap/appinfo/register_command.php index 3ae8bdbfc131e..bd706e61f2275 100644 --- a/apps/user_ldap/appinfo/register_command.php +++ b/apps/user_ldap/appinfo/register_command.php @@ -36,7 +36,8 @@ $helper->getServerConfigurationPrefixes(true), new LDAP(), $ocConfig, - \OC::$server->getNotificationManager() + \OC::$server->getNotificationManager(), + \OC::$server->getUserSession() ); $deletedUsersIndex = new DeletedUsersIndex( $ocConfig, $dbConnection, $userMapping diff --git a/apps/user_ldap/lib/Command/Search.php b/apps/user_ldap/lib/Command/Search.php index 463ad2eaeb445..d348d5b31c94c 100644 --- a/apps/user_ldap/lib/Command/Search.php +++ b/apps/user_ldap/lib/Command/Search.php @@ -120,7 +120,13 @@ protected function execute(InputInterface $input, OutputInterface $output) { $limit = null; } } 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'; $printID = true; } diff --git a/apps/user_ldap/lib/Helper.php b/apps/user_ldap/lib/Helper.php index 83b2f05f1d25d..891ab7f0a3ae5 100644 --- a/apps/user_ldap/lib/Helper.php +++ b/apps/user_ldap/lib/Helper.php @@ -294,9 +294,10 @@ public static function loginName2UserName($param) { $ldapWrapper = new LDAP(); $ocConfig = \OC::$server->getConfig(); $notificationManager = \OC::$server->getNotificationManager(); + $userSession = \OC::$server->getUserSession(); $userBackend = new User_Proxy( - $configPrefixes, $ldapWrapper, $ocConfig, $notificationManager + $configPrefixes, $ldapWrapper, $ocConfig, $notificationManager, $userSession ); $uid = $userBackend->loginName2UserName($param['uid'] ); if($uid !== false) { diff --git a/apps/user_ldap/lib/Jobs/CleanUp.php b/apps/user_ldap/lib/Jobs/CleanUp.php index a4dd4ba32ad5a..44e8f5469f160 100644 --- a/apps/user_ldap/lib/Jobs/CleanUp.php +++ b/apps/user_ldap/lib/Jobs/CleanUp.php @@ -99,7 +99,8 @@ public function setArguments($arguments) { $this->ldapHelper->getServerConfigurationPrefixes(true), new LDAP(), $this->ocConfig, - \OC::$server->getNotificationManager() + \OC::$server->getNotificationManager(), + \OC::$server->getUserSession() ); } diff --git a/apps/user_ldap/lib/Migration/UUIDFixGroup.php b/apps/user_ldap/lib/Migration/UUIDFixGroup.php index 6aacb37257edc..9ea406efadf98 100644 --- a/apps/user_ldap/lib/Migration/UUIDFixGroup.php +++ b/apps/user_ldap/lib/Migration/UUIDFixGroup.php @@ -33,6 +33,6 @@ class UUIDFixGroup extends UUIDFix { public function __construct(GroupMapping $mapper, LDAP $ldap, IConfig $config, Helper $helper) { $this->mapper = $mapper; $this->proxy = new User_Proxy($helper->getServerConfigurationPrefixes(true), $ldap, $config, - \OC::$server->getNotificationManager()); + \OC::$server->getNotificationManager(), \OC::$server->getUserSession()); } } diff --git a/apps/user_ldap/lib/User_LDAP.php b/apps/user_ldap/lib/User_LDAP.php index f677ba77b985d..dcfc5cc13b929 100644 --- a/apps/user_ldap/lib/User_LDAP.php +++ b/apps/user_ldap/lib/User_LDAP.php @@ -42,6 +42,7 @@ use OCA\User_LDAP\User\User; use OCP\IConfig; use OCP\IUser; +use OCP\IUserSession; use OCP\Notification\IManager as INotificationManager; use OCP\Util; @@ -59,24 +60,21 @@ class User_LDAP extends BackendUtility implements \OCP\IUserBackend, \OCP\UserIn * @param Access $access * @param \OCP\IConfig $ocConfig * @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); $this->ocConfig = $ocConfig; $this->notificationManager = $notificationManager; - $this->registerHooks(); + $this->registerHooks($userSession); } - protected function registerHooks() { - Util::connectHook('OC_User','pre_deleteUser', $this, 'preDeleteUser'); - Util::connectHook('OC_User','post_deleteUser', $this, 'postDeleteUser'); + protected function registerHooks(IUserSession $userSession) { + $userSession->listen('\OC\User', 'preDelete', [$this, 'preDeleteUser']); + $userSession->listen('\OC\User', 'postDelete', [$this, 'postDeleteUser']); } - public function preDeleteUser(array $param) { - $user = $param[0]; - if(!$user instanceof IUser) { - throw new \RuntimeException('IUser expected'); - } + public function preDeleteUser(IUser $user) { $this->currentUserInDeletionProcess = $user->getUID(); } @@ -376,8 +374,6 @@ public function deleteUser($uid) { \OC::$server->getLogger()->info('Cleaning up after user ' . $uid, 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->userManager->invalidate($uid); return true; @@ -406,7 +402,9 @@ public function getHome($uid) { // early return path if it is a deleted user $user = $this->access->userManager->get($uid); if($user instanceof OfflineUser) { - if($this->currentUserInDeletionProcess === $user->getUID()) { + if($this->currentUserInDeletionProcess !== null + && $this->currentUserInDeletionProcess === $user->getOCName() + ) { return $user->getHomePath(); } else { throw new NoUserException($uid . ' is not a valid user anymore'); diff --git a/apps/user_ldap/lib/User_Proxy.php b/apps/user_ldap/lib/User_Proxy.php index 8e81b10f7b31b..50bb68edc2d9d 100644 --- a/apps/user_ldap/lib/User_Proxy.php +++ b/apps/user_ldap/lib/User_Proxy.php @@ -31,6 +31,7 @@ use OCA\User_LDAP\User\User; use OCP\IConfig; +use OCP\IUserSession; use OCP\Notification\IManager as INotificationManager; 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 + * * @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, - INotificationManager $notificationManager) { + INotificationManager $notificationManager, IUserSession $userSession) { parent::__construct($ldap); foreach($serverConfigPrefixes as $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)) { $this->refBackend = &$this->backends[$configPrefix]; } diff --git a/apps/user_ldap/tests/Integration/AbstractIntegrationTest.php b/apps/user_ldap/tests/Integration/AbstractIntegrationTest.php index 9891a4f719d16..84e1e6b458afd 100644 --- a/apps/user_ldap/tests/Integration/AbstractIntegrationTest.php +++ b/apps/user_ldap/tests/Integration/AbstractIntegrationTest.php @@ -144,11 +144,17 @@ public function run() { foreach($methods as $method) { if(strpos($method, 'case') === 0) { print("running $method " . PHP_EOL); - if(!$this->$method()) { - print(PHP_EOL . '>>> !!! Test ' . $method . ' FAILED !!! <<<' . PHP_EOL . PHP_EOL); + try { + 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); } - $atLeastOneCaseRan = true; } } if($atLeastOneCaseRan) { diff --git a/apps/user_ldap/tests/Integration/Lib/IntegrationTestAttributeDetection.php b/apps/user_ldap/tests/Integration/Lib/IntegrationTestAttributeDetection.php index be35556111fe8..bd8e4bdd7a242 100644 --- a/apps/user_ldap/tests/Integration/Lib/IntegrationTestAttributeDetection.php +++ b/apps/user_ldap/tests/Integration/Lib/IntegrationTestAttributeDetection.php @@ -49,7 +49,7 @@ public function init() { $groupMapper->clear(); $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->clearBackends(); $userManager->registerBackend($userBackend); diff --git a/apps/user_ldap/tests/Integration/Lib/IntegrationTestFetchUsersByLoginName.php b/apps/user_ldap/tests/Integration/Lib/IntegrationTestFetchUsersByLoginName.php index 5dda79c2ec0c1..95bfb99b65bdc 100644 --- a/apps/user_ldap/tests/Integration/Lib/IntegrationTestFetchUsersByLoginName.php +++ b/apps/user_ldap/tests/Integration/Lib/IntegrationTestFetchUsersByLoginName.php @@ -47,7 +47,7 @@ public function init() { $this->mapping = new UserMapping(\OC::$server->getDatabaseConnection()); $this->mapping->clear(); $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()); } /** diff --git a/apps/user_ldap/tests/Integration/Lib/IntegrationTestPaging.php b/apps/user_ldap/tests/Integration/Lib/IntegrationTestPaging.php index 35d8524fd8825..3c8cf22bb5bf5 100644 --- a/apps/user_ldap/tests/Integration/Lib/IntegrationTestPaging.php +++ b/apps/user_ldap/tests/Integration/Lib/IntegrationTestPaging.php @@ -47,7 +47,7 @@ public function init() { require(__DIR__ . '/../setup-scripts/createExplicitUsers.php'); 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() { diff --git a/apps/user_ldap/tests/Integration/Lib/IntegrationTestUserHome.php b/apps/user_ldap/tests/Integration/Lib/IntegrationTestUserHome.php index 9226c56ee8956..765cee00f912d 100644 --- a/apps/user_ldap/tests/Integration/Lib/IntegrationTestUserHome.php +++ b/apps/user_ldap/tests/Integration/Lib/IntegrationTestUserHome.php @@ -51,7 +51,7 @@ public function init() { $this->mapping = new UserMapping(\OC::$server->getDatabaseConnection()); $this->mapping->clear(); $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()); } /** diff --git a/apps/user_ldap/tests/Integration/Lib/User/IntegrationTestUserAvatar.php b/apps/user_ldap/tests/Integration/Lib/User/IntegrationTestUserAvatar.php index 1710f51915882..8c9e215edaa19 100644 --- a/apps/user_ldap/tests/Integration/Lib/User/IntegrationTestUserAvatar.php +++ b/apps/user_ldap/tests/Integration/Lib/User/IntegrationTestUserAvatar.php @@ -50,7 +50,7 @@ public function init() { $this->mapping = new UserMapping(\OC::$server->getDatabaseConnection()); $this->mapping->clear(); $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); } diff --git a/apps/user_ldap/tests/Integration/Lib/User/IntegrationTestUserCleanUp.php b/apps/user_ldap/tests/Integration/Lib/User/IntegrationTestUserCleanUp.php index 7d45ee69fbc7b..ce81b9a26d6ad 100644 --- a/apps/user_ldap/tests/Integration/Lib/User/IntegrationTestUserCleanUp.php +++ b/apps/user_ldap/tests/Integration/Lib/User/IntegrationTestUserCleanUp.php @@ -46,7 +46,7 @@ public function init() { $this->mapping->clear(); $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); } @@ -76,16 +76,18 @@ protected function case1() { $dn = 'uid=alice,ou=Users,' . $this->base; $this->prepareUser($dn, $username); - $user = \OC::$server->getUserManager()->get($username); - if($user === null) { - return false; - } - $this->deleteUserFromLDAP($dn); $job = new CleanUp(); $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(); return null === \OC::$server->getUserManager()->get($username); diff --git a/apps/user_ldap/tests/Integration/Lib/User/IntegrationTestUserDisplayName.php b/apps/user_ldap/tests/Integration/Lib/User/IntegrationTestUserDisplayName.php index 886b0b46f0e1b..1f5d16567f78e 100644 --- a/apps/user_ldap/tests/Integration/Lib/User/IntegrationTestUserDisplayName.php +++ b/apps/user_ldap/tests/Integration/Lib/User/IntegrationTestUserDisplayName.php @@ -43,7 +43,7 @@ public function init() { $this->mapping = new UserMapping(\OC::$server->getDatabaseConnection()); $this->mapping->clear(); $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); } diff --git a/apps/user_ldap/tests/User_LDAPTest.php b/apps/user_ldap/tests/User_LDAPTest.php index 9f1b1d3ffc8ff..969e8c5651a6e 100644 --- a/apps/user_ldap/tests/User_LDAPTest.php +++ b/apps/user_ldap/tests/User_LDAPTest.php @@ -29,6 +29,7 @@ namespace OCA\User_LDAP\Tests; +use OC\User\Session; use OCA\User_LDAP\Access; use OCA\User_LDAP\Connection; use OCA\User_LDAP\FilesystemHelper; @@ -46,6 +47,7 @@ use OCP\IConfig; use OCP\IDBConnection; use OCP\Image; +use OCP\IUser; use OCP\IUserManager; use Test\TestCase; use OCP\Notification\IManager as INotificationManager; @@ -202,7 +204,7 @@ public function testCheckPasswordUidReturn() { $access = $this->getAccessMock(); $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); $result = $backend->checkPassword('roland', 'dt19'); @@ -213,7 +215,7 @@ public function testCheckPasswordWrongPassword() { $access = $this->getAccessMock(); $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); $result = $backend->checkPassword('roland', 'wrong'); @@ -224,7 +226,7 @@ public function testCheckPasswordWrongUser() { $access = $this->getAccessMock(); $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); $result = $backend->checkPassword('mallory', 'evil'); @@ -239,7 +241,7 @@ public function testCheckPasswordNoDisplayName() { ->method('username2dn') ->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); $result = $backend->checkPassword('roland', 'dt19'); @@ -249,7 +251,7 @@ public function testCheckPasswordNoDisplayName() { public function testCheckPasswordPublicAPI() { $access = $this->getAccessMock(); $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); $result = \OCP\User::checkPassword('roland', 'dt19'); @@ -259,7 +261,7 @@ public function testCheckPasswordPublicAPI() { public function testCheckPasswordPublicAPIWrongPassword() { $access = $this->getAccessMock(); $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); $result = \OCP\User::checkPassword('roland', 'wrong'); @@ -269,7 +271,7 @@ public function testCheckPasswordPublicAPIWrongPassword() { public function testCheckPasswordPublicAPIWrongUser() { $access = $this->getAccessMock(); $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); $result = \OCP\User::checkPassword('mallory', 'evil'); @@ -278,7 +280,7 @@ public function testCheckPasswordPublicAPIWrongUser() { public function testDeleteUserCancel() { $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'); $this->assertFalse($result); } @@ -304,9 +306,18 @@ public function testDeleteUserSuccess() { $this->offlineUser->expects($this->once()) ->method('getHomePath') ->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); $this->assertTrue($result); $this->assertSame($backend->getHome($uid), $home); @@ -364,7 +375,7 @@ private function prepareAccessForGetUsers(&$access) { public function testGetUsersNoParam() { $access = $this->getAccessMock(); $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(); $this->assertEquals(3, count($result)); @@ -373,7 +384,7 @@ public function testGetUsersNoParam() { public function testGetUsersLimitOffset() { $access = $this->getAccessMock(); $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); $this->assertEquals(1, count($result)); @@ -382,7 +393,7 @@ public function testGetUsersLimitOffset() { public function testGetUsersLimitOffset2() { $access = $this->getAccessMock(); $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); $this->assertEquals(2, count($result)); @@ -391,7 +402,7 @@ public function testGetUsersLimitOffset2() { public function testGetUsersSearchWithResult() { $access = $this->getAccessMock(); $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'); $this->assertEquals(2, count($result)); @@ -400,7 +411,7 @@ public function testGetUsersSearchWithResult() { public function testGetUsersSearchEmptyResult() { $access = $this->getAccessMock(); $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'); $this->assertEquals(0, count($result)); @@ -409,7 +420,7 @@ public function testGetUsersSearchEmptyResult() { public function testGetUsersViaAPINoParam() { $access = $this->getAccessMock(); $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); $result = \OCP\User::getUsers(); @@ -419,7 +430,7 @@ public function testGetUsersViaAPINoParam() { public function testGetUsersViaAPILimitOffset() { $access = $this->getAccessMock(); $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); $result = \OCP\User::getUsers('', 1, 2); @@ -429,7 +440,7 @@ public function testGetUsersViaAPILimitOffset() { public function testGetUsersViaAPILimitOffset2() { $access = $this->getAccessMock(); $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); $result = \OCP\User::getUsers('', 2, 1); @@ -439,7 +450,7 @@ public function testGetUsersViaAPILimitOffset2() { public function testGetUsersViaAPISearchWithResult() { $access = $this->getAccessMock(); $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); $result = \OCP\User::getUsers('yo'); @@ -449,7 +460,7 @@ public function testGetUsersViaAPISearchWithResult() { public function testGetUsersViaAPISearchEmptyResult() { $access = $this->getAccessMock(); $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); $result = \OCP\User::getUsers('nix'); @@ -458,7 +469,7 @@ public function testGetUsersViaAPISearchEmptyResult() { public function testUserExists() { $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); $access->expects($this->any()) @@ -480,7 +491,7 @@ public function testUserExists() { */ public function testUserExistsForDeleted() { $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); $access->expects($this->any()) @@ -498,7 +509,7 @@ public function testUserExistsForDeleted() { public function testUserExistsForNeverExisting() { $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); $access->expects($this->any()) @@ -517,7 +528,7 @@ public function testUserExistsForNeverExisting() { public function testUserExistsPublicAPI() { $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); \OC_User::useBackend($backend); @@ -540,7 +551,7 @@ public function testUserExistsPublicAPI() { */ public function testUserExistsPublicAPIForDeleted() { $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); \OC_User::useBackend($backend); @@ -559,7 +570,7 @@ public function testUserExistsPublicAPIForDeleted() { public function testUserExistsPublicAPIForNeverExisting() { $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); \OC_User::useBackend($backend); @@ -579,7 +590,7 @@ public function testUserExistsPublicAPIForNeverExisting() { public function testDeleteUserExisting() { $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 $result = $backend->deleteUser('gunslinger'); @@ -590,7 +601,7 @@ public function testGetHomeAbsolutePath() { $access = $this->getAccessMock(); $config = $this->createMock(IConfig::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); $access->connection->expects($this->any()) @@ -626,7 +637,7 @@ public function testGetHomeRelative() { $access = $this->getAccessMock(); $config = $this->createMock(IConfig::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); $dataDir = \OC::$server->getConfig()->getSystemValue( @@ -669,7 +680,7 @@ public function testGetHomeRelative() { */ public function testGetHomeNoPath() { $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); $access->connection->expects($this->any()) @@ -702,7 +713,7 @@ public function testGetHomeDeletedUser() { $uid = 'newyorker'; $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); $access->connection->expects($this->any()) @@ -730,9 +741,6 @@ public function testGetHomeDeletedUser() { $this->offlineUser->expects($this->never()) ->method('getHomePath'); - $this->offlineUser->expects($this->once()) - ->method('getUID') - ->willReturn($uid); $backend->getHome($uid); } @@ -775,7 +783,7 @@ private function prepareAccessForGetDisplayName(&$access) { public function testGetDisplayName() { $access = $this->getAccessMock(); $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); $access->connection->expects($this->any()) @@ -816,7 +824,7 @@ public function testGetDisplayNamePublicAPI() { } })); $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); $access->connection->expects($this->any()) @@ -846,7 +854,7 @@ public function testCountUsers() { ->method('countUsers') ->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(); $this->assertEquals(5, $result); @@ -859,7 +867,7 @@ public function testCountUsersFailing() { ->method('countUsers') ->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(); $this->assertFalse($result); @@ -892,7 +900,7 @@ public function testLoginName2UserNameSuccess() { ->method('writeToCache') ->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); $this->assertSame($username, $name); @@ -921,7 +929,7 @@ public function testLoginName2UserNameNoUsersOnLDAP() { ->method('writeToCache') ->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); $this->assertSame(false, $name); @@ -968,7 +976,7 @@ public function testLoginName2UserNameOfflineUser() { ->method('getUserValue') ->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); $this->assertSame(false, $name); @@ -1049,7 +1057,7 @@ public function testSetPasswordInvalid() { $access = $this->getAccessMock(); $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); $this->assertTrue(\OC_User::setPassword('roland', 'dt')); @@ -1059,7 +1067,7 @@ public function testSetPasswordValid() { $access = $this->getAccessMock(); $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); $this->assertTrue(\OC_User::setPassword('roland', 'dt12234$')); @@ -1069,7 +1077,7 @@ public function testSetPasswordValidDisabled() { $access = $this->getAccessMock(); $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); $this->assertFalse(\OC_User::setPassword('roland', 'dt12234$')); @@ -1089,10 +1097,12 @@ public function testSetPasswordWithInvalidUser() { ->willReturn(null); $config = $this->createMock(IConfig::class); $noti = $this->createMock(INotificationManager::class); + $userSession = $this->createMock(Session::class); $ldap = new User_LDAP( $access, $config, - $noti + $noti, + $userSession ); $ldap->setPassword('NotExistingUser', 'Password'); } @@ -1112,10 +1122,12 @@ public function testSetPasswordWithUsernameFalse() { ->willReturn($user); $config = $this->createMock(IConfig::class); $noti = $this->createMock(INotificationManager::class); + $userSession = $this->createMock(Session::class); $ldap = new User_LDAP( $access, $config, - $noti + $noti, + $userSession ); $this->assertFalse($ldap->setPassword('NotExistingUser', 'Password')); } diff --git a/apps/user_ldap/tests/User_ProxyTest.php b/apps/user_ldap/tests/User_ProxyTest.php index df021a6de3d5d..68b1e4428ca64 100644 --- a/apps/user_ldap/tests/User_ProxyTest.php +++ b/apps/user_ldap/tests/User_ProxyTest.php @@ -24,6 +24,7 @@ use OCA\User_LDAP\ILDAPWrapper; use OCA\User_LDAP\User_Proxy; use OCP\IConfig; +use OCP\IUserSession; use OCP\Notification\IManager as INotificationManager; use Test\TestCase; @@ -32,8 +33,10 @@ class User_ProxyTest extends TestCase { private $ldapWrapper; /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ private $config; - /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */ + /** @var INotificationManager|\PHPUnit_Framework_MockObject_MockObject */ private $notificationManager; + /** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */ + private $userSession; /** @var User_Proxy|\PHPUnit_Framework_MockObject_MockObject */ private $proxy; @@ -43,12 +46,14 @@ public function setUp() { $this->ldapWrapper = $this->createMock(ILDAPWrapper::class); $this->config = $this->createMock(IConfig::class); $this->notificationManager = $this->createMock(INotificationManager::class); + $this->userSession = $this->createMock(IUserSession::class); $this->proxy = $this->getMockBuilder(User_Proxy::class) ->setConstructorArgs([ [], $this->ldapWrapper, $this->config, $this->notificationManager, + $this->userSession, ]) ->setMethods(['handleRequest']) ->getMock();