From cb3af1dce294ab058162b3c4d1a0feb84465dd0b Mon Sep 17 00:00:00 2001
From: Arthur Schiwon <blizzz@owncloud.com>
Date: Fri, 24 Oct 2014 18:26:48 +0200
Subject: [PATCH] detect user display name attribute and return user count
 depending on its presence

---
 apps/user_ldap/ajax/wizard.php |  2 +-
 apps/user_ldap/js/settings.js  | 15 ++++++++---
 apps/user_ldap/lib/wizard.php  | 49 ++++++++++++++++++++++++++++++++--
 3 files changed, 59 insertions(+), 7 deletions(-)

diff --git a/apps/user_ldap/ajax/wizard.php b/apps/user_ldap/ajax/wizard.php
index ef1241b914..6d7d70c8c5 100644
--- a/apps/user_ldap/ajax/wizard.php
+++ b/apps/user_ldap/ajax/wizard.php
@@ -62,6 +62,7 @@ switch($action) {
 	case 'guessPortAndTLS':
 	case 'guessBaseDN':
 	case 'detectEmailAttribute':
+	case 'detectUserDisplayNameAttribute':
 	case 'determineGroupMemberAssoc':
 	case 'determineUserObjectClasses':
 	case 'determineGroupObjectClasses':
@@ -115,4 +116,3 @@ switch($action) {
 		//TODO: return 4xx error
 		break;
 }
-
diff --git a/apps/user_ldap/js/settings.js b/apps/user_ldap/js/settings.js
index fa40aba73b..93fd97c1e7 100644
--- a/apps/user_ldap/js/settings.js
+++ b/apps/user_ldap/js/settings.js
@@ -151,8 +151,10 @@ var LdapWizard = {
 	ajaxRequests: {},
 
 	ajax: function(param, fnOnSuccess, fnOnError, reqID) {
-		if(reqID !== undefined) {
+		if(typeof reqID !== 'undefined') {
 			if(LdapWizard.ajaxRequests.hasOwnProperty(reqID)) {
+				console.log('aborting ' + reqID);
+				console.log(param);
 				LdapWizard.ajaxRequests[reqID].abort();
 			}
 		}
@@ -167,7 +169,7 @@ var LdapWizard = {
 				}
 			}
 		);
-		if(reqID !== undefined) {
+		if(typeof reqID !== 'undefined') {
 			LdapWizard.ajaxRequests[reqID] = request;
 		}
 	},
@@ -342,7 +344,7 @@ var LdapWizard = {
 	},
 
 	_countThings: function(method, spinnerID, doneCallback) {
-		param = 'action='+method+
+		var param = 'action='+method+
 				'&ldap_serverconfig_chooser='+
 				encodeURIComponent($('#ldap_serverconfig_chooser').val());
 
@@ -371,7 +373,12 @@ var LdapWizard = {
 	},
 
 	countUsers: function(doneCallback) {
-		LdapWizard._countThings('countUsers', '#ldap_user_count', doneCallback);
+		//we make user counting depending on having a display name attribute
+		var param = 'action=detectUserDisplayNameAttribute' +
+			'&ldap_serverconfig_chooser='+
+			encodeURIComponent($('#ldap_serverconfig_chooser').val());
+
+			LdapWizard._countThings('countUsers', '#ldap_user_count', doneCallback);
 	},
 
 	detectEmailAttribute: function() {
diff --git a/apps/user_ldap/lib/wizard.php b/apps/user_ldap/lib/wizard.php
index 1d7701440e..c5db4c66cc 100644
--- a/apps/user_ldap/lib/wizard.php
+++ b/apps/user_ldap/lib/wizard.php
@@ -56,7 +56,7 @@ class Wizard extends LDAPUtility {
 			Wizard::$l = \OC::$server->getL10N('user_ldap');
 		}
 		$this->access = $access;
-		$this->result = new WizardResult;
+		$this->result = new WizardResult();
 	}
 
 	public function  __destruct() {
@@ -120,7 +120,11 @@ class Wizard extends LDAPUtility {
 	 * @throws \Exception
 	 */
 	public function countUsers() {
-		$filter = $this->configuration->ldapUserFilter;
+		$this->detectUserDisplayNameAttribute();
+		$filter = $this->access->combineFilterWithAnd(array(
+			$this->configuration->ldapUserFilter,
+			$this->configuration->ldapUserDisplayName . '=*'
+		));
 
 		$usersTotal = $this->countEntries($filter, 'users');
 		$usersTotal = ($usersTotal !== false) ? $usersTotal : 0;
@@ -151,6 +155,47 @@ class Wizard extends LDAPUtility {
 		return $this->access->countUsers($filter);
 	}
 
+	/**
+	 * detects the display name attribute. If a setting is already present that
+	 * returns at least one hit, the detection will be canceled.
+	 * @return WizardResult|bool
+	 */
+	public function detectUserDisplayNameAttribute() {
+		if(!$this->checkRequirements(array('ldapHost',
+										'ldapPort',
+										'ldapBase',
+										'ldapUserFilter',
+										))) {
+			return  false;
+		}
+
+		$attr = $this->configuration->ldapUserDisplayName;
+		if($attr !== 'displayName' && !empty($attr)) {
+			// most likely not the default value with upper case N,
+			// verify it still produces a result
+			$count = intval($this->countUsersWithAttribute($attr));
+			if($count > 0) {
+				//no change, but we sent it back to make sure the user interface
+				//is still correct, even if the ajax call was cancelled inbetween
+				$this->result->addChange('ldap_display_name', $attr);
+				return $this->result;
+			}
+		}
+
+		// first attribute that has at least one result wins
+		$displayNameAttrs = array('displayname', 'cn');
+		foreach ($displayNameAttrs as $attr) {
+			$count = intval($this->countUsersWithAttribute($attr));
+
+			if($count > 0) {
+				$this->applyFind('ldap_display_name', $attr);
+				return $this->result;
+			}
+		};
+
+		throw new \Exception(self::$t->l('Could not detect user display name attribute. Please specify it yourself in advanced ldap settings.'));
+	}
+
 	/**
 	 * detects the most often used email attribute for users applying to the
 	 * user list filter. If a setting is already present that returns at least
-- 
GitLab