LDAP: make Access be a dependency to the user and group backend instead of inheriting it.

This commit is contained in:
Arthur Schiwon 2013-09-10 17:11:02 +02:00
parent b9cc2ad660
commit d4f92494a2
16 changed files with 226 additions and 174 deletions

View File

@ -27,5 +27,6 @@ OCP\JSON::checkAppEnabled('user_ldap');
OCP\JSON::callCheck(); OCP\JSON::callCheck();
$prefix = $_POST['ldap_serverconfig_chooser']; $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())); OCP\JSON::success(array('configuration' => $connection->getConfiguration()));

View File

@ -27,7 +27,8 @@ OCP\JSON::checkAppEnabled('user_ldap');
OCP\JSON::callCheck(); OCP\JSON::callCheck();
$prefix = $_POST['ldap_serverconfig_chooser']; $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->setConfiguration($_POST);
$connection->saveConfiguration(); $connection->saveConfiguration();
OCP\JSON::success(); OCP\JSON::success();

View File

@ -28,7 +28,8 @@ OCP\JSON::callCheck();
$l=OC_L10N::get('user_ldap'); $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)) { if($connection->setConfiguration($_POST)) {
//Configuration is okay //Configuration is okay
if($connection->bind()) { if($connection->bind()) {

View File

@ -24,15 +24,15 @@
OCP\App::registerAdmin('user_ldap', 'settings'); OCP\App::registerAdmin('user_ldap', 'settings');
$configPrefixes = OCA\user_ldap\lib\Helper::getServerConfigurationPrefixes(true); $configPrefixes = OCA\user_ldap\lib\Helper::getServerConfigurationPrefixes(true);
$ldapWrapper = new OCA\user_ldap\lib\LDAP();
if(count($configPrefixes) === 1) { if(count($configPrefixes) === 1) {
$connector = new OCA\user_ldap\lib\Connection($configPrefixes[0]); $connector = new OCA\user_ldap\lib\Connection($ldapWrapper, $configPrefixes[0]);
$userBackend = new OCA\user_ldap\USER_LDAP(); $ldapAccess = new OCA\user_ldap\lib\Access($connector, $ldapWrapper);
$userBackend->setConnector($connector); $userBackend = new OCA\user_ldap\USER_LDAP($ldapAccess);
$groupBackend = new OCA\user_ldap\GROUP_LDAP(); $groupBackend = new OCA\user_ldap\GROUP_LDAP($ldapAccess);
$groupBackend->setConnector($connector);
} else { } else {
$userBackend = new OCA\user_ldap\User_Proxy($configPrefixes); $userBackend = new OCA\user_ldap\User_Proxy($configPrefixes, $ldapWrapper);
$groupBackend = new OCA\user_ldap\Group_Proxy($configPrefixes); $groupBackend = new OCA\user_ldap\Group_Proxy($configPrefixes, $ldapWrapper);
} }
if(count($configPrefixes) > 0) { if(count($configPrefixes) > 0) {

View File

@ -23,13 +23,16 @@
namespace OCA\user_ldap; 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; protected $enabled = false;
public function setConnector(lib\Connection &$connection) { public function __construct(Access $access) {
parent::setConnector($connection); parent::__construct($access);
$filter = $this->connection->ldapGroupFilter; $filter = $this->access->connection->ldapGroupFilter;
$gassoc = $this->connection->ldapGroupMemberAssocAttr; $gassoc = $this->access->connection->ldapGroupMemberAssocAttr;
if(!empty($filter) && !empty($gassoc)) { if(!empty($filter) && !empty($gassoc)) {
$this->enabled = true; $this->enabled = true;
} }
@ -47,30 +50,30 @@ class GROUP_LDAP extends lib\Access implements \OCP\GroupInterface {
if(!$this->enabled) { if(!$this->enabled) {
return false; return false;
} }
if($this->connection->isCached('inGroup'.$uid.':'.$gid)) { if($this->access->connection->isCached('inGroup'.$uid.':'.$gid)) {
return $this->connection->getFromCache('inGroup'.$uid.':'.$gid); return $this->access->connection->getFromCache('inGroup'.$uid.':'.$gid);
} }
$dn_user = $this->username2dn($uid); $dn_user = $this->access->username2dn($uid);
$dn_group = $this->groupname2dn($gid); $dn_group = $this->access->groupname2dn($gid);
// just in case // just in case
if(!$dn_group || !$dn_user) { if(!$dn_group || !$dn_user) {
$this->connection->writeToCache('inGroup'.$uid.':'.$gid, false); $this->access->connection->writeToCache('inGroup'.$uid.':'.$gid, false);
return false; return false;
} }
//usually, LDAP attributes are said to be case insensitive. But there are exceptions of course. //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) { if(!$members) {
$this->connection->writeToCache('inGroup'.$uid.':'.$gid, false); $this->access->connection->writeToCache('inGroup'.$uid.':'.$gid, false);
return false; return false;
} }
//extra work if we don't get back user DNs //extra work if we don't get back user DNs
//TODO: this can be done with one LDAP query //TODO: this can be done with one LDAP query
if(strtolower($this->connection->ldapGroupMemberAssocAttr) === 'memberuid') { if(strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') {
$dns = array(); $dns = array();
foreach($members as $mid) { foreach($members as $mid) {
$filter = str_replace('%uid', $mid, $this->connection->ldapLoginFilter); $filter = str_replace('%uid', $mid, $this->access->connection->ldapLoginFilter);
$ldap_users = $this->fetchListOfUsers($filter, 'dn'); $ldap_users = $this->access->fetchListOfUsers($filter, 'dn');
if(count($ldap_users) < 1) { if(count($ldap_users) < 1) {
continue; continue;
} }
@ -80,7 +83,7 @@ class GROUP_LDAP extends lib\Access implements \OCP\GroupInterface {
} }
$isInGroup = in_array($dn_user, $members); $isInGroup = in_array($dn_user, $members);
$this->connection->writeToCache('inGroup'.$uid.':'.$gid, $isInGroup); $this->access->connection->writeToCache('inGroup'.$uid.':'.$gid, $isInGroup);
return $isInGroup; return $isInGroup;
} }
@ -98,35 +101,35 @@ class GROUP_LDAP extends lib\Access implements \OCP\GroupInterface {
return array(); return array();
} }
$cacheKey = 'getUserGroups'.$uid; $cacheKey = 'getUserGroups'.$uid;
if($this->connection->isCached($cacheKey)) { if($this->access->connection->isCached($cacheKey)) {
return $this->connection->getFromCache($cacheKey); return $this->access->connection->getFromCache($cacheKey);
} }
$userDN = $this->username2dn($uid); $userDN = $this->access->username2dn($uid);
if(!$userDN) { if(!$userDN) {
$this->connection->writeToCache($cacheKey, array()); $this->access->connection->writeToCache($cacheKey, array());
return array(); return array();
} }
//uniqueMember takes DN, memberuid the uid, so we need to distinguish //uniqueMember takes DN, memberuid the uid, so we need to distinguish
if((strtolower($this->connection->ldapGroupMemberAssocAttr) === 'uniquemember') if((strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'uniquemember')
|| (strtolower($this->connection->ldapGroupMemberAssocAttr) === 'member') || (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'member')
) { ) {
$uid = $userDN; $uid = $userDN;
} else if(strtolower($this->connection->ldapGroupMemberAssocAttr) === 'memberuid') { } else if(strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') {
$result = $this->readAttribute($userDN, 'uid'); $result = $this->access->readAttribute($userDN, 'uid');
$uid = $result[0]; $uid = $result[0];
} else { } else {
// just in case // just in case
$uid = $userDN; $uid = $userDN;
} }
$filter = $this->combineFilterWithAnd(array( $filter = $this->access->combineFilterWithAnd(array(
$this->connection->ldapGroupFilter, $this->access->connection->ldapGroupFilter,
$this->connection->ldapGroupMemberAssocAttr.'='.$uid $this->access->connection->ldapGroupMemberAssocAttr.'='.$uid
)); ));
$groups = $this->fetchListOfGroups($filter, array($this->connection->ldapGroupDisplayName, 'dn')); $groups = $this->access->fetchListOfGroups($filter, array($this->access->connection->ldapGroupDisplayName, 'dn'));
$groups = array_unique($this->ownCloudGroupNames($groups), SORT_LOCALE_STRING); $groups = array_unique($this->access->ownCloudGroupNames($groups), SORT_LOCALE_STRING);
$this->connection->writeToCache($cacheKey, $groups); $this->access->connection->writeToCache($cacheKey, $groups);
return $groups; return $groups;
} }
@ -144,70 +147,70 @@ class GROUP_LDAP extends lib\Access implements \OCP\GroupInterface {
} }
$cachekey = 'usersInGroup-'.$gid.'-'.$search.'-'.$limit.'-'.$offset; $cachekey = 'usersInGroup-'.$gid.'-'.$search.'-'.$limit.'-'.$offset;
// check for cache of the exact query // check for cache of the exact query
$groupUsers = $this->connection->getFromCache($cachekey); $groupUsers = $this->access->connection->getFromCache($cachekey);
if(!is_null($groupUsers)) { if(!is_null($groupUsers)) {
return $groupUsers; return $groupUsers;
} }
// check for cache of the query without limit and offset // 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)) { if(!is_null($groupUsers)) {
$groupUsers = array_slice($groupUsers, $offset, $limit); $groupUsers = array_slice($groupUsers, $offset, $limit);
$this->connection->writeToCache($cachekey, $groupUsers); $this->access->connection->writeToCache($cachekey, $groupUsers);
return $groupUsers; return $groupUsers;
} }
if($limit === -1) { if($limit === -1) {
$limit = null; $limit = null;
} }
$groupDN = $this->groupname2dn($gid); $groupDN = $this->access->groupname2dn($gid);
if(!$groupDN) { if(!$groupDN) {
// group couldn't be found, return empty resultset // group couldn't be found, return empty resultset
$this->connection->writeToCache($cachekey, array()); $this->access->connection->writeToCache($cachekey, array());
return array(); return array();
} }
$members = $this->readAttribute($groupDN, $this->connection->ldapGroupMemberAssocAttr); $members = $this->access->readAttribute($groupDN, $this->access->connection->ldapGroupMemberAssocAttr);
if(!$members) { if(!$members) {
//in case users could not be retrieved, return empty resultset //in case users could not be retrieved, return empty resultset
$this->connection->writeToCache($cachekey, array()); $this->access->connection->writeToCache($cachekey, array());
return array(); return array();
} }
$groupUsers = array(); $groupUsers = array();
$isMemberUid = (strtolower($this->connection->ldapGroupMemberAssocAttr) === 'memberuid'); $isMemberUid = (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid');
foreach($members as $member) { foreach($members as $member) {
if($isMemberUid) { if($isMemberUid) {
//we got uids, need to get their DNs to 'tranlsate' them to usernames //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, \OCP\Util::mb_str_replace('%uid', $member,
$this->connection->ldapLoginFilter, 'UTF-8'), $this->access->connection->ldapLoginFilter, 'UTF-8'),
$this->getFilterPartForUserSearch($search) $this->access->getFilterPartForUserSearch($search)
)); ));
$ldap_users = $this->fetchListOfUsers($filter, 'dn'); $ldap_users = $this->access->fetchListOfUsers($filter, 'dn');
if(count($ldap_users) < 1) { if(count($ldap_users) < 1) {
continue; continue;
} }
$groupUsers[] = $this->dn2username($ldap_users[0]); $groupUsers[] = $this->access->dn2username($ldap_users[0]);
} else { } else {
//we got DNs, check if we need to filter by search or we can give back all of them //we got DNs, check if we need to filter by search or we can give back all of them
if(!empty($search)) { if(!empty($search)) {
if(!$this->readAttribute($member, if(!$this->access->readAttribute($member,
$this->connection->ldapUserDisplayName, $this->access->connection->ldapUserDisplayName,
$this->getFilterPartForUserSearch($search))) { $this->access->getFilterPartForUserSearch($search))) {
continue; continue;
} }
} }
// dn2username will also check if the users belong to the allowed base // 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; $groupUsers[] = $ocname;
} }
} }
} }
natsort($groupUsers); natsort($groupUsers);
$this->connection->writeToCache('usersInGroup-'.$gid.'-'.$search, $groupUsers); $this->access->connection->writeToCache('usersInGroup-'.$gid.'-'.$search, $groupUsers);
$groupUsers = array_slice($groupUsers, $offset, $limit); $groupUsers = array_slice($groupUsers, $offset, $limit);
$this->connection->writeToCache($cachekey, $groupUsers); $this->access->connection->writeToCache($cachekey, $groupUsers);
return $groupUsers; return $groupUsers;
} }
@ -245,7 +248,7 @@ class GROUP_LDAP extends lib\Access implements \OCP\GroupInterface {
//Check cache before driving unnecessary searches //Check cache before driving unnecessary searches
\OCP\Util::writeLog('user_ldap', 'getGroups '.$cachekey, \OCP\Util::DEBUG); \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)) { if(!is_null($ldap_groups)) {
return $ldap_groups; return $ldap_groups;
} }
@ -255,16 +258,16 @@ class GROUP_LDAP extends lib\Access implements \OCP\GroupInterface {
if($limit <= 0) { if($limit <= 0) {
$limit = null; $limit = null;
} }
$filter = $this->combineFilterWithAnd(array( $filter = $this->access->combineFilterWithAnd(array(
$this->connection->ldapGroupFilter, $this->access->connection->ldapGroupFilter,
$this->getFilterPartForGroupSearch($search) $this->access->getFilterPartForGroupSearch($search)
)); ));
\OCP\Util::writeLog('user_ldap', 'getGroups Filter '.$filter, \OCP\Util::DEBUG); \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); $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; return $ldap_groups;
} }
@ -278,25 +281,25 @@ class GROUP_LDAP extends lib\Access implements \OCP\GroupInterface {
* @return bool * @return bool
*/ */
public function groupExists($gid) { public function groupExists($gid) {
if($this->connection->isCached('groupExists'.$gid)) { if($this->access->connection->isCached('groupExists'.$gid)) {
return $this->connection->getFromCache('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. //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) { if(!$dn) {
$this->connection->writeToCache('groupExists'.$gid, false); $this->access->connection->writeToCache('groupExists'.$gid, false);
return false; return false;
} }
//if group really still exists, we will be able to read its objectclass //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)) { if(!$objcs || empty($objcs)) {
$this->connection->writeToCache('groupExists'.$gid, false); $this->access->connection->writeToCache('groupExists'.$gid, false);
return false; return false;
} }
$this->connection->writeToCache('groupExists'.$gid, true); $this->access->connection->writeToCache('groupExists'.$gid, true);
return true; return true;
} }

