]> source.dussan.org Git - nextcloud-server.git/commitdiff
LDAP: make Access be a dependency to the user and group backend instead of inheriting it.
authorArthur Schiwon <blizzz@owncloud.com>
Tue, 10 Sep 2013 15:11:02 +0000 (17:11 +0200)
committerArthur Schiwon <blizzz@owncloud.com>
Fri, 27 Sep 2013 11:34:15 +0000 (13:34 +0200)
17 files changed:
apps/user_ldap/ajax/getConfiguration.php
apps/user_ldap/ajax/setConfiguration.php
apps/user_ldap/ajax/testConfiguration.php
apps/user_ldap/appinfo/app.php
apps/user_ldap/group_ldap.php
apps/user_ldap/group_proxy.php
apps/user_ldap/lib/access.php
apps/user_ldap/lib/backendbase.php [deleted file]
apps/user_ldap/lib/backendutility.php [new file with mode: 0644]
apps/user_ldap/lib/connection.php
apps/user_ldap/lib/jobs.php
apps/user_ldap/lib/ldap.php
apps/user_ldap/lib/ldaputility.php [new file with mode: 0644]
apps/user_ldap/lib/proxy.php
apps/user_ldap/settings.php
apps/user_ldap/user_ldap.php
apps/user_ldap/user_proxy.php

index baca588976fc8a8a7755d059d88173477f26dab3..fc51b459a25b0b672900815ff9f68fc8a731e4ed 100644 (file)
@@ -27,5 +27,6 @@ OCP\JSON::checkAppEnabled('user_ldap');
 OCP\JSON::callCheck();
 
 $prefix = $_POST['ldap_serverconfig_chooser'];
-$connection = new \OCA\user_ldap\lib\Connection($prefix);
+$ldapWrapper = new OCA\user_ldap\lib\LDAP();
+$connection = new \OCA\user_ldap\lib\Connection($ldapWrapper, $prefix);
 OCP\JSON::success(array('configuration' => $connection->getConfiguration()));
index d850bda24706830145d965059b7bb1be8e375f22..94de8835fbcf9540367c2dd3c6d2cd060e940498 100644 (file)
@@ -27,7 +27,8 @@ OCP\JSON::checkAppEnabled('user_ldap');
 OCP\JSON::callCheck();
 
 $prefix = $_POST['ldap_serverconfig_chooser'];
-$connection = new \OCA\user_ldap\lib\Connection($prefix);
+$ldapWrapper = new OCA\user_ldap\lib\LDAP();
+$connection = new \OCA\user_ldap\lib\Connection($ldapWrapper, $prefix);
 $connection->setConfiguration($_POST);
 $connection->saveConfiguration();
 OCP\JSON::success();
index 7ce1258a7967e7b8cd711f021b4f0fe4346b213d..0b8e4ccfe2048dac6248a43eb667dc75b64b5ddf 100644 (file)
@@ -28,7 +28,8 @@ OCP\JSON::callCheck();
 
 $l=OC_L10N::get('user_ldap');
 
