3 commits - lib/api lib/Auth lib/kolab_api_service.php
Aleksander Machniak
machniak at kolabsys.com
Wed Mar 14 18:59:26 CET 2012
lib/Auth/LDAP.php | 132 +++++++++++++++---------------
lib/api/kolab_api_service_group.php | 2
lib/api/kolab_api_service_group_types.php | 23 +----
lib/kolab_api_service.php | 57 ++++++++++--
4 files changed, 118 insertions(+), 96 deletions(-)
New commits:
commit 30e056b4d04a2f6af5031d353aac9f412420a0e3
Author: Aleksander Machniak <machniak at kolabsys.com>
Date: Wed Mar 14 18:58:49 2012 +0100
More input checks in user_find_by_attribute() to skip "empty" searches
diff --git a/lib/Auth/LDAP.php b/lib/Auth/LDAP.php
index 5e360ed..99ab0f1 100644
--- a/lib/Auth/LDAP.php
+++ b/lib/Auth/LDAP.php
@@ -110,7 +110,7 @@ class LDAP
$root_dn = "";
}
- if (($bind_ok = $this->_bind($user_dn, $password)) == TRUE) {
+ if (($bind_ok = $this->_bind($user_dn, $password)) == true) {
$this->_unbind();
if (isset($_SESSION['user'])) {
@@ -123,11 +123,11 @@ class LDAP
error_log("Successfully bound with User DN: " . $user_dn . " but not saving it to the session");
}
- return TRUE;
+ return true;
}
else {
error_log("LDAP Error: " . $this->_errstr());
- return FALSE;
+ return false;
}
}
@@ -148,7 +148,7 @@ class LDAP
return $this->_delete($dn);
}
- public function domain_add($domain, $domain_alias = FALSE, $prepopulate = TRUE)
+ public function domain_add($domain, $domain_alias = false, $prepopulate = true)
{
// Apply some routines for access control to this function here.
if ($domain_alias) {
@@ -164,7 +164,7 @@ class LDAP
return $this->_ldap->domain_exists($domain);
}
- public function domain_list($rev_sort = FALSE)
+ public function domain_list($rev_sort = false)
{
return $this->_ldap->domain_list($rev_sort);
}
@@ -178,7 +178,7 @@ class LDAP
$conf = Conf::get_instance();
if ($domain == '') {
- return FALSE;
+ return false;
}
error_log("Searching for domain $domain");
@@ -187,14 +187,14 @@ class LDAP
error_log("From domain to root dn");
- if (($this->_bind($conf->get('ldap', 'bind_dn'), $conf->get('ldap', 'bind_pw'))) == FALSE) {
+ if (($this->_bind($conf->get('ldap', 'bind_dn'), $conf->get('ldap', 'bind_pw'))) == false) {
error_log("WARNING: Invalid Service bind credentials supplied");
$this->_bind($conf->manager_bind_dn, $conf->manager_bind_pw);
}
- if (($results = ldap_search($this->_connection, $conf->get('domain_base_dn'), '(associatedDomain=' . $domain . ')')) == FALSE) {
+ if (($results = ldap_search($this->_connection, $conf->get('domain_base_dn'), '(associatedDomain=' . $domain . ')')) == false) {
error_log("No results?");
- return FALSE;
+ return false;
}
$domain = ldap_first_entry($this->_connection, $results);
@@ -259,7 +259,7 @@ class LDAP
}
if (!$group_dn) {
- return FALSE;
+ return false;
}
return $this->search($group_dn);
@@ -277,7 +277,7 @@ class LDAP
}
if (!$group_dn) {
- return FALSE;
+ return false;
}
return $this->_list_group_members($group_dn);
@@ -418,7 +418,7 @@ class LDAP
}
if (!$user_dn) {
- return FALSE;
+ return false;
}
return $this->delete($user_dn);
@@ -426,8 +426,12 @@ class LDAP
public function user_find_by_attribute($attribute)
{
- if (!is_array($attribute) || count($attribute) > 1) {
- return FALSE;
+ if (empty($attribute) || !is_array($attribute) || count($attribute) > 1) {
+ return false;
+ }
+
+ if (empty($attribute[key($attribute)])) {
+ return false;
}
$filter = "(&";
@@ -448,7 +452,7 @@ class LDAP
}
else {
error_log("No result");
- return FALSE;
+ return false;
}
}
@@ -465,7 +469,7 @@ class LDAP
}
if (!$user_dn) {
- return FALSE;
+ return false;
}
return $this->search($user_dn);
@@ -561,7 +565,7 @@ class LDAP
Deprecated, use domain_root_dn()
*/
- public function user_type_attribute_filter($type = FALSE)
+ public function user_type_attribute_filter($type = false)
{
global $conf;
@@ -582,7 +586,7 @@ class LDAP
return $attributes_filter;
}
- public function user_type_search_filter($type = FALSE)
+ public function user_type_search_filter($type = false)
{
global $conf;
@@ -640,12 +644,12 @@ class LDAP
$this->_connect();
$this->bind($_SESSION['user']->user_bind_dn, $_SESSION['user']->user_bind_pw);
- if (($add_result = ldap_add($this->_connection, $entry_dn, $attributes)) == FALSE) {
+ if (($add_result = ldap_add($this->_connection, $entry_dn, $attributes)) == false) {
// Issue warning
- return FALSE;
+ return false;
}
- return TRUE;
+ return true;
}
/**
@@ -659,16 +663,16 @@ class LDAP
error_log("->_bind() Binding with $dn");
if (!$dn || !$pw) {
- return FALSE;
+ return false;
}
- if (($bind_ok = ldap_bind($this->_connection, $dn, $pw)) == FALSE) {
+ if (($bind_ok = ldap_bind($this->_connection, $dn, $pw)) == false) {
error_log("LDAP Error: " . $this->_errstr());
// Issue error message
- return FALSE;
+ return false;
}
else {
- return TRUE;
+ return true;
}
}
@@ -677,13 +681,13 @@ class LDAP
*/
private function _connect()
{
- if ($this->_connection == FALSE) {
+ if ($this->_connection == false) {
// TODO: Debug logging
error_log("Connecting to " . $this->_ldap_server . " on port " . $this->_ldap_port);
$connection = ldap_connect($this->_ldap_server, $this->_ldap_port);
- if ($connection == FALSE) {
- $this->_connection = FALSE;
+ if ($connection == false) {
+ $this->_connection = false;
// TODO: Debug logging
error_log("Not connected: " . ldap_err2str() . "(no.) " . ldap_errno());
}
@@ -707,12 +711,12 @@ class LDAP
$this->_connect();
$this->bind($_SESSION['user']->user_bind_dn, $_SESSION['user']->user_bind_pw);
- if (($delete_result = ldap_delete($this->_connection, $entry_dn)) == FALSE) {
+ if (($delete_result = ldap_delete($this->_connection, $entry_dn)) == false) {
// Issue warning
- return FALSE;
+ return false;
}
else {
- return TRUE;
+ return true;
}
}
@@ -721,19 +725,19 @@ class LDAP
*/
private function _disconnect()
{
- if ($this->_connection == FALSE) {
- return TRUE;
+ if ($this->_connection == false) {
+ return true;
}
- if (($result = ldap_close($this->_connection)) == TRUE) {
- $this->_connection = FALSE;
- return TRUE;
+ if (($result = ldap_close($this->_connection)) == true) {
+ $this->_connection = false;
+ return true;
}
// Issue a warning
- $this->_connection = FALSE;
- $this->_ldap = FALSE;
- return FALSE;
+ $this->_connection = false;
+ $this->_ldap = false;
+ return false;
}
/**
@@ -761,11 +765,11 @@ class LDAP
{
$ldap_entries = array( "count" => 0 );
- if (($ldap_list = @ldap_list($this->_connection, $base_dn, $filter)) == FALSE) {
+ if (($ldap_list = @ldap_list($this->_connection, $base_dn, $filter)) == false) {
//message("LDAP Error: Could not search " . $base_dn . ": " . $this->_errstr() );
}
else {
- if (($ldap_entries = @ldap_get_entries($this->_connection, $ldap_list)) == FALSE) {
+ if (($ldap_entries = @ldap_get_entries($this->_connection, $ldap_list)) == false) {
//message("LDAP Error: No entries for " . $filter . " in " . $base_dn . ": " . $this->_errstr());
}
}
@@ -781,14 +785,14 @@ class LDAP
error_log("Searching with user " . $_SESSION['user']->user_bind_dn);
$this->_bind($_SESSION['user']->user_bind_dn, $_SESSION['user']->user_bind_pw);
- if (($search_results = @ldap_search($this->_connection, $base_dn, $search_filter, $attributes)) == FALSE) {
+ if (($search_results = @ldap_search($this->_connection, $base_dn, $search_filter, $attributes)) == false) {
#message("Could not search in " . __METHOD__ . " in " . __FILE__ . " on line " . __LINE__ . ": " . $this->_errstr());
- return FALSE;
+ return false;
}
- if (($entries = ldap_get_entries($this->_connection, $search_results)) == FALSE) {
+ if (($entries = ldap_get_entries($this->_connection, $search_results)) == false) {
#message("Could not get the results of the search: " . $this->_errstr());
- return FALSE;
+ return false;
}
return $entries;
@@ -797,11 +801,11 @@ class LDAP
/**
* Shortcut to ldap_unbind()
*/
- private function _unbind($yes = FALSE, $really = FALSE)
+ private function _unbind($yes = false, $really = false)
{
if ($yes && $really) {
ldap_unbind($this->_connection);
- $this->_connection = FALSE;
+ $this->_connection = false;
}
else {
// What?
@@ -810,7 +814,7 @@ class LDAP
// - in case of fail, bind as user
}
- return TRUE;
+ return true;
}
/*
@@ -829,31 +833,31 @@ class LDAP
private function _probe_root_dn($entry_root_dn)
{
error_log("Running for entry root dn: " . $entry_root_dn);
- if (($tmp_connection = ldap_connect($this->_ldap_server)) == FALSE) {
+ if (($tmp_connection = ldap_connect($this->_ldap_server)) == false) {
//message("LDAP Error: " . $this->_errstr());
- return FALSE;
+ return false;
}
error_log("User DN: " . $_SESSION['user']->user_bind_dn);
- if (($bind_success = ldap_bind($tmp_connection, $_SESSION['user']->user_bind_dn, $_SESSION['user']->user_bind_pw)) == FALSE) {
+ if (($bind_success = ldap_bind($tmp_connection, $_SESSION['user']->user_bind_dn, $_SESSION['user']->user_bind_pw)) == false) {
//message("LDAP Error: " . $this->_errstr());
- return FALSE;
+ return false;
}
- if (($list_success = ldap_list($tmp_connection, $entry_root_dn, '(objectClass=*)', array('*', 'aci'))) == FALSE) {
+ if (($list_success = ldap_list($tmp_connection, $entry_root_dn, '(objectClass=*)', array('*', 'aci'))) == false) {
#message("LDAP Error: " . $this->_errstr());
- return FALSE;
+ return false;
}
# print_r(ldap_get_entries($tmp_connection, $list_success));
/*
if (ldap_count_entries($tmp_connection, $list_success) == 0) {
echo "<li>Listed things, but got no results";
- return FALSE;
+ return false;
}
*/
- return TRUE;
+ return true;
}
/**
@@ -918,18 +922,18 @@ class LDAP
$this->_connect();
- if (($this->_bind($this->conf->get('bind_dn'), $this->conf->get('bind_pw'))) == FALSE) {
+ if (($this->_bind($this->conf->get('bind_dn'), $this->conf->get('bind_pw'))) == false) {
$this->_bind($this->conf->get('manager_bind_dn'), $this->conf->get('manager_bind_pw'));
}
$search_results = ldap_search($this->_connection, $root_dn, $search_filter);
if (ldap_count_entries($this->_connection, $search_results) == 0) {
- return FALSE;
+ return false;
}
- if (($first_entry = ldap_first_entry($this->_connection, $search_results)) == FALSE) {
- return FALSE;
+ if (($first_entry = ldap_first_entry($this->_connection, $search_results)) == false) {
+ return false;
}
$group_dn = ldap_get_dn($this->_connection, $first_entry);
@@ -942,7 +946,7 @@ class LDAP
$this->_connect();
- if (($this->_bind($this->conf->get('bind_dn'), $this->conf->get('bind_pw'))) == FALSE) {
+ if (($this->_bind($this->conf->get('bind_dn'), $this->conf->get('bind_pw'))) == false) {
//message("WARNING: Invalid Service bind credentials supplied");
$this->_bind($this->conf->get('manager_bind_dn'), $this->conf->get('manager_bind_pw'));
}
@@ -951,11 +955,11 @@ class LDAP
if (ldap_count_entries($this->_connection, $search_results) == 0) {
//message("No entries found for the user dn in " . __METHOD__);
- return FALSE;
+ return false;
}
- if (($first_entry = ldap_first_entry($this->_connection, $search_results)) == FALSE) {
- return FALSE;
+ if (($first_entry = ldap_first_entry($this->_connection, $search_results)) == false) {
+ return false;
}
$user_dn = ldap_get_dn($this->_connection, $first_entry);
commit 04b3d9d35f05e0838e9a59ddebb04afe716873ca
Author: Aleksander Machniak <machniak at kolabsys.com>
Date: Wed Mar 14 18:51:10 2012 +0100
Fix typo in user_type_attributes()
diff --git a/lib/kolab_api_service.php b/lib/kolab_api_service.php
index 7316643..1dcd2a0 100644
--- a/lib/kolab_api_service.php
+++ b/lib/kolab_api_service.php
@@ -72,7 +72,7 @@ abstract class kolab_api_service
throw new Exception($this->controller->translate('user.invalidtypeid'), 35);
}
- return $user_types[$user_id]['attributes'];
+ return $user_types[$type_id]['attributes'];
}
/**
commit 501df8ab14f67c0097661e992997901e76c38bc7
Author: Aleksander Machniak <machniak at kolabsys.com>
Date: Wed Mar 14 18:40:03 2012 +0100
Improve performance by caching group/user types in memory
diff --git a/lib/api/kolab_api_service_group.php b/lib/api/kolab_api_service_group.php
index 7496202..9cf7b22 100644
--- a/lib/api/kolab_api_service_group.php
+++ b/lib/api/kolab_api_service_group.php
@@ -40,7 +40,7 @@ class kolab_api_service_group extends kolab_api_service
public function group_add($getdata, $postdata)
{
- $gta = $this->user_type_attributes($postdata['group_type_id']);
+ $gta = $this->group_type_attributes($postdata['group_type_id']);
$group_attributes = array();
if (isset($gta['form_fields'])) {
diff --git a/lib/api/kolab_api_service_group_types.php b/lib/api/kolab_api_service_group_types.php
index d873baf..3ee0b83 100644
--- a/lib/api/kolab_api_service_group_types.php
+++ b/lib/api/kolab_api_service_group_types.php
@@ -37,24 +37,11 @@ class kolab_api_service_group_types extends kolab_api_service
public function group_types_list($get, $post)
{
- $sql_result = $this->db->query("SELECT * FROM group_types");
- $group_types = array();
+ $group_types = $this->group_types();
- while ($row = $this->db->fetch_assoc($sql_result)) {
- $group_types[$row['id']] = array();
-
- foreach ($row as $key => $value) {
- if ($key != "id") {
- if ($key == "attributes") {
- $group_types[$row['id']][$key] = json_decode(unserialize($value), true);
- }
- else {
- $group_types[$row['id']][$key] = $value;
- }
- }
- }
- }
-
- return $group_types;
+ return array(
+ 'list' => $group_types,
+ 'count' => count($group_types),
+ );
}
}
diff --git a/lib/kolab_api_service.php b/lib/kolab_api_service.php
index 8630cbe..7316643 100644
--- a/lib/kolab_api_service.php
+++ b/lib/kolab_api_service.php
@@ -30,6 +30,7 @@ abstract class kolab_api_service
{
protected $controller;
protected $db;
+ protected $cache = array();
/**
* Class constructor.
@@ -65,16 +66,13 @@ abstract class kolab_api_service
return array();
}
- $sql_result = $this->db->query("SELECT attributes FROM user_types WHERE id = ?", $type_id);
- $user_type = $this->db->fetch_assoc($sql_result);
+ $user_types = $this->user_types();
- if (empty($user_type)) {
+ if (empty($user_types[$type_id])) {
throw new Exception($this->controller->translate('user.invalidtypeid'), 35);
}
- $uta = json_decode(unserialize($user_type['attributes']), true);
-
- return $uta;
+ return $user_types[$user_id]['attributes'];
}
/**
@@ -95,16 +93,13 @@ abstract class kolab_api_service
return array();
}
- $sql_result = $this->db->query("SELECT attributes FROM group_types WHERE id = ?", $type_id);
- $group_type = $this->db->fetch_assoc($sql_result);
+ $group_types = $this->group_types();
- if (empty($group_type)) {
+ if (empty($group_types[$type_id])) {
throw new Exception($this->controller->translate('group.invalidtypeid'), 35);
}
- $uta = json_decode(unserialize($group_type['attributes']), true);
-
- return $uta;
+ return $group_types[$type_id]['attributes'];
}
/**
@@ -160,6 +155,10 @@ abstract class kolab_api_service
*/
protected function user_types()
{
+ if (!empty($this->cache['user_types'])) {
+ return $this->cache['user_types'];
+ }
+
$sql_result = $this->db->query("SELECT * FROM user_types");
$user_types = array();
@@ -178,6 +177,38 @@ abstract class kolab_api_service
}
}
- return $user_types;
+ return $this->cache['user_types'] = $user_types;
+ }
+
+ /**
+ * Returns group types definitions.
+ *
+ * @return array Group types.
+ */
+ protected function group_types()
+ {
+ if (!empty($this->cache['group_types'])) {
+ return $this->cache['group_types'];
+ }
+
+ $sql_result = $this->db->query("SELECT * FROM group_types");
+ $group_types = array();
+
+ while ($row = $this->db->fetch_assoc($sql_result)) {
+ $group_types[$row['id']] = array();
+
+ foreach ($row as $key => $value) {
+ if ($key != "id") {
+ if ($key == "attributes") {
+ $group_types[$row['id']][$key] = json_decode(unserialize($value), true);
+ }
+ else {
+ $group_types[$row['id']][$key] = $value;
+ }
+ }
+ }
+ }
+
+ return $this->cache['group_types'] = $group_types;
}
}
More information about the commits
mailing list