summaryrefslogtreecommitdiffstats
path: root/apps/user_ldap
diff options
context:
space:
mode:
authorArthur Schiwon <blizzz@owncloud.com>2012-06-07 18:55:32 +0200
committerArthur Schiwon <blizzz@owncloud.com>2012-06-07 18:55:32 +0200
commitc2f557f1dd89b2ca5a612d7cbf7ab940ea8d1b68 (patch)
tree8e4276ee6df572862a9a1686063280f3338bf3fd /apps/user_ldap
parent66265984352850c4045fb5e921ebc1322aa471cd (diff)
downloadnextcloud-server-c2f557f1dd89b2ca5a612d7cbf7ab940ea8d1b68.tar.gz
nextcloud-server-c2f557f1dd89b2ca5a612d7cbf7ab940ea8d1b68.zip
LDAP: cache the results, reduce LDAP searches
Diffstat (limited to 'apps/user_ldap')
-rw-r--r--apps/user_ldap/group_ldap.php40
-rw-r--r--apps/user_ldap/user_ldap.php13
2 files changed, 39 insertions, 14 deletions
diff --git a/apps/user_ldap/group_ldap.php b/apps/user_ldap/group_ldap.php
index baca1d32bad..62e7c8ca6bc 100644
--- a/apps/user_ldap/group_ldap.php
+++ b/apps/user_ldap/group_ldap.php
@@ -27,6 +27,11 @@ class OC_GROUP_LDAP extends OC_Group_Backend {
protected $ldapGroupMemberAssocAttr;
protected $configured = false;
+ protected $_group_user = array();
+ protected $_user_groups = array();
+ protected $_group_users = array();
+ protected $_groups = array();
+
public function __construct() {
$this->ldapGroupFilter = OCP\Config::getAppValue('user_ldap', 'ldap_group_filter', '(objectClass=posixGroup)');
$this->ldapGroupMemberAssocAttr = OCP\Config::getAppValue('user_ldap', 'ldap_group_member_assoc_attribute', 'uniqueMember');
@@ -48,6 +53,9 @@ class OC_GROUP_LDAP extends OC_Group_Backend {
if(!$this->configured) {
return false;
}
+ if(isset($this->_group_user[$gid][$uid])) {
+ return $this->_group_user[$gid][$uid];
+ }
$dn_user = OC_LDAP::username2dn($uid);
$dn_group = OC_LDAP::groupname2dn($gid);
// just in case
@@ -64,8 +72,8 @@ class OC_GROUP_LDAP extends OC_Group_Backend {
//TODO: this can be done with one LDAP query
if(strtolower($this->ldapGroupMemberAssocAttr) == 'memberuid') {
$dns = array();
- foreach($members as $uid) {
- $filter = str_replace('%uid', $uid, OC_LDAP::conf('ldapLoginFilter'));
+ foreach($members as $mid) {
+ $filter = str_replace('%uid', $mid, OC_LDAP::conf('ldapLoginFilter'));
$ldap_users = OC_LDAP::fetchListOfUsers($filter, 'dn');
if(count($ldap_users) < 1) {
continue;
@@ -75,7 +83,8 @@ class OC_GROUP_LDAP extends OC_Group_Backend {
$members = $dns;
}
- return in_array($dn_user, $members);
+ $this->_group_user[$gid][$uid] = in_array($dn_user, $members);
+ return $this->_group_user[$gid][$uid];
}
/**
@@ -90,8 +99,12 @@ class OC_GROUP_LDAP extends OC_Group_Backend {
if(!$this->configured) {
return array();
}
+ if(isset($this->_user_groups[$uid])) {
+ return $this->_user_groups[$uid];
+ }
$userDN = OC_LDAP::username2dn($uid);
if(!$userDN) {
+ $this->_user_groups[$uid] = array();
return array();
}
@@ -112,9 +125,9 @@ class OC_GROUP_LDAP extends OC_Group_Backend {
$this->ldapGroupMemberAssocAttr.'='.$uid
));
$groups = OC_LDAP::fetchListOfGroups($filter, array(OC_LDAP::conf('ldapGroupDisplayName'),'dn'));
- $userGroups = OC_LDAP::ownCloudGroupNames($groups);
+ $this->_user_groups[$uid] = array_unique(OC_LDAP::ownCloudGroupNames($groups), SORT_LOCALE_STRING);
- return array_unique($userGroups, SORT_LOCALE_STRING);
+ return $this->_user_groups[$uid];
}
/**
@@ -125,14 +138,19 @@ class OC_GROUP_LDAP extends OC_Group_Backend {
if(!$this->configured) {
return array();
}
+ if(isset($this->_group_users[$gid])) {
+ return $this->_group_users[$gid];
+ }
$groupDN = OC_LDAP::groupname2dn($gid);
if(!$groupDN) {
+ $this->_group_users[$gid] = array();
return array();
}
$members = OC_LDAP::readAttribute($groupDN, $this->ldapGroupMemberAssocAttr);
if(!$members) {
+ $this->_group_users[$gid] = array();
return array();
}
@@ -154,7 +172,8 @@ class OC_GROUP_LDAP extends OC_Group_Backend {
if(!$isMemberUid) {
$result = array_intersect($result, OCP\User::getUsers());
}
- return array_unique($result, SORT_LOCALE_STRING);
+ $this->_group_users[$gid] = array_unique($result, SORT_LOCALE_STRING);
+ return $this->_group_users[$gid];
}
/**
@@ -167,10 +186,11 @@ class OC_GROUP_LDAP extends OC_Group_Backend {
if(!$this->configured) {
return array();
}
-
- $ldap_groups = OC_LDAP::fetchListOfGroups($this->ldapGroupFilter, array(OC_LDAP::conf('ldapGroupDisplayName'), 'dn'));
- $groups = OC_LDAP::ownCloudGroupNames($ldap_groups);
- return $groups;
+ if(is_null($this->_groups)) {
+ $ldap_groups = OC_LDAP::fetchListOfGroups($this->ldapGroupFilter, array(OC_LDAP::conf('ldapGroupDisplayName'), 'dn'));
+ $this->_groups = OC_LDAP::ownCloudGroupNames($ldap_groups);
+ }
+ return $this->groups;
}
/**
diff --git a/apps/user_ldap/user_ldap.php b/apps/user_ldap/user_ldap.php
index ba66c7a9ca8..9281aebe81d 100644
--- a/apps/user_ldap/user_ldap.php
+++ b/apps/user_ldap/user_ldap.php
@@ -34,6 +34,9 @@ class OC_USER_LDAP extends OC_User_Backend {
// will be retrieved from LDAP server
protected $ldap_dc = false;
+ // cache getUsers()
+ protected $_users = null;
+
public function __construct() {
$this->ldapUserFilter = OCP\Config::getAppValue('user_ldap', 'ldap_userlist_filter', '(objectClass=posixAccount)');
$this->ldapQuotaAttribute = OCP\Config::getAppValue('user_ldap', 'ldap_quota_attr', '');
@@ -108,9 +111,11 @@ class OC_USER_LDAP extends OC_User_Backend {
* Get a list of all users.
*/
public function getUsers(){
- $ldap_users = OC_LDAP::fetchListOfUsers($this->ldapUserFilter, array(OC_LDAP::conf('ldapUserDisplayName'), 'dn'));
- $users = OC_LDAP::ownCloudUserNames($ldap_users);
- return $users;
+ if(is_null($this->_users)) {
+ $ldap_users = OC_LDAP::fetchListOfUsers($this->ldapUserFilter, array(OC_LDAP::conf('ldapUserDisplayName'), 'dn'));
+ $this->_users = OC_LDAP::ownCloudUserNames($ldap_users);
+ }
+ return $this->_users;
}
/**
@@ -119,7 +124,7 @@ class OC_USER_LDAP extends OC_User_Backend {
* @return boolean
*/
public function userExists($uid){
- return in_array($uid, self::getUsers());
+ return in_array($uid, $this->getUsers());
}
}