View File

@ -23,6 +23,8 @@
namespace OCA\user_ldap; namespace OCA\user_ldap;
use OCA\user_ldap\lib\ILDAPWrapper;
class Group_Proxy extends lib\Proxy implements \OCP\GroupInterface { class Group_Proxy extends lib\Proxy implements \OCP\GroupInterface {
private $backends = array(); private $backends = array();
private $refBackend = null; private $refBackend = null;
@ -31,12 +33,10 @@ class Group_Proxy extends lib\Proxy implements \OCP\GroupInterface {
* @brief Constructor * @brief Constructor
* @param $serverConfigPrefixes array containing the config Prefixes * @param $serverConfigPrefixes array containing the config Prefixes
*/ */
public function __construct($serverConfigPrefixes) { public function __construct($serverConfigPrefixes, ILDAPWrapper $ldap) {
parent::__construct(); parent::__construct($ldap);
foreach($serverConfigPrefixes as $configPrefix) { foreach($serverConfigPrefixes as $configPrefix) {
$this->backends[$configPrefix] = new \OCA\user_ldap\GROUP_LDAP(); $this->backends[$configPrefix] = new \OCA\user_ldap\GROUP_LDAP($this->getAccess($configPrefix));
$connector = $this->getConnector($configPrefix);
$this->backends[$configPrefix]->setConnector($connector);
if(is_null($this->refBackend)) { if(is_null($this->refBackend)) {
$this->refBackend = &$this->backends[$configPrefix]; $this->refBackend = &$this->backends[$configPrefix];
} }

View File

@ -23,12 +23,13 @@
namespace OCA\user_ldap\lib; namespace OCA\user_ldap\lib;
abstract class Access extends BackendBase { class Access extends LDAPUtility {
protected $connection; public $connection;
//never ever check this var directly, always use getPagedSearchResultState //never ever check this var directly, always use getPagedSearchResultState
protected $pagedSearchedSuccessful; protected $pagedSearchedSuccessful;
public function setConnector(Connection &$connection) { public function __construct(Connection $connection, ILDAPWrapper $ldap) {
parent::__construct($ldap);
$this->connection = $connection; $this->connection = $connection;
} }

View File

@ -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;
}
}

View File

@ -23,7 +23,7 @@
namespace OCA\user_ldap\lib; namespace OCA\user_ldap\lib;
class Connection extends BackendBase { class Connection extends LDAPUtility {
private $ldapConnectionRes = null; private $ldapConnectionRes = null;
private $configPrefix; private $configPrefix;
private $configID; private $configID;
@ -60,7 +60,7 @@ class Connection extends BackendBase {
'ldapQuotaDefault' => null, 'ldapQuotaDefault' => null,
'ldapEmailAttribute' => null, 'ldapEmailAttribute' => null,
'ldapCacheTTL' => null, 'ldapCacheTTL' => null,
'ldapUuidAttribute' => null, 'ldapUuidAttribute' => 'auto',
'ldapOverrideUuidAttribute' => null, 'ldapOverrideUuidAttribute' => null,
'ldapOverrideMainServer' => false, 'ldapOverrideMainServer' => false,
'ldapConfigurationActive' => 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 $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 * @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') { public function __construct(ILDAPWrapper $ldap, $configPrefix = '', $configID = 'user_ldap') {
parent::__construct(); parent::__construct($ldap);
$this->configPrefix = $configPrefix; $this->configPrefix = $configPrefix;
$this->configID = $configID; $this->configID = $configID;
$memcache = new \OC\Memcache\Factory(); $memcache = new \OC\Memcache\Factory();
@ -363,6 +363,14 @@ class Connection extends BackendBase {
&& $params[$parameter] === 'homeFolderNamingRule')) && $params[$parameter] === 'homeFolderNamingRule'))
&& !empty($value)) { && !empty($value)) {
$value = 'attr:'.$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])) { if(isset($this->config[$parameter])) {
$this->config[$parameter] = $value; $this->config[$parameter] = $value;
@ -433,10 +441,6 @@ class Connection extends BackendBase {
$config[$dbKey] = ''; $config[$dbKey] = '';
} }
continue; continue;
} else if((strpos($classKey, 'ldapBase') !== false)
|| (strpos($classKey, 'ldapAttributes') !== false)) {
$config[$dbKey] = implode("\n", $this->config[$classKey]);
continue;
} }
$config[$dbKey] = $this->config[$classKey]; $config[$dbKey] = $this->config[$classKey];
} }
@ -553,7 +557,7 @@ class Connection extends BackendBase {
* @returns an associative array with the default values. Keys are correspond * @returns an associative array with the default values. Keys are correspond
* to config-value entries in the database table * to config-value entries in the database table
*/ */
public function getDefaults() { static public function getDefaults() {
return array( return array(
'ldap_host' => '', 'ldap_host' => '',
'ldap_port' => '389', 'ldap_port' => '389',

View File

@ -139,13 +139,14 @@ class Jobs extends \OC\BackgroundJob\TimedJob {
return self::$groupBE; return self::$groupBE;
} }
$configPrefixes = Helper::getServerConfigurationPrefixes(true); $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 //avoid the proxy when there is only one LDAP server configured
$connector = new Connection($configPrefixes[0]); $connector = new OCA\user_ldap\lib\Connection($ldapWrapper, $configPrefixes[0]);
self::$groupBE = new \OCA\user_ldap\GROUP_LDAP(); $ldapAccess = new OCA\user_ldap\lib\Access($connector, $ldapWrapper);
self::$groupBE->setConnector($connector); self::$groupBE = new OCA\user_ldap\GROUP_LDAP($ldapAccess);
} else { } else {
self::$groupBE = new \OCA\user_ldap\Group_Proxy($configPrefixes); self::$groupBE = new \OCA\user_ldap\Group_Proxy($configPrefixes, $ldapWrapper);
} }
return self::$groupBE; return self::$groupBE;

View File

@ -149,6 +149,8 @@ class LDAP implements ILDAPWrapper {
&& $errorCode === -4) { && $errorCode === -4) {
} else if ($errorCode === 32) { } else if ($errorCode === 32) {
//for now //for now
} else if ($errorCode === 10) {
//referrals, we switch them off, but then there is AD :)
} else { } else {
throw new \Exception('LDAP error '.$errorMsg.' (' . throw new \Exception('LDAP error '.$errorMsg.' (' .
$errorCode.') after calling '.$this->curFunc. $errorCode.') after calling '.$this->curFunc.

View File

@ -1,7 +1,7 @@
<?php <?php
/** /**
* ownCloud LDAP BackendBase * ownCloud LDAP LDAPUtility
* *
* @author Arthur Schiwon * @author Arthur Schiwon
* @copyright 2013 Arthur Schiwon blizzz@owncloud.com * @copyright 2013 Arthur Schiwon blizzz@owncloud.com
@ -23,24 +23,14 @@
namespace OCA\user_ldap\lib; namespace OCA\user_ldap\lib;
abstract class BackendBase { abstract class LDAPUtility {
protected $ldap; protected $ldap;
public function __construct() {
$this->ldap = new LDAP();
}
/** /**
* @brief sets the LDAP Wrapper to be used * @brief constructor, make sure the subclasses call this one!
* * @param $ldapWrapper an instance of an ILDAPWrapper
* @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) { public function __construct(ILDAPWrapper $ldapWrapper) {
$this->ldap = $ldapWrapper; $this->ldap = $ldapWrapper;
return true;
} }
} }

View File

@ -23,26 +23,27 @@
namespace OCA\user_ldap\lib; namespace OCA\user_ldap\lib;
abstract class Proxy { use OCA\user_ldap\lib\Access;
static private $connectors = array();
public function __construct() { abstract class Proxy {
static private $accesses = array();
private $ldap = null;
public function __construct(ILDAPWrapper $ldap) {
$this->ldap = $ldap;
$this->cache = \OC_Cache::getGlobalCache(); $this->cache = \OC_Cache::getGlobalCache();
} }
private function addConnector($configPrefix) { private function addAccess($configPrefix) {
self::$connectors[$configPrefix] = new \OCA\user_ldap\lib\Connection($configPrefix); $connector = new Connection($this->ldap, $configPrefix);
self::$accesses[$configPrefix] = new Access($connector, $this->ldap);
} }
protected function getConnector($configPrefix) { protected function getAccess($configPrefix) {
if(!isset(self::$connectors[$configPrefix])) { if(!isset(self::$accesses[$configPrefix])) {
$this->addConnector($configPrefix); $this->addAccess($configPrefix);
} }
return self::$connectors[$configPrefix]; return self::$accesses[$configPrefix];
}
protected function getConnectors() {
return self::$connectors;
} }
protected function getUserCacheKey($uid) { protected function getUserCacheKey($uid) {

View File

@ -49,14 +49,9 @@ $tmpl->assign('serverConfigurationPrefixes', $prefixes);
$tmpl->assign('serverConfigurationHosts', $hosts); $tmpl->assign('serverConfigurationHosts', $hosts);
// assign default values // assign default values
if(!isset($ldap)) { $defaults = \OCA\user_ldap\lib\Connection::getDefaults();
$ldap = new \OCA\user_ldap\lib\Connection();
}
$defaults = $ldap->getDefaults();
foreach($defaults as $key => $default) { foreach($defaults as $key => $default) {
$tmpl->assign($key.'_default', $default); $tmpl->assign($key.'_default', $default);
} }
// $tmpl->assign();
return $tmpl->fetchPage(); return $tmpl->fetchPage();

View File

@ -25,37 +25,46 @@
namespace OCA\user_ldap; 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) { private function updateQuota($dn) {
$quota = null; $quota = null;
$quotaDefault = $this->connection->ldapQuotaDefault; $quotaDefault = $this->access->connection->ldapQuotaDefault;
$quotaAttribute = $this->connection->ldapQuotaAttribute; $quotaAttribute = $this->access->connection->ldapQuotaAttribute;
if(!empty($quotaDefault)) { if(!empty($quotaDefault)) {
$quota = $quotaDefault; $quota = $quotaDefault;
} }
if(!empty($quotaAttribute)) { if(!empty($quotaAttribute)) {
$aQuota = $this->readAttribute($dn, $quotaAttribute); $aQuota = $this->access->readAttribute($dn, $quotaAttribute);
if($aQuota && (count($aQuota) > 0)) { if($aQuota && (count($aQuota) > 0)) {
$quota = $aQuota[0]; $quota = $aQuota[0];
} }
} }
if(!is_null($quota)) { 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) { private function updateEmail($dn) {
$email = null; $email = null;
$emailAttribute = $this->connection->ldapEmailAttribute; $emailAttribute = $this->access->connection->ldapEmailAttribute;
if(!empty($emailAttribute)) { if(!empty($emailAttribute)) {
$aEmail = $this->readAttribute($dn, $emailAttribute); $aEmail = $this->access->readAttribute($dn, $emailAttribute);
if($aEmail && (count($aEmail) > 0)) { if($aEmail && (count($aEmail) > 0)) {
$email = $aEmail[0]; $email = $aEmail[0];
} }
if(!is_null($email)) { 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) { public function checkPassword($uid, $password) {
//find out dn of the user name //find out dn of the user name
$filter = \OCP\Util::mb_str_replace('%uid', $uid, $this->connection->ldapLoginFilter, 'UTF-8'); $filter = \OCP\Util::mb_str_replace('%uid', $uid, $this->access->connection->ldapLoginFilter, 'UTF-8');
$ldap_users = $this->fetchListOfUsers($filter, 'dn'); $ldap_users = $this->access->fetchListOfUsers($filter, 'dn');
if(count($ldap_users) < 1) { if(count($ldap_users) < 1) {
return false; return false;
} }
$dn = $ldap_users[0]; $dn = $ldap_users[0];
//do we have a username for him/her? //do we have a username for him/her?
$ocname = $this->dn2username($dn); $ocname = $this->access->dn2username($dn);
if($ocname) { if($ocname) {
//update some settings, if necessary //update some settings, if necessary
@ -86,7 +95,7 @@ class USER_LDAP extends lib\Access implements \OCP\UserInterface {
$this->updateEmail($dn); $this->updateEmail($dn);
//are the credentials OK? //are the credentials OK?
if(!$this->areCredentialsValid($dn, $password)) { if(!$this->access->areCredentialsValid($dn, $password)) {
return false; return false;
} }
@ -107,7 +116,7 @@ class USER_LDAP extends lib\Access implements \OCP\UserInterface {
$cachekey = 'getUsers-'.$search.'-'.$limit.'-'.$offset; $cachekey = 'getUsers-'.$search.'-'.$limit.'-'.$offset;
//check if users are cached, if so return //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)) { if(!is_null($ldap_users)) {
return $ldap_users; return $ldap_users;
} }
@ -117,21 +126,23 @@ class USER_LDAP extends lib\Access implements \OCP\UserInterface {
if($limit <= 0) { if($limit <= 0) {
$limit = null; $limit = null;
} }
$filter = $this->combineFilterWithAnd(array( $filter = $this->access->combineFilterWithAnd(array(
$this->connection->ldapUserFilter, $this->access->connection->ldapUserFilter,
$this->getFilterPartForUserSearch($search) $this->access->getFilterPartForUserSearch($search)
)); ));
\OCP\Util::writeLog('user_ldap', \OCP\Util::writeLog('user_ldap',
'getUsers: Options: search '.$search.' limit '.$limit.' offset '.$offset.' Filter: '.$filter, 'getUsers: Options: search '.$search.' limit '.$limit.' offset '.$offset.' Filter: '.$filter,
\OCP\Util::DEBUG); \OCP\Util::DEBUG);
//do the search and translate results to owncloud names //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); $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); \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; return $ldap_users;
} }
@ -141,22 +152,24 @@ class USER_LDAP extends lib\Access implements \OCP\UserInterface {
* @return boolean * @return boolean
*/ */
public function userExists($uid) { public function userExists($uid) {
if($this->connection->isCached('userExists'.$uid)) { if($this->access->connection->isCached('userExists'.$uid)) {
return $this->connection->getFromCache('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. //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) { 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; return false;
} }
//check if user really still exists by reading its entry //check if user really still exists by reading its entry
if(!is_array($this->readAttribute($dn, ''))) { if(!is_array($this->access->readAttribute($dn, ''))) {
$this->connection->writeToCache('userExists'.$uid, false); \OCP\Util::writeLog('user_ldap', 'LDAP says no user '.$dn, \OCP\Util::DEBUG);
$this->access->connection->writeToCache('userExists'.$uid, false);
return false; return false;
} }
$this->connection->writeToCache('userExists'.$uid, true); $this->access->connection->writeToCache('userExists'.$uid, true);
$this->updateQuota($dn); $this->updateQuota($dn);
return true; return true;
} }
@ -184,12 +197,13 @@ class USER_LDAP extends lib\Access implements \OCP\UserInterface {
} }
$cacheKey = 'getHome'.$uid; $cacheKey = 'getHome'.$uid;
if($this->connection->isCached($cacheKey)) { if($this->access->connection->isCached($cacheKey)) {
return $this->connection->getFromCache($cacheKey); return $this->access->connection->getFromCache($cacheKey);
} }
if(strpos($this->connection->homeFolderNamingRule, 'attr:') === 0) { if(strpos($this->access->connection->homeFolderNamingRule, 'attr:') === 0) {
$attr = substr($this->connection->homeFolderNamingRule, strlen('attr:')); $attr = substr($this->access->connection->homeFolderNamingRule, strlen('attr:'));
$homedir = $this->readAttribute($this->username2dn($uid), $attr); $homedir = $this->access->readAttribute(
$this->access->username2dn($uid), $attr);
if($homedir && isset($homedir[0])) { if($homedir && isset($homedir[0])) {
$path = $homedir[0]; $path = $homedir[0];
//if attribute's value is an absolute path take this, otherwise append it to data dir //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', $homedir = \OCP\Config::getSystemValue('datadirectory',
\OC::$SERVERROOT.'/data' ) . '/' . $homedir[0]; \OC::$SERVERROOT.'/data' ) . '/' . $homedir[0];
} }
$this->connection->writeToCache($cacheKey, $homedir); $this->access->connection->writeToCache($cacheKey, $homedir);
return $homedir; return $homedir;
} }
} }
//false will apply default behaviour as defined and done by OC_User //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; return false;
} }
@ -225,16 +239,16 @@ class USER_LDAP extends lib\Access implements \OCP\UserInterface {
} }
$cacheKey = 'getDisplayName'.$uid; $cacheKey = 'getDisplayName'.$uid;
if(!is_null($displayName = $this->connection->getFromCache($cacheKey))) { if(!is_null($displayName = $this->access->connection->getFromCache($cacheKey))) {
return $displayName; return $displayName;
} }
$displayName = $this->readAttribute( $displayName = $this->access->readAttribute(
$this->username2dn($uid), $this->access->username2dn($uid),
$this->connection->ldapUserDisplayName); $this->access->connection->ldapUserDisplayName);
if($displayName && (count($displayName) > 0)) { if($displayName && (count($displayName) > 0)) {
$this->connection->writeToCache($cacheKey, $displayName[0]); $this->access->connection->writeToCache($cacheKey, $displayName[0]);
return $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) { public function getDisplayNames($search = '', $limit = null, $offset = null) {
$cacheKey = 'getDisplayNames-'.$search.'-'.$limit.'-'.$offset; $cacheKey = 'getDisplayNames-'.$search.'-'.$limit.'-'.$offset;
if(!is_null($displayNames = $this->connection->getFromCache($cacheKey))) { if(!is_null($displayNames = $this->access->connection->getFromCache($cacheKey))) {
return $displayNames; return $displayNames;
} }
@ -258,7 +272,7 @@ class USER_LDAP extends lib\Access implements \OCP\UserInterface {
foreach ($users as $user) { foreach ($users as $user) {
$displayNames[$user] = $this->getDisplayName($user); $displayNames[$user] = $this->getDisplayName($user);
} }
$this->connection->writeToCache($cacheKey, $displayNames); $this->access->connection->writeToCache($cacheKey, $displayNames);
return $displayNames; return $displayNames;
} }

View File

@ -23,6 +23,8 @@
namespace OCA\user_ldap; namespace OCA\user_ldap;
use OCA\user_ldap\lib\ILDAPWrapper;
class User_Proxy extends lib\Proxy implements \OCP\UserInterface { class User_Proxy extends lib\Proxy implements \OCP\UserInterface {
private $backends = array(); private $backends = array();
private $refBackend = null; private $refBackend = null;
@ -31,12 +33,10 @@ class User_Proxy extends lib\Proxy implements \OCP\UserInterface {
* @brief Constructor * @brief Constructor
* @param $serverConfigPrefixes array containing the config Prefixes * @param $serverConfigPrefixes array containing the config Prefixes
*/ */
public function __construct($serverConfigPrefixes) { public function __construct($serverConfigPrefixes, ILDAPWrapper $ldap) {
parent::__construct(); parent::__construct($ldap);
foreach($serverConfigPrefixes as $configPrefix) { foreach($serverConfigPrefixes as $configPrefix) {
$this->backends[$configPrefix] = new \OCA\user_ldap\USER_LDAP(); $this->backends[$configPrefix] = new \OCA\user_ldap\USER_LDAP($this->getAccess($configPrefix));
$connector = $this->getConnector($configPrefix);
$this->backends[$configPrefix]->setConnector($connector);
if(is_null($this->refBackend)) { if(is_null($this->refBackend)) {
$this->refBackend = &$this->backends[$configPrefix]; $this->refBackend = &$this->backends[$configPrefix];
} }