]> source.dussan.org Git - nextcloud-server.git/commitdiff
Wizard: autodetection of group-member-assoc attribute
authorArthur Schiwon <blizzz@owncloud.com>
Wed, 9 Oct 2013 23:21:05 +0000 (01:21 +0200)
committerArthur Schiwon <blizzz@owncloud.com>
Thu, 17 Oct 2013 17:13:28 +0000 (19:13 +0200)
apps/user_ldap/ajax/wizard.php
apps/user_ldap/js/settings.js
apps/user_ldap/lib/wizard.php
apps/user_ldap/lib/wizardresult.php

index ebeedaee959cf79f651942914450d4d56854e211..c90efdf7e9e95bbc660e90cf1de43760d52c5be4 100644 (file)
@@ -46,6 +46,7 @@ $wizard = new \OCA\user_ldap\lib\Wizard($configuration, $ldapWrapper);
 switch($action) {
        case 'guessPortAndTLS':
        case 'guessBaseDN':
+       case 'determineGroupMemberAssoc':
        case 'determineUserObjectClasses':
        case 'determineGroupObjectClasses':
        case 'determineGroupsForUsers':
index 049df9b8b17cc6ec38c910c002df4711729bc556..6b1f363bbaf6d54abfe9b7325f6f484f8165e06e 100644 (file)
@@ -226,6 +226,7 @@ var LdapWizard = {
                                        LdapWizard.countUsers();
                                } else if(type == 'group') {
                                        LdapWizard.countGroups();
+                                       LdapWizard.detectGroupMemberAssoc();
                                }
                        },
                        function (result) {
@@ -256,6 +257,20 @@ var LdapWizard = {
                LdapWizard._countThings('countUsers');
        },
 
+       detectGroupMemberAssoc: function() {
+               param = 'action=determineGroupMemberAssoc'+
+                               '&ldap_serverconfig_chooser='+$('#ldap_serverconfig_chooser').val();
+
+               LdapWizard.ajax(param,
+                       function(result) {
+                               //pure background story
+                       },
+                       function (result) {
+                               // error handling
+                       }
+               );
+       },
+
        findAttributes: function() {
                param = 'action=determineAttributes'+
                                '&ldap_serverconfig_chooser='+$('#ldap_serverconfig_chooser').val();
@@ -395,6 +410,7 @@ var LdapWizard = {
                        LdapWizard.countUsers();
                } else if(triggerObj.id == 'ldap_group_filter') {
                        LdapWizard.countGroups();
+                       LdapWizard.detectGroupMemberAssoc();
                }
 
                if(triggerObj.id == 'ldap_loginfilter_username'
index 86cf8d75af673b302a1efd00ea259337a28f578a..2538fe4a2c5cc1a97188373293ce5d95b3c5a4e3 100644 (file)
@@ -230,6 +230,26 @@ class Wizard extends LDAPUtility {
                return $this->result;
        }
 
+       public function determineGroupMemberAssoc() {
+               if(!$this->checkRequirements(array('ldapHost',
+                                                                                  'ldapPort',
+                                                                                  'ldapAgentName',
+                                                                                  'ldapAgentPassword',
+                                                                                  'ldapGroupFilter',
+                                                                                  ))) {
+                       return  false;
+               }
+               $attribute = $this->detectGroupMemberAssoc();
+               if($attribute === false) {
+                       return false;
+               }
+               $this->configuration->setConfiguration(array('ldapGroupMemberAssocAttr' => $attribute));
+               //so it will be saved on destruct
+               $this->result->markChange();
+
+               return $this->result;
+       }
+
        /**
         * @brief detects the available object classes
         * @returns the instance's WizardResult instance
@@ -462,6 +482,47 @@ class Wizard extends LDAPUtility {
                }
        }
 
+       /**
+        * @brief tries to detect the group member association attribute which is
+        * one of 'uniqueMember', 'memberUid', 'member'
+        * @return mixed, string with the attribute name, false on error
+        */
+       private function detectGroupMemberAssoc() {
+               $possibleAttrs = array('uniqueMember', 'memberUid', 'member', 'unfugasdfasdfdfa');
+               $filter = $this->configuration->ldapGroupFilter;
+               if(empty($filter)) {
+                       return false;
+               }
+               $cr = $this->getConnection();
+               if(!$cr) {
+                       throw new \Excpetion('Could not connect to LDAP');
+               }
+               $base = $this->configuration->ldapBase[0];
+               $rr = $this->ldap->search($cr, $base, $filter, $possibleAttrs);
+               if(!$this->ldap->isResource($rr)) {
+                       return false;
+               }
+               $er = $this->ldap->firstEntry($cr, $rr);
+               while(is_resource($er)) {
+                       $dn = $this->ldap->getDN($cr, $er);
+                       $attrs = $this->ldap->getAttributes($cr, $er);
+                       $result = array();
+                       for($i = 0; $i < count($possibleAttrs); $i++) {
+                               if(isset($attrs[$possibleAttrs[$i]])) {
+                                       $result[$possibleAttrs[$i]] = $attrs[$possibleAttrs[$i]]['count'];
+                               }
+                       }
+                       if(!empty($result)) {
+                               natsort($result);
+                               return key($result);
+                       }
+
+                       $er = $this->ldap->nextEntry($cr, $er);
+               }
+
+               return false;
+       }
+
        /**
         * @brief Checks whether for a given BaseDN results will be returned
         * @param $base the BaseDN to test
index 4c3b563c0c2ab7a90ccb25e678fe1801b1f4790d..542f106cad8dc2b88a80aeac3bdce6ec400045fe 100644 (file)
@@ -26,11 +26,16 @@ namespace OCA\user_ldap\lib;
 class WizardResult {
        protected $changes = array();
        protected $options = array();
+       protected $markedChange = false;
 
        public function addChange($key, $value) {
                $this->changes[$key] = $value;
        }
 
+       public function markChange() {
+               $this->markedChange = true;
+       }
+
        public function addOptions($key, $values) {
                if(!is_array($values)) {
                        $values = array($values);
@@ -39,7 +44,7 @@ class WizardResult {
        }
 
        public function hasChanges() {
-               return count($this->changes) > 0;
+               return (count($this->changes) > 0 || $this->markedChange);
        }
 
        public function getResultArray() {