-$connection = new \OCA\user_ldap\lib\Connection('', null);
+$ldapWrapper = new OCA\user_ldap\lib\LDAP();
+$connection = new \OCA\user_ldap\lib\Connection($ldapWrapper, '', null);
 if($connection->setConfiguration($_POST)) {
        //Configuration is okay
        if($connection->bind()) {
index 593e846bc03f8dea4514bd05cf32c99672e90717..9d6327181afb3b67181eac2afa88267ad6789399 100644 (file)
 OCP\App::registerAdmin('user_ldap', 'settings');
 
 $configPrefixes = OCA\user_ldap\lib\Helper::getServerConfigurationPrefixes(true);
+$ldapWrapper = new OCA\user_ldap\lib\LDAP();
 if(count($configPrefixes) === 1) {
-       $connector = new OCA\user_ldap\lib\Connection($configPrefixes[0]);
-       $userBackend  = new OCA\user_ldap\USER_LDAP();
-       $userBackend->setConnector($connector);
-       $groupBackend = new OCA\user_ldap\GROUP_LDAP();
-       $groupBackend->setConnector($connector);
+       $connector = new OCA\user_ldap\lib\Connection($ldapWrapper, $configPrefixes[0]);
+       $ldapAccess = new OCA\user_ldap\lib\Access($connector, $ldapWrapper);
+       $userBackend  = new OCA\user_ldap\USER_LDAP($ldapAccess);
+       $groupBackend = new OCA\user_ldap\GROUP_LDAP($ldapAccess);
 } else {
-       $userBackend  = new OCA\user_ldap\User_Proxy($configPrefixes);
-       $groupBackend  = new OCA\user_ldap\Group_Proxy($configPrefixes);
+       $userBackend  = new OCA\user_ldap\User_Proxy($configPrefixes, $ldapWrapper);
+       $groupBackend  = new OCA\user_ldap\Group_Proxy($configPrefixes, $ldapWrapper);
 }
 
 if(count($configPrefixes) > 0) {
index 04ff392f9205ab88f692e4994abc47a19b080a6d..80b988594dea31bd2afd7be0668c1d616bbe16ee 100644 (file)
 
 namespace OCA\user_ldap;
 
-class GROUP_LDAP extends lib\Access implements \OCP\GroupInterface {
+use OCA\user_ldap\lib\Access;
+use OCA\user_ldap\lib\BackendUtility;
+
+class GROUP_LDAP extends BackendUtility implements \OCP\GroupInterface {
        protected $enabled = false;
 
-       public function setConnector(lib\Connection &$connection) {
-               parent::setConnector($connection);
-               $filter = $this->connection->ldapGroupFilter;
-               $gassoc = $this->connection->ldapGroupMemberAssocAttr;
+       public function __construct(Access $access) {
+               parent::__construct($access);
+               $filter = $this->access->connection->ldapGroupFilter;
+               $gassoc = $this->access->connection->ldapGroupMemberAssocAttr;
                if(!empty($filter) && !empty($gassoc)) {
                        $this->enabled = true;
                }
@@ -47,30 +50,30 @@ class GROUP_LDAP extends lib\Access implements \OCP\GroupInterface {
                if(!$this->enabled) {
                        return false;
                }
-               if($this->connection->isCached('inGroup'.$uid.':'.$gid)) {
-                       return $this->connection->getFromCache('inGroup'.$uid.':'.$gid);
+               if($this->access->connection->isCached('inGroup'.$uid.':'.$gid)) {
+                       return $this->access->connection->getFromCache('inGroup'.$uid.':'.$gid);
                }
-               $dn_user = $this->username2dn($uid);
-               $dn_group = $this->groupname2dn($gid);
+               $dn_user = $this->access->username2dn($uid);
+               $dn_group = $this->access->groupname2dn($gid);
                // just in case
                if(!$dn_group || !$dn_user) {
-                       $this->connection->writeToCache('inGroup'.$uid.':'.$gid, false);
+                       $this->access->connection->writeToCache('inGroup'.$uid.':'.$gid, false);
                        return false;
                }
                //usually, LDAP attributes are said to be case insensitive. But there are exceptions of course.
-               $members = $this->readAttribute($dn_group, $this->connection->ldapGroupMemberAssocAttr);
+               $members = $this->access->readAttribute($dn_group, $this->access->connection->ldapGroupMemberAssocAttr);
                if(!$members) {
-                       $this->connection->writeToCache('inGroup'.$uid.':'.$gid, false);
+                       $this->access->connection->writeToCache('inGroup'.$uid.':'.$gid, false);
                        return false;
                }
 
                //extra work if we don't get back user DNs
                //TODO: this can be done with one LDAP query
-               if(strtolower($this->connection->ldapGroupMemberAssocAttr) === 'memberuid') {
+               if(strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') {
                        $dns = array();
                        foreach($members as $mid) {
-                               $filter = str_replace('%uid', $mid, $this->connection->ldapLoginFilter);
-                               $ldap_users = $this->fetchListOfUsers($filter, 'dn');
+                               $filter = str_replace('%uid', $mid, $this->access->connection->ldapLoginFilter);
+                               $ldap_users = $this->access->fetchListOfUsers($filter, 'dn');
                                if(count($ldap_users) < 1) {
                                        continue;
                                }
@@ -80,7 +83,7 @@ class GROUP_LDAP extends lib\Access implements \OCP\GroupInterface {
                }
 
                $isInGroup = in_array($dn_user, $members);
-               $this->connection->writeToCache('inGroup'.$uid.':'.$gid, $isInGroup);
+               $this->access->connection->writeToCache('inGroup'.$uid.':'.$gid, $isInGroup);
 
                return $isInGroup;
        }
@@ -98,35 +101,35 @@ class GROUP_LDAP extends lib\Access implements \OCP\GroupInterface {
                        return array();
                }
                $cacheKey = 'getUserGroups'.$uid;
-               if($this->connection->isCached($cacheKey)) {
-                       return $this->connection->getFromCache($cacheKey);
+               if($this->access->connection->isCached($cacheKey)) {
+                       return $this->access->connection->getFromCache($cacheKey);
                }
-               $userDN = $this->username2dn($uid);
+               $userDN = $this->access->username2dn($uid);
                if(!$userDN) {
-                       $this->connection->writeToCache($cacheKey, array());
+                       $this->access->connection->writeToCache($cacheKey, array());
                        return array();
                }
 
                //uniqueMember takes DN, memberuid the uid, so we need to distinguish
-               if((strtolower($this->connection->ldapGroupMemberAssocAttr) === 'uniquemember')
-                       || (strtolower($this->connection->ldapGroupMemberAssocAttr) === 'member')
+               if((strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'uniquemember')
+                       || (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'member')
                ) {
                        $uid = $userDN;
-               } else if(strtolower($this->connection->ldapGroupMemberAssocAttr) === 'memberuid') {
-                       $result = $this->readAttribute($userDN, 'uid');
+               } else if(strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') {
+                       $result = $this->access->readAttribute($userDN, 'uid');
                        $uid = $result[0];
                } else {
                        // just in case
                        $uid = $userDN;
                }
 
-               $filter = $this->combineFilterWithAnd(array(
-                       $this->connection->ldapGroupFilter,
-                       $this->connection->ldapGroupMemberAssocAttr.'='.$uid
+               $filter = $this->access->combineFilterWithAnd(array(
+                       $this->access->connection->ldapGroupFilter,
+                       $this->access->connection->ldapGroupMemberAssocAttr.'='.$uid
                ));
-               $groups = $this->fetchListOfGroups($filter, array($this->connection->ldapGroupDisplayName, 'dn'));
-               $groups = array_unique($this->ownCloudGroupNames($groups), SORT_LOCALE_STRING);
-               $this->connection->writeToCache($cacheKey, $groups);
+               $groups = $this->access->fetchListOfGroups($filter, array($this->access->connection->ldapGroupDisplayName, 'dn'));
+               $groups = array_unique($this->access->ownCloudGroupNames($groups), SORT_LOCALE_STRING);
+               $this->access->connection->writeToCache($cacheKey, $groups);
 
                return $groups;
        }
@@ -144,70 +147,70 @@ class GROUP_LDAP extends lib\Access implements \OCP\GroupInterface {
                }
                $cachekey = 'usersInGroup-'.$gid.'-'.$search.'-'.$limit.'-'.$offset;
                // check for cache of the exact query
-               $groupUsers = $this->connection->getFromCache($cachekey);
+               $groupUsers = $this->access->connection->getFromCache($cachekey);
                if(!is_null($groupUsers)) {
                        return $groupUsers;
                }
 
                // check for cache of the query without limit and offset
-               $groupUsers = $this->connection->getFromCache('usersInGroup-'.$gid.'-'.$search);
+               $groupUsers = $this->access->connection->getFromCache('usersInGroup-'.$gid.'-'.$search);
                if(!is_null($groupUsers)) {
                        $groupUsers = array_slice($groupUsers, $offset, $limit);
-                       $this->connection->writeToCache($cachekey, $groupUsers);
+                       $this->access->connection->writeToCache($cachekey, $groupUsers);
                        return $groupUsers;
                }
 
                if($limit === -1) {
                        $limit = null;
                }
-               $groupDN = $this->groupname2dn($gid);
+               $groupDN = $this->access->groupname2dn($gid);
                if(!$groupDN) {
                        // group couldn't be found, return empty resultset
-                       $this->connection->writeToCache($cachekey, array());
+                       $this->access->connection->writeToCache($cachekey, array());
                        return array();
                }
 
-               $members = $this->readAttribute($groupDN, $this->connection->ldapGroupMemberAssocAttr);
+               $members = $this->access->readAttribute($groupDN, $this->access->connection->ldapGroupMemberAssocAttr);
                if(!$members) {
                        //in case users could not be retrieved, return empty resultset
-                       $this->connection->writeToCache($cachekey, array());
+                       $this->access->connection->writeToCache($cachekey, array());
                        return array();
                }
 
                $groupUsers = array();
-               $isMemberUid = (strtolower($this->connection->ldapGroupMemberAssocAttr) === 'memberuid');
+               $isMemberUid = (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid');
                foreach($members as $member) {
                        if($isMemberUid) {
                                //we got uids, need to get their DNs to 'tranlsate' them to usernames
-                               $filter = $this->combineFilterWithAnd(array(
+                               $filter = $this->access->combineFilterWithAnd(array(
                                        \OCP\Util::mb_str_replace('%uid', $member,
-                                               $this->connection->ldapLoginFilter, 'UTF-8'),
-                                       $this->getFilterPartForUserSearch($search)
+                                               $this->access->connection->ldapLoginFilter, 'UTF-8'),
+                                       $this->access->getFilterPartForUserSearch($search)
                                ));
-                               $ldap_users = $this->fetchListOfUsers($filter, 'dn');
+                               $ldap_users = $this->access->fetchListOfUsers($filter, 'dn');
                                if(count($ldap_users) < 1) {
                                        continue;
                                }
-                               $groupUsers[] = $this->dn2username($ldap_users[0]);
+                               $groupUsers[] = $this->access->dn2username($ldap_users[0]);
                        } else {
                                //we got DNs, check if we need to filter by search or we can give back all of them
                                if(!empty($search)) {
-                                       if(!$this->readAttribute($member,
-                                               $this->connection->ldapUserDisplayName,
-                                               $this->getFilterPartForUserSearch($search))) {
+                                       if(!$this->access->readAttribute($member,
+                                               $this->access->connection->ldapUserDisplayName,
+                                               $this->access->getFilterPartForUserSearch($search))) {
                                                continue;
                                        }
                                }
                                // dn2username will also check if the users belong to the allowed base
-                               if($ocname = $this->dn2username($member)) {
+                               if($ocname = $this->access->dn2username($member)) {
                                        $groupUsers[] = $ocname;
                                }
                        }
                }
                natsort($groupUsers);
-               $this->connection->writeToCache('usersInGroup-'.$gid.'-'.$search, $groupUsers);
+               $this->access->connection->writeToCache('usersInGroup-'.$gid.'-'.$search, $groupUsers);
                $groupUsers = array_slice($groupUsers, $offset, $limit);
-               $this->connection->writeToCache($cachekey, $groupUsers);
+               $this->access->connection->writeToCache($cachekey, $groupUsers);
 
                return $groupUsers;
        }
@@ -245,7 +248,7 @@ class GROUP_LDAP extends lib\Access implements \OCP\GroupInterface {
 
                //Check cache before driving unnecessary searches
                \OCP\Util::writeLog('user_ldap', 'getGroups '.$cachekey, \OCP\Util::DEBUG);
-               $ldap_groups = $this->connection->getFromCache($cachekey);
+               $ldap_groups = $this->access->connection->getFromCache($cachekey);
                if(!is_null($ldap_groups)) {
                        return $ldap_groups;
                }
@@ -255,16 +258,16 @@ class GROUP_LDAP extends lib\Access implements \OCP\GroupInterface {
                if($limit <= 0) {
                        $limit = null;
                }
-               $filter = $this->combineFilterWithAnd(array(
-                       $this->connection->ldapGroupFilter,
-                       $this->getFilterPartForGroupSearch($search)
+               $filter = $this->access->combineFilterWithAnd(array(
+                       $this->access->connection->ldapGroupFilter,
+                       $this->access->getFilterPartForGroupSearch($search)
                ));
                \OCP\Util::writeLog('user_ldap', 'getGroups Filter '.$filter, \OCP\Util::DEBUG);
-               $ldap_groups = $this->fetchListOfGroups($filter, array($this->connection->ldapGroupDisplayName, 'dn'),
+               $ldap_groups = $this->access->fetchListOfGroups($filter, array($this->access->connection->ldapGroupDisplayName, 'dn'),
                        $limit, $offset);
-               $ldap_groups = $this->ownCloudGroupNames($ldap_groups);
+               $ldap_groups = $this->access->ownCloudGroupNames($ldap_groups);
 
-               $this->connection->writeToCache($cachekey, $ldap_groups);
+               $this->access->connection->writeToCache($cachekey, $ldap_groups);
                return $ldap_groups;
        }
 
@@ -278,25 +281,25 @@ class GROUP_LDAP extends lib\Access implements \OCP\GroupInterface {
         * @return bool
         */
        public function groupExists($gid) {
-               if($this->connection->isCached('groupExists'.$gid)) {
-                       return $this->connection->getFromCache('groupExists'.$gid);
+               if($this->access->connection->isCached('groupExists'.$gid)) {
+                       return $this->access->connection->getFromCache('groupExists'.$gid);
                }
 
                //getting dn, if false the group does not exist. If dn, it may be mapped only, requires more checking.
-               $dn = $this->groupname2dn($gid);
+               $dn = $this->access->groupname2dn($gid);
                if(!$dn) {
-                       $this->connection->writeToCache('groupExists'.$gid, false);
+                       $this->access->connection->writeToCache('groupExists'.$gid, false);
                        return false;
                }
 
                //if group really still exists, we will be able to read its objectclass
-               $objcs = $this->readAttribute($dn, 'objectclass');
+               $objcs = $this->access->readAttribute($dn, 'objectclass');
                if(!$objcs || empty($objcs)) {
-                       $this->connection->writeToCache('groupExists'.$gid, false);
+                       $this->access->connection->writeToCache('groupExists'.$gid, false);
                        return false;
                }
 
-               $this->connection->writeToCache('groupExists'.$gid, true);
+               $this->access->connection->writeToCache('groupExists'.$gid, true);
                return true;
        }
 
index eb6f176c58ce6e5e70da2d151f086299562690c1..78a616854c718b1b0020cd4bf7f83abb1d2598b8 100644 (file)
@@ -23,6 +23,8 @@
 
 namespace OCA\user_ldap;
 
+use OCA\user_ldap\lib\ILDAPWrapper;
+
 class Group_Proxy extends lib\Proxy implements \OCP\GroupInterface {
        private $backends = array();
        private $refBackend = null;
@@ -31,12 +33,10 @@ class Group_Proxy extends lib\Proxy implements \OCP\GroupInterface {
         * @brief Constructor
         * @param $serverConfigPrefixes array containing the config Prefixes
         */
-       public function __construct($serverConfigPrefixes) {
-               parent::__construct();
+       public function __construct($serverConfigPrefixes, ILDAPWrapper $ldap) {
+               parent::__construct($ldap);
                foreach($serverConfigPrefixes as $configPrefix) {
-                   $this->backends[$configPrefix] = new \OCA\user_ldap\GROUP_LDAP();
-                   $connector = $this->getConnector($configPrefix);
-                       $this->backends[$configPrefix]->setConnector($connector);
+                   $this->backends[$configPrefix] = new \OCA\user_ldap\GROUP_LDAP($this->getAccess($configPrefix));
                        if(is_null($this->refBackend)) {
                                $this->refBackend = &$this->backends[$configPrefix];
                        }
index 4b7920b7162c7bd5bd58e6203b03227de8b856ff..3d791755aba866a3d2328357b7f4a6a48397b030 100644 (file)
 
 namespace OCA\user_ldap\lib;
 
-abstract class Access extends BackendBase {
-       protected $connection;
+class Access extends LDAPUtility {
+       public $connection;
        //never ever check this var directly, always use getPagedSearchResultState
        protected $pagedSearchedSuccessful;
 
-       public function setConnector(Connection &$connection) {
+       public function __construct(Connection $connection, ILDAPWrapper $ldap) {
+               parent::__construct($ldap);
                $this->connection = $connection;
        }
 
diff --git a/apps/user_ldap/lib/backendbase.php b/apps/user_ldap/lib/backendbase.php
deleted file mode 100644 (file)
index 5ed73a0..0000000
+++ /dev/null
@@ -1,46 +0,0 @@
-<?php
-
-/**
- * ownCloud â€“ LDAP BackendBase
- *
- * @author Arthur Schiwon
- * @copyright 2013 Arthur Schiwon blizzz@owncloud.com
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
- *
- * You should have received a copy of the GNU Affero General Public
- * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
- *
- */
-
-namespace OCA\user_ldap\lib;
-
-abstract class BackendBase {
-       protected $ldap;
-
-       public function __construct() {
-               $this->ldap = new LDAP();
-       }
-
-       /**
-        * @brief sets the LDAP Wrapper to be used
-        *
-        * @param $ldapWrapper an instance of the Wrapper
-        * @return true on success, otherwise false
-        *
-        * The LDAP Wrapper must implement the PHP LDAP functions, which are used
-        * in the LDAP backend
-        */
-       public function setLDAPWrapper(ILDAPWrapper $ldapWrapper) {
-               $this->ldap = $ldapWrapper;
-               return true;
-       }
-}
\ No newline at end of file
diff --git a/apps/user_ldap/lib/backendutility.php b/apps/user_ldap/lib/backendutility.php
new file mode 100644 (file)
index 0000000..f279b1e
--- /dev/null
@@ -0,0 +1,38 @@
+<?php
+
+/**
+ * ownCloud â€“ LDAP BackendUtility
+ *
+ * @author Arthur Schiwon
+ * @copyright 2013 Arthur Schiwon blizzz@owncloud.com
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public
+ * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCA\user_ldap\lib;
+
+use OCA\user_ldap\lib\Access;
+
+abstract class BackendUtility {
+       protected $access;
+
+       /**
+        * @brief constructor, make sure the subclasses call this one!
+        * @param $access an instance of Access for LDAP interaction
+        */
+       public function __construct(Access $access) {
+               $this->access = $access;
+       }
+}
\ No newline at end of file
index dd627a4e1b12375884eecf8a1aa9a312922f3297..0bf13a19377bcfff107859c8bd9290d62a4e1322 100644 (file)
@@ -23,7 +23,7 @@
 
 namespace OCA\user_ldap\lib;
 
-class Connection extends BackendBase {
+class Connection extends LDAPUtility {
        private $ldapConnectionRes = null;
        private $configPrefix;
        private $configID;
@@ -60,7 +60,7 @@ class Connection extends BackendBase {
                'ldapQuotaDefault' => null,
                'ldapEmailAttribute' => null,
                'ldapCacheTTL' => null,
-               'ldapUuidAttribute' => null,
+               'ldapUuidAttribute' => 'auto',
                'ldapOverrideUuidAttribute' => null,
                'ldapOverrideMainServer' => false,
                'ldapConfigurationActive' => false,
@@ -77,8 +77,8 @@ class Connection extends BackendBase {
         * @param $configPrefix a string with the prefix for the configkey column (appconfig table)
         * @param $configID a string with the value for the appid column (appconfig table) or null for on-the-fly connections
         */
-       public function __construct($configPrefix = '', $configID = 'user_ldap') {
-               parent::__construct();
+       public function __construct(ILDAPWrapper $ldap, $configPrefix = '', $configID = 'user_ldap') {
+               parent::__construct($ldap);
                $this->configPrefix = $configPrefix;
                $this->configID = $configID;
                $memcache = new \OC\Memcache\Factory();
@@ -363,6 +363,14 @@ class Connection extends BackendBase {
                                        && $params[$parameter] === 'homeFolderNamingRule'))
                                && !empty($value)) {
                                $value = 'attr:'.$value;
+                       } else if (strpos($parameter, 'ldapBase') !== false
+                               || (isset($params[$parameter])
+                                       && strpos($params[$parameter], 'ldapBase') !== false)) {
+                               $this->readBase($params[$parameter], $value);
+                               if(is_array($setParameters)) {
+                                       $setParameters[] = $parameter;
+                               }
+                               continue;
                        }
                    if(isset($this->config[$parameter])) {
                                $this->config[$parameter] = $value;
@@ -433,10 +441,6 @@ class Connection extends BackendBase {
                                        $config[$dbKey] = '';
                                }
                                continue;
-                       } else if((strpos($classKey, 'ldapBase') !== false)
-                                       || (strpos($classKey, 'ldapAttributes') !== false)) {
-                               $config[$dbKey] = implode("\n", $this->config[$classKey]);
-                               continue;
                        }
                        $config[$dbKey] = $this->config[$classKey];
                }
@@ -553,7 +557,7 @@ class Connection extends BackendBase {
         * @returns an associative array with the default values. Keys are correspond
         * to config-value entries in the database table
         */
-       public function getDefaults() {
+       static public function getDefaults() {
                return array(
                        'ldap_host'                         => '',
                        'ldap_port'                         => '389',
index 6b7666d4ca1752ee869221fb41ff03ee9998832e..2f90da3bfb617e082e44e6d2a6db5707519cabe9 100644 (file)
@@ -139,13 +139,14 @@ class Jobs extends \OC\BackgroundJob\TimedJob {
                        return self::$groupBE;
                }
                $configPrefixes = Helper::getServerConfigurationPrefixes(true);
-               if(count($configPrefixes) == 1) {
+               $ldapWrapper = new OCA\user_ldap\lib\LDAP();
+               if(count($configPrefixes) === 1) {
                        //avoid the proxy when there is only one LDAP server configured
-                       $connector = new Connection($configPrefixes[0]);
-                       self::$groupBE = new \OCA\user_ldap\GROUP_LDAP();
-                       self::$groupBE->setConnector($connector);
+                       $connector = new OCA\user_ldap\lib\Connection($ldapWrapper, $configPrefixes[0]);
+                       $ldapAccess = new OCA\user_ldap\lib\Access($connector, $ldapWrapper);
+                       self::$groupBE = new OCA\user_ldap\GROUP_LDAP($ldapAccess);
                } else {
-                       self::$groupBE = new \OCA\user_ldap\Group_Proxy($configPrefixes);
+                       self::$groupBE = new \OCA\user_ldap\Group_Proxy($configPrefixes, $ldapWrapper);
                }
 
                return self::$groupBE;
index 0f082147b147d863eebae22a9d2653438703d783..ee2e336cc95793e61bf188d2ab4f48364b743a75 100644 (file)
@@ -149,6 +149,8 @@ class LDAP implements ILDAPWrapper {
                                                  && $errorCode === -4) {
                                } else if ($errorCode === 32) {
                                        //for now
+                               } else if ($errorCode === 10) {
+                                       //referrals, we switch them off, but then there is AD :)
                                } else {
                                        throw new \Exception('LDAP error '.$errorMsg.' (' .
                                                $errorCode.') after calling '.$this->curFunc.
diff --git a/apps/user_ldap/lib/ldaputility.php b/apps/user_ldap/lib/ldaputility.php
new file mode 100644 (file)
index 0000000..975df7d
--- /dev/null
@@ -0,0 +1,36 @@
+<?php
+
+/**
+ * ownCloud â€“ LDAP LDAPUtility
+ *
+ * @author Arthur Schiwon
+ * @copyright 2013 Arthur Schiwon blizzz@owncloud.com
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public
+ * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCA\user_ldap\lib;
+
+abstract class LDAPUtility {
+       protected $ldap;
+
+       /**
+        * @brief constructor, make sure the subclasses call this one!
+        * @param $ldapWrapper an instance of an ILDAPWrapper
+        */
+       public function __construct(ILDAPWrapper $ldapWrapper) {
+               $this->ldap = $ldapWrapper;
+       }
+}
\ No newline at end of file
index ae3e3be73611e355c3b16dfca5d0662e537424e3..c74b357bdd2fa77fdb564878fe31d9f6e6b4cac0 100644 (file)
 
 namespace OCA\user_ldap\lib;
 
+use OCA\user_ldap\lib\Access;
+
 abstract class Proxy {
-       static private $connectors = array();
+       static private $accesses = array();
+       private $ldap = null;
 
-       public function __construct() {
+       public function __construct(ILDAPWrapper $ldap) {
+               $this->ldap = $ldap;
                $this->cache = \OC_Cache::getGlobalCache();
        }
 
-       private function addConnector($configPrefix) {
-               self::$connectors[$configPrefix] = new \OCA\user_ldap\lib\Connection($configPrefix);
+       private function addAccess($configPrefix) {
+               $connector = new Connection($this->ldap, $configPrefix);
+               self::$accesses[$configPrefix] = new Access($connector, $this->ldap);
        }
 
-       protected function getConnector($configPrefix) {
-               if(!isset(self::$connectors[$configPrefix])) {
-                       $this->addConnector($configPrefix);
+       protected function getAccess($configPrefix) {
+               if(!isset(self::$accesses[$configPrefix])) {
+                       $this->addAccess($configPrefix);
                }
-               return self::$connectors[$configPrefix];
-       }
-
-       protected function getConnectors() {
-               return self::$connectors;
+               return self::$accesses[$configPrefix];
        }
 
        protected function getUserCacheKey($uid) {
index 7169192a18e4a94e4eb3d087e594577aed43a8e5..b7070f23183b00b2c5cd73fe6d62b21ae7f2652f 100644 (file)
@@ -49,14 +49,9 @@ $tmpl->assign('serverConfigurationPrefixes', $prefixes);
 $tmpl->assign('serverConfigurationHosts', $hosts);
 
 // assign default values
-if(!isset($ldap)) {
-       $ldap = new \OCA\user_ldap\lib\Connection();
-}
-$defaults = $ldap->getDefaults();
+$defaults = \OCA\user_ldap\lib\Connection::getDefaults();
 foreach($defaults as $key => $default) {
     $tmpl->assign($key.'_default', $default);
 }
 
-// $tmpl->assign();
-
 return $tmpl->fetchPage();
index ab3b57001b6bf3f364e72f922e5b9bd1a02f711e..f2e99d4579477aaaab009cf348ce606d0caa6a63 100644 (file)
 
 namespace OCA\user_ldap;
 
-class USER_LDAP extends lib\Access implements \OCP\UserInterface {
+use OCA\user_ldap\lib\ILDAPWrapper;
+use OCA\user_ldap\lib\BackendUtility;
+
+class USER_LDAP extends BackendUtility implements \OCP\UserInterface {
 
        private function updateQuota($dn) {
                $quota = null;
-               $quotaDefault = $this->connection->ldapQuotaDefault;
-               $quotaAttribute = $this->connection->ldapQuotaAttribute;
+               $quotaDefault = $this->access->connection->ldapQuotaDefault;
+               $quotaAttribute = $this->access->connection->ldapQuotaAttribute;
                if(!empty($quotaDefault)) {
                        $quota = $quotaDefault;
                }
                if(!empty($quotaAttribute)) {
-                       $aQuota = $this->readAttribute($dn, $quotaAttribute);
+                       $aQuota = $this->access->readAttribute($dn, $quotaAttribute);
 
                        if($aQuota && (count($aQuota) > 0)) {
                                $quota = $aQuota[0];
                        }
                }
                if(!is_null($quota)) {
-                       \OCP\Config::setUserValue($this->dn2username($dn), 'files', 'quota', \OCP\Util::computerFileSize($quota));
+                       \OCP\Config::setUserValue(      $this->access->dn2username($dn),
+                                                                               'files',
+                                                                               'quota',
+                                                                               \OCP\Util::computerFileSize($quota));
                }
        }
 
        private function updateEmail($dn) {
                $email = null;
-               $emailAttribute = $this->connection->ldapEmailAttribute;
+               $emailAttribute = $this->access->connection->ldapEmailAttribute;
                if(!empty($emailAttribute)) {
-                       $aEmail = $this->readAttribute($dn, $emailAttribute);
+                       $aEmail = $this->access->readAttribute($dn, $emailAttribute);
                        if($aEmail && (count($aEmail) > 0)) {
                                $email = $aEmail[0];
                        }
                        if(!is_null($email)) {
-                               \OCP\Config::setUserValue($this->dn2username($dn), 'settings', 'email', $email);
+                               \OCP\Config::setUserValue(      $this->access->dn2username($dn),
+                                                                                       'settings',
+                                                                                       'email',
+                                                                                       $email);
                        }
                }
        }
@@ -70,15 +79,15 @@ class USER_LDAP extends lib\Access implements \OCP\UserInterface {
         */
        public function checkPassword($uid, $password) {
                //find out dn of the user name
-               $filter = \OCP\Util::mb_str_replace('%uid', $uid, $this->connection->ldapLoginFilter, 'UTF-8');
-               $ldap_users = $this->fetchListOfUsers($filter, 'dn');
+               $filter = \OCP\Util::mb_str_replace('%uid', $uid, $this->access->connection->ldapLoginFilter, 'UTF-8');
+               $ldap_users = $this->access->fetchListOfUsers($filter, 'dn');
                if(count($ldap_users) < 1) {
                        return false;
                }
                $dn = $ldap_users[0];
 
                //do we have a username for him/her?
-               $ocname = $this->dn2username($dn);
+               $ocname = $this->access->dn2username($dn);
 
                if($ocname) {
                        //update some settings, if necessary
@@ -86,7 +95,7 @@ class USER_LDAP extends lib\Access implements \OCP\UserInterface {
                        $this->updateEmail($dn);
 
                        //are the credentials OK?
-                       if(!$this->areCredentialsValid($dn, $password)) {
+                       if(!$this->access->areCredentialsValid($dn, $password)) {
                                return false;
                        }
 
@@ -107,7 +116,7 @@ class USER_LDAP extends lib\Access implements \OCP\UserInterface {
                $cachekey = 'getUsers-'.$search.'-'.$limit.'-'.$offset;
 
                //check if users are cached, if so return
-               $ldap_users = $this->connection->getFromCache($cachekey);
+               $ldap_users = $this->access->connection->getFromCache($cachekey);
                if(!is_null($ldap_users)) {
                        return $ldap_users;
                }
@@ -117,21 +126,23 @@ class USER_LDAP extends lib\Access implements \OCP\UserInterface {
                if($limit <= 0) {
                        $limit = null;
                }
-               $filter = $this->combineFilterWithAnd(array(
-                       $this->connection->ldapUserFilter,
-                       $this->getFilterPartForUserSearch($search)
+               $filter = $this->access->combineFilterWithAnd(array(
+                       $this->access->connection->ldapUserFilter,
+                       $this->access->getFilterPartForUserSearch($search)
                ));
 
                \OCP\Util::writeLog('user_ldap',
                        'getUsers: Options: search '.$search.' limit '.$limit.' offset '.$offset.' Filter: '.$filter,
                        \OCP\Util::DEBUG);
                //do the search and translate results to owncloud names
-               $ldap_users = $this->fetchListOfUsers($filter, array($this->connection->ldapUserDisplayName, 'dn'),
+               $ldap_users = $this->access->fetchListOfUsers(
+                       $filter,
+                       array($this->access->connection->ldapUserDisplayName, 'dn'),
                        $limit, $offset);
-               $ldap_users = $this->ownCloudUserNames($ldap_users);
+               $ldap_users = $this->access->ownCloudUserNames($ldap_users);
                \OCP\Util::writeLog('user_ldap', 'getUsers: '.count($ldap_users). ' Users found', \OCP\Util::DEBUG);
 
-               $this->connection->writeToCache($cachekey, $ldap_users);
+               $this->access->connection->writeToCache($cachekey, $ldap_users);
                return $ldap_users;
        }
 
@@ -141,22 +152,24 @@ class USER_LDAP extends lib\Access implements \OCP\UserInterface {
         * @return boolean
         */
        public function userExists($uid) {
-               if($this->connection->isCached('userExists'.$uid)) {
-                       return $this->connection->getFromCache('userExists'.$uid);
+               if($this->access->connection->isCached('userExists'.$uid)) {
+                       return $this->access->connection->getFromCache('userExists'.$uid);
                }
                //getting dn, if false the user does not exist. If dn, he may be mapped only, requires more checking.
-               $dn = $this->username2dn($uid);
+               $dn = $this->access->username2dn($uid);
                if(!$dn) {
-                       $this->connection->writeToCache('userExists'.$uid, false);
+                       \OCP\Util::writeLog('user_ldap', 'No DN found for '.$uid.' on '.$this->access->connection->ldapHost, \OCP\Util::DEBUG);
+                       $this->access->connection->writeToCache('userExists'.$uid, false);
                        return false;
                }
                //check if user really still exists by reading its entry
-               if(!is_array($this->readAttribute($dn, ''))) {
-                       $this->connection->writeToCache('userExists'.$uid, false);
+               if(!is_array($this->access->readAttribute($dn, ''))) {
+                       \OCP\Util::writeLog('user_ldap', 'LDAP says no user '.$dn, \OCP\Util::DEBUG);
+                       $this->access->connection->writeToCache('userExists'.$uid, false);
                        return false;
                }
 
-               $this->connection->writeToCache('userExists'.$uid, true);
+               $this->access->connection->writeToCache('userExists'.$uid, true);
                $this->updateQuota($dn);
                return true;
        }
@@ -184,12 +197,13 @@ class USER_LDAP extends lib\Access implements \OCP\UserInterface {
                }
 
                $cacheKey = 'getHome'.$uid;
-               if($this->connection->isCached($cacheKey)) {
-                       return $this->connection->getFromCache($cacheKey);
+               if($this->access->connection->isCached($cacheKey)) {
+                       return $this->access->connection->getFromCache($cacheKey);
                }
-               if(strpos($this->connection->homeFolderNamingRule, 'attr:') === 0) {
-                       $attr = substr($this->connection->homeFolderNamingRule, strlen('attr:'));
-                       $homedir = $this->readAttribute($this->username2dn($uid), $attr);
+               if(strpos($this->access->connection->homeFolderNamingRule, 'attr:') === 0) {
+                       $attr = substr($this->access->connection->homeFolderNamingRule, strlen('attr:'));
+                       $homedir = $this->access->readAttribute(
+                                               $this->access->username2dn($uid), $attr);
                        if($homedir && isset($homedir[0])) {
                                $path = $homedir[0];
                                //if attribute's value is an absolute path take this, otherwise append it to data dir
@@ -204,13 +218,13 @@ class USER_LDAP extends lib\Access implements \OCP\UserInterface {
                                        $homedir = \OCP\Config::getSystemValue('datadirectory',
                                                \OC::$SERVERROOT.'/data' ) . '/' . $homedir[0];
                                }
-                               $this->connection->writeToCache($cacheKey, $homedir);
+                               $this->access->connection->writeToCache($cacheKey, $homedir);
                                return $homedir;
                        }
                }
 
                //false will apply default behaviour as defined and done by OC_User
-               $this->connection->writeToCache($cacheKey, false);
+               $this->access->connection->writeToCache($cacheKey, false);
                return false;
        }
 
@@ -225,16 +239,16 @@ class USER_LDAP extends lib\Access implements \OCP\UserInterface {
                }
 
                $cacheKey = 'getDisplayName'.$uid;
-               if(!is_null($displayName = $this->connection->getFromCache($cacheKey))) {
+               if(!is_null($displayName = $this->access->connection->getFromCache($cacheKey))) {
                        return $displayName;
                }
 
-               $displayName = $this->readAttribute(
-                       $this->username2dn($uid),
-                       $this->connection->ldapUserDisplayName);
+               $displayName = $this->access->readAttribute(
+                       $this->access->username2dn($uid),
+                       $this->access->connection->ldapUserDisplayName);
 
                if($displayName && (count($displayName) > 0)) {
-                       $this->connection->writeToCache($cacheKey, $displayName[0]);
+                       $this->access->connection->writeToCache($cacheKey, $displayName[0]);
                        return $displayName[0];
                }
 
@@ -249,7 +263,7 @@ class USER_LDAP extends lib\Access implements \OCP\UserInterface {
         */
        public function getDisplayNames($search = '', $limit = null, $offset = null) {
                $cacheKey = 'getDisplayNames-'.$search.'-'.$limit.'-'.$offset;
-               if(!is_null($displayNames = $this->connection->getFromCache($cacheKey))) {
+               if(!is_null($displayNames = $this->access->connection->getFromCache($cacheKey))) {
                        return $displayNames;
                }
 
@@ -258,7 +272,7 @@ class USER_LDAP extends lib\Access implements \OCP\UserInterface {
                foreach ($users as $user) {
                        $displayNames[$user] = $this->getDisplayName($user);
                }
-               $this->connection->writeToCache($cacheKey, $displayNames);
+               $this->access->connection->writeToCache($cacheKey, $displayNames);
                return $displayNames;
        }
 
index 0722d8871a42ce06eb097b7a6f4fe403d200ad17..c7b469c8224061f4ae0d884860a9736a21dc116e 100644 (file)
@@ -23,6 +23,8 @@
 
 namespace OCA\user_ldap;
 
+use OCA\user_ldap\lib\ILDAPWrapper;
+
 class User_Proxy extends lib\Proxy implements \OCP\UserInterface {
        private $backends = array();
        private $refBackend = null;
@@ -31,12 +33,10 @@ class User_Proxy extends lib\Proxy implements \OCP\UserInterface {
         * @brief Constructor
         * @param $serverConfigPrefixes array containing the config Prefixes
         */
-       public function __construct($serverConfigPrefixes) {
-               parent::__construct();
+       public function __construct($serverConfigPrefixes, ILDAPWrapper $ldap) {
+               parent::__construct($ldap);
                foreach($serverConfigPrefixes as $configPrefix) {
-                   $this->backends[$configPrefix] = new \OCA\user_ldap\USER_LDAP();
-                   $connector = $this->getConnector($configPrefix);
-                       $this->backends[$configPrefix]->setConnector($connector);
+                   $this->backends[$configPrefix] = new \OCA\user_ldap\USER_LDAP($this->getAccess($configPrefix));
                        if(is_null($this->refBackend)) {
                                $this->refBackend = &$this->backends[$configPrefix];
                        }