diff options
author | Arthur Schiwon <blizzz@owncloud.com> | 2014-06-25 14:04:06 +0200 |
---|---|---|
committer | Arthur Schiwon <blizzz@owncloud.com> | 2014-08-29 11:14:16 +0200 |
commit | baa49cd58a670ca7fdd9211cf057b2257d86eff9 (patch) | |
tree | 7a4cb6e8267c0c9705525ba709b6d91ed201ca53 | |
parent | ed2424c3822a3470065788ff113a3807d24d80dc (diff) | |
download | nextcloud-server-baa49cd58a670ca7fdd9211cf057b2257d86eff9.tar.gz nextcloud-server-baa49cd58a670ca7fdd9211cf057b2257d86eff9.zip |
Wizad: email attribute detection
-rw-r--r-- | apps/user_ldap/lib/wizard.php | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/apps/user_ldap/lib/wizard.php b/apps/user_ldap/lib/wizard.php index 0cec493d9ed..9a7f7520f56 100644 --- a/apps/user_ldap/lib/wizard.php +++ b/apps/user_ldap/lib/wizard.php @@ -129,6 +129,69 @@ class Wizard extends LDAPUtility { } /** + * counts users with a specified attribute + * @param string $attr + * @return int|bool + */ + public function countUsersWithAttribute($attr) { + if(!$this->checkRequirements(array('ldapHost', + 'ldapPort', + 'ldapBase', + 'ldapUserFilter', + ))) { + return false; + } + + $access = $this->getAccess(); + $filter = $access->combineFilterWithAnd(array( + $this->configuration->ldapUserFilter, + $attr . '=*' + )); + + return $access->countUsers($filter); + } + + /** + * 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 + * one hit, the detection will be canceled. + * @return bool + */ + public function detectEmailAttribute() { + if(!$this->checkRequirements(array('ldapHost', + 'ldapPort', + 'ldapBase', + 'ldapUserFilter', + ))) { + return false; + } + + $attr = $this->configuration->ldapEmailAttribute; + if(!empty($attr)) { + $count = intval($this->countUsersWithAttribute($attr)); + if($count > 0) { + return false; + } + } + + $emailAttributes = array('mail', 'mailPrimaryAddress'); + $winner = ''; + $maxUsers = 0; + foreach($emailAttributes as $attr) { + $count = $this->countUsersWithAttribute($attr); + if($count > $maxUsers) { + $maxUsers = $count; + $winner = $attr; + } + } + + if($winner !== '') { + $this->result->addChange('ldap_email_attr', $winner); + } + + } + + /** * @return WizardResult * @throws \Exception */ @@ -749,6 +812,7 @@ class Wizard extends LDAPUtility { if(empty($filter)) { $filter = '(objectclass=*)'; } + $this->detectEmailAttribute(); break; case self::LFILTER_GROUP_LIST: |