From: Nicolas Boisselier Date: Mon, 5 Feb 2018 21:16:29 +0000 (+0000) Subject: lib/php/db/types/ldap.php X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=cdd380448eb43edeb9439b61217a5af298f95cb6;p=nb.git lib/php/db/types/ldap.php --- diff --git a/etc/dbq/ldap.php b/etc/dbq/ldap.php index edebe263..af04172b 100644 --- a/etc/dbq/ldap.php +++ b/etc/dbq/ldap.php @@ -40,14 +40,15 @@ $GLOBALS['DBQ_LDAP'] = [ 'base' => $base, ]; #'(&('.$search['filter'].')(userPassword=*))' -if (preg_match('/^([^,]+),(.*?)$/',$user,$m) -) { + +if (preg_match('/^([^,]+),(.*?)$/',$user,$m)) { $filter = $m[1]; } else { $filter = 'cn='.Http::user(); } + $filter = '(&('.$filter.')(userPassword=*))'; -$DBQ['ldap2'] = $GLOBALS['DBQ_LDAP'] + [ +$DBQ['ldap'] = $GLOBALS['DBQ_LDAP'] + [ 'type' => 'ldap', 'tables' => [ $filter => [ diff --git a/lib/php/db/types/ldap.php b/lib/php/db/types/ldap.php index 6687913d..ae37ec1e 100644 --- a/lib/php/db/types/ldap.php +++ b/lib/php/db/types/ldap.php @@ -8,10 +8,18 @@ $DB_TYPES['ldap'] = [ 'quote' => function($v) { return $v; }, 'nosql' => true, #'prepare' => function($sql) { return $this->ldap->prepare($sql); }, - 'prepare' => function($table,$sql) { $table->db()->ldap->prepare($sql); return $table->db()->ldap; }, + 'prepare' => function($table,$sql) { + $fields = (empty($table->fields_only) ? array_keys($table->fields()) : $table->fields_only); + #foreach (array_keys($table->fields) as $k) { if (!in_array($k,$fields)) unset($table->fields[$k]); } + $table->db()->ldap->prepare($sql,['attrs'=>$fields]); return $table->db()->ldap; + }, 'fetch' => function($table) { - $row = $table->db()->ldap->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT); - $row = $table->db()->ldap->ar_filter_keys($row,array_keys($table->fields())); + #debug($table->fields_only); + #debug($table->fields_only); + $row = $table->db()->ldap->fetch(); + if (!$row) return []; + $fields = (empty($table->fields_only) ? array_keys($table->fields()) : $table->fields_only); + $row = $table->db()->ldap->ar_filter_keys($row,$fields); return $row; }, '_connect' => function($db) { diff --git a/lib/php/ldap.php b/lib/php/ldap.php index 75d00916..6e687358 100644 --- a/lib/php/ldap.php +++ b/lib/php/ldap.php @@ -26,9 +26,12 @@ class Ldap { public static function ar_filter_keys($ar,$keys) { $new = []; - foreach ($keys as $k) { - if (isset($ar[$k])) $new[$k] = $ar[$k]; + foreach ($ar as $k => $v) { + if (in_array($k,$keys)) $new[$k] = $v; } +# NB 05.02.18 foreach ($keys as $k) { +# NB 05.02.18 if (isset($ar[$k])) $new[$k] = $ar[$k]; +# NB 05.02.18 } return $new; } @@ -155,7 +158,7 @@ class Ldap { return $this->read; } - public function fetch($type='',$opt='') { + public function fetch($type='',$o='') { static $count = 0; if ($type === null) { $count = 0; diff --git a/www/dbq/dbq.php b/www/dbq/dbq.php index f6ef67ff..4e74eda4 100644 --- a/www/dbq/dbq.php +++ b/www/dbq/dbq.php @@ -1150,7 +1150,7 @@ EOF; } $this->page($types); - } elseif ($action == 'ldap') { + } elseif ($action == 'ldap2') { $table = $this->params['table']; $attrs = ($this->params['action'] ? explode(',',$this->params['action']) : ['dn','objectClass']); $this->page($this->ldap($table,$attrs)); @@ -1325,6 +1325,7 @@ EOF; $this->notImplemented('Unknown table action'." `$action`"); } + } public function redirect_if_no_referer($url) {