[Pkg-owncloud-commits] [owncloud] 26/90: add ldap:check-user to check user existance on the fly

David Prévot taffit at moszumanska.debian.org
Fri Feb 6 21:10:44 UTC 2015


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository owncloud.

commit cb5f9d2164da7b71ea0b0e6f5a4bc02f28e0eecb
Author: Arthur Schiwon <blizzz at owncloud.com>
Date:   Wed Dec 17 13:37:53 2014 +0100

    add ldap:check-user to check user existance on the fly
---
 apps/user_ldap/appinfo/register_command.php | 12 ++++++++
 apps/user_ldap/lib/helper.php               | 16 ++++++++++
 apps/user_ldap/lib/jobs/cleanup.php         | 22 +++-----------
 apps/user_ldap/user_ldap.php                | 47 +++++++++++++++++++++++------
 apps/user_ldap/user_proxy.php               | 12 ++++++++
 5 files changed, 81 insertions(+), 28 deletions(-)

diff --git a/apps/user_ldap/appinfo/register_command.php b/apps/user_ldap/appinfo/register_command.php
index ff8871e..0c90ec0 100644
--- a/apps/user_ldap/appinfo/register_command.php
+++ b/apps/user_ldap/appinfo/register_command.php
@@ -6,8 +6,20 @@
  * See the COPYING-README file.
  */
 
+use OCA\user_ldap\lib\Helper;
+use OCA\user_ldap\lib\LDAP;
+use OCA\user_ldap\User_Proxy;
+
 $application->add(new OCA\user_ldap\Command\ShowConfig());
 $application->add(new OCA\user_ldap\Command\SetConfig());
 $application->add(new OCA\user_ldap\Command\TestConfig());
 $application->add(new OCA\user_ldap\Command\Search());
 $application->add(new OCA\user_ldap\Command\ShowRemnants());
+$helper = new OCA\user_ldap\lib\Helper();
+$uBackend = new OCA\user_ldap\User_Proxy(
+	$helper->getServerConfigurationPrefixes(true),
+	new OCA\user_ldap\lib\LDAP()
+);
+$application->add(new OCA\user_ldap\Command\CheckUser(
+	$uBackend, $helper, \OC::$server->getConfig()
+));
diff --git a/apps/user_ldap/lib/helper.php b/apps/user_ldap/lib/helper.php
index 350942f..0eae403 100644
--- a/apps/user_ldap/lib/helper.php
+++ b/apps/user_ldap/lib/helper.php
@@ -145,6 +145,22 @@ class Helper {
 	}
 
 	/**
+	 * checks whether there is one or more disabled LDAP configurations
+	 * @throws \Exception
+	 * @return bool
+	 */
+	public function haveDisabledConfigurations() {
+		$all = $this->getServerConfigurationPrefixes(false);
+		$active = $this->getServerConfigurationPrefixes(true);
+
+		if(!is_array($all) || !is_array($active)) {
+			throw new \Exception('Unexpected Return Value');
+		}
+
+		return count($all) !== count($active) || count($all) === 0;
+	}
+
+	/**
 	 * Truncate's the given mapping table
 	 *
 	 * @param string $mapping either 'user' or 'group'
diff --git a/apps/user_ldap/lib/jobs/cleanup.php b/apps/user_ldap/lib/jobs/cleanup.php
index c25dfe6..56fb296 100644
--- a/apps/user_ldap/lib/jobs/cleanup.php
+++ b/apps/user_ldap/lib/jobs/cleanup.php
@@ -131,7 +131,7 @@ class CleanUp extends \OC\BackgroundJob\TimedJob {
 	 */
 	public function isCleanUpAllowed() {
 		try {
-			if($this->haveDisabledConfigurations()) {
+			if($this->ldapHelper->haveDisabledConfigurations()) {
 				return false;
 			}
 		} catch (\Exception $e) {
@@ -153,22 +153,6 @@ class CleanUp extends \OC\BackgroundJob\TimedJob {
 	}
 
 	/**
-	 * checks whether there is one or more disabled LDAP configurations
-	 * @throws \Exception
-	 * @return bool
-	 */
-	private function haveDisabledConfigurations() {
-		$all = $this->ldapHelper->getServerConfigurationPrefixes(false);
-		$active = $this->ldapHelper->getServerConfigurationPrefixes(true);
-
-		if(!is_array($all) || !is_array($active)) {
-			throw new \Exception('Unexpected Return Value');
-		}
-
-		return count($all) !== count($active) || count($all) === 0;
-	}
-
-	/**
 	 * checks users whether they are still existing
 	 * @param array $users result from getMappedUsers()
 	 */
@@ -183,11 +167,13 @@ class CleanUp extends \OC\BackgroundJob\TimedJob {
 	 * @param string[] $user
 	 */
 	private function checkUser($user) {
-		if($this->userBackend->userExists($user['name'])) {
+		if($this->userBackend->userExistsOnLDAP($user['name'])) {
 			//still available, all good
 			return;
 		}
 
+		// TODO FIXME consolidate next line in DeletedUsersIndex
+		// (impractical now, because of class dependencies)
 		$this->ocConfig->setUserValue($user['name'], 'user_ldap', 'isDeleted', '1');
 	}
 
diff --git a/apps/user_ldap/user_ldap.php b/apps/user_ldap/user_ldap.php
index 4fa3bdc..78add72 100644
--- a/apps/user_ldap/user_ldap.php
+++ b/apps/user_ldap/user_ldap.php
@@ -146,6 +146,33 @@ class USER_LDAP extends BackendUtility implements \OCP\UserInterface {
 	}
 
 	/**
+	 * checks whether a user is still available on LDAP
+	 * @param string|OCA\User_LDAP\lib\User\User $user either the ownCloud user
+	 * name or an instance of that user
+	 * @return bool
+	 */
+	public function userExistsOnLDAP($user) {
+		if(is_string($user)) {
+			$user = $this->access->userManager->get($user);
+		}
+		if(!$user instanceof User) {
+			return false;
+		}
+
+		$dn = $user->getDN();
+		//check if user really still exists by reading its entry
+		if(!is_array($this->access->readAttribute($dn, ''))) {
+			$lcr = $this->access->connection->getConnectionResource();
+			if(is_null($lcr)) {
+				throw new \Exception('No LDAP Connection to server ' . $this->access->connection->ldapHost);
+			}
+			return false;
+		}
+
+		return true;
+	}
+
+	/**
 	 * check if a user exists
 	 * @param string $uid the username
 	 * @return boolean
@@ -166,18 +193,18 @@ class USER_LDAP extends BackendUtility implements \OCP\UserInterface {
 			//necessary for cleanup
 			return true;
 		}
-		$dn = $user->getDN();
-		//check if user really still exists by reading its entry
-		if(!is_array($this->access->readAttribute($dn, ''))) {
-			\OCP\Util::writeLog('user_ldap', 'LDAP says no user '.$dn.' on '.
-				$this->access->connection->ldapHost, \OCP\Util::DEBUG);
-			$this->access->connection->writeToCache('userExists'.$uid, false);
+
+		try {
+			$result = $this->userExistsOnLDAP($user);
+			$this->access->connection->writeToCache('userExists'.$uid, $result);
+			if($result === true) {
+				$user->update();
+			}
+			return $result;
+		} catch (\Exception $e) {
+			\OCP\Util::writeLog('user_ldap', $e->getMessage(), \OCP\Util::WARN);
 			return false;
 		}
-
-		$this->access->connection->writeToCache('userExists'.$uid, true);
-		$user->update();
-		return true;
 	}
 
 	/**
diff --git a/apps/user_ldap/user_proxy.php b/apps/user_ldap/user_proxy.php
index ae148ec..92ba66c 100644
--- a/apps/user_ldap/user_proxy.php
+++ b/apps/user_ldap/user_proxy.php
@@ -24,6 +24,7 @@
 namespace OCA\user_ldap;
 
 use OCA\user_ldap\lib\ILDAPWrapper;
+use OCA\User_LDAP\lib\User\User;
 
 class User_Proxy extends lib\Proxy implements \OCP\UserInterface {
 	private $backends = array();
@@ -145,6 +146,17 @@ class User_Proxy extends lib\Proxy implements \OCP\UserInterface {
 	}
 
 	/**
+	 * check if a user exists on LDAP
+	 * @param string|OCA\User_LDAP\lib\User\User $user either the ownCloud user
+	 * name or an instance of that user
+	 * @return boolean
+	 */
+	public function userExistsOnLDAP($user) {
+		$id = ($user instanceof User) ? $user->getUsername() : $user;
+		return $this->handleRequest($id, 'userExistsOnLDAP', array($user));
+	}
+
+	/**
 	 * Check if the password is correct
 	 * @param string $uid The username
 	 * @param string $password The password

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud.git



More information about the Pkg-owncloud-commits mailing list