MDL-36119: auth_{ldap,cas}: LDAP Sync - implement paged results

Thanks to Jerome Charaoui for the original patch.
This commit is contained in:
Iñaki Arenaza 2012-10-31 17:41:44 +01:00
parent 6109f2112c
commit c090d7c90e
12 changed files with 282 additions and 110 deletions

View file

@ -304,6 +304,8 @@ class enrol_ldap_plugin extends enrol_plugin {
return;
}
$ldap_pagedresults = ldap_paged_results_supported($this->get_config('ldap_version'));
// we may need a lot of memory here
@set_time_limit(0);
raise_memory_limit(MEMORY_HUGE);
@ -332,39 +334,57 @@ class enrol_ldap_plugin extends enrol_plugin {
// Define the search pattern
$ldap_search_pattern = $this->config->objectclass;
$ldap_cookie = '';
foreach ($ldap_contexts as $ldap_context) {
$ldap_context = trim($ldap_context);
if (empty($ldap_context)) {
continue; // Next;
}
if ($this->config->course_search_sub) {
// Use ldap_search to find first user from subtree
$ldap_result = @ldap_search($ldapconnection,
$ldap_context,
$ldap_search_pattern,
$ldap_fields_wanted);
} else {
// Search only in this context
$ldap_result = @ldap_list($ldapconnection,
$ldap_context,
$ldap_search_pattern,
$ldap_fields_wanted);
}
if (!$ldap_result) {
continue; // Next
}
do {
if ($ldap_pagedresults) {
ldap_control_paged_result($ldapconnection, $this->config->pagesize, true, $ldap_cookie);
}
// Check and push results
$records = ldap_get_entries($ldapconnection, $ldap_result);
if ($this->config->course_search_sub) {
// Use ldap_search to find first user from subtree
$ldap_result = @ldap_search($ldapconnection,
$ldap_context,
$ldap_search_pattern,
$ldap_fields_wanted);
} else {
// Search only in this context
$ldap_result = @ldap_list($ldapconnection,
$ldap_context,
$ldap_search_pattern,
$ldap_fields_wanted);
}
if (!$ldap_result) {
continue; // Next
}
// LDAP libraries return an odd array, really. fix it:
$flat_records = array();
for ($c = 0; $c < $records['count']; $c++) {
array_push($flat_records, $records[$c]);
if ($ldap_pagedresults) {
ldap_control_paged_result_response($ldapconnection, $ldap_result, $ldap_cookie);
}
// Check and push results
$records = ldap_get_entries($ldapconnection, $ldap_result);
// LDAP libraries return an odd array, really. fix it:
$flat_records = array();
for ($c = 0; $c < $records['count']; $c++) {
array_push($flat_records, $records[$c]);
}
// Free some mem
unset($records);
} while ($ldap_pagedresults && !empty($ldap_cookie));
// If LDAP paged results were used, the current connection must be completely
// closed and a new one created, to work without paged results from here on.
if ($ldap_pagedresults) {
$this->ldap_close(true);
$ldapconnection = $this->ldap_connect();
}
// Free some mem
unset($records);
if (count($flat_records)) {
$ignorehidden = $this->get_config('ignorehiddencourses');
@ -697,41 +717,61 @@ class enrol_ldap_plugin extends enrol_plugin {
// Get all contexts and look for first matching user
$ldap_contexts = explode(';', $ldap_contexts);
$ldap_pagedresults = ldap_paged_results_supported($this->get_config('ldap_version'));
$ldap_cookie = '';
foreach ($ldap_contexts as $context) {
$context = trim($context);
if (empty($context)) {
continue;
}
if ($this->get_config('course_search_sub')) {
// Use ldap_search to find first user from subtree
$ldap_result = @ldap_search($ldapconnection,
$context,
$ldap_search_pattern,
$ldap_fields_wanted);
} else {
// Search only in this context
$ldap_result = @ldap_list($ldapconnection,
$context,
$ldap_search_pattern,
$ldap_fields_wanted);
}
do {
if ($ldap_pagedresults) {
ldap_control_paged_result($ldapconnection, $this->config->pagesize, true, $ldap_cookie);
}
if (!$ldap_result) {
continue;
}
if ($this->get_config('course_search_sub')) {
// Use ldap_search to find first user from subtree
$ldap_result = @ldap_search($ldapconnection,
$context,
$ldap_search_pattern,
$ldap_fields_wanted);
} else {
// Search only in this context
$ldap_result = @ldap_list($ldapconnection,
$context,
$ldap_search_pattern,
$ldap_fields_wanted);
}
// Check and push results. ldap_get_entries() already
// lowercases the attribute index, so there's no need to
// use array_change_key_case() later.
$records = ldap_get_entries($ldapconnection, $ldap_result);
if (!$ldap_result) {
continue;
}
// LDAP libraries return an odd array, really. Fix it.
$flat_records = array();
for ($c = 0; $c < $records['count']; $c++) {
array_push($flat_records, $records[$c]);
if ($ldap_pagedresults) {
ldap_control_paged_result_response($ldapconnection, $ldap_result, $ldap_cookie);
}
// Check and push results. ldap_get_entries() already
// lowercases the attribute index, so there's no need to
// use array_change_key_case() later.
$records = ldap_get_entries($ldapconnection, $ldap_result);
// LDAP libraries return an odd array, really. Fix it.
$flat_records = array();
for ($c = 0; $c < $records['count']; $c++) {
array_push($flat_records, $records[$c]);
}
// Free some mem
unset($records);
} while ($ldap_pagedresults && !empty($ldap_cookie));
// If LDAP paged results were used, the current connection must be completely
// closed and a new one created, to work without paged results from here on.
if ($ldap_pagedresults) {
$this->ldap_close(true);
$ldapconnection = $this->ldap_connect();
}
unset($records);
if (count($flat_records)) {
$courses = array_merge($courses, $flat_records);
@ -788,7 +828,7 @@ class enrol_ldap_plugin extends enrol_plugin {
* groups.
*/
protected function ldap_find_user_groups_recursively($ldapconnection, $memberdn, &$membergroups) {
$result = @ldap_read ($ldapconnection, $memberdn, '(objectClass=*)', array($this->get_config('group_memberofattribute')));
$result = @ldap_read($ldapconnection, $memberdn, '(objectClass=*)', array($this->get_config('group_memberofattribute')));
if (!$result) {
return;
}

View file

@ -39,6 +39,7 @@ if ($ADMIN->fulltree) {
require_once($CFG->libdir.'/ldaplib.php');
$yesno = array(get_string('no'), get_string('yes'));
$pagedresults = ldap_paged_results_supported(get_config('enrol_ldap', 'ldap_version'));
//--- connection settings ---
$settings->add(new admin_setting_heading('enrol_ldap_server_settings', get_string('server_settings', 'enrol_ldap'), ''));
@ -47,6 +48,7 @@ if ($ADMIN->fulltree) {
$options = array(3=>'3', 2=>'2');
$settings->add(new admin_setting_configselect('enrol_ldap/ldap_version', get_string('version_key', 'enrol_ldap'), get_string('version', 'enrol_ldap'), 3, $options));
$settings->add(new admin_setting_configtext_trim_lower('enrol_ldap/ldapencoding', get_string('ldap_encoding_key', 'enrol_ldap'), get_string('ldap_encoding', 'enrol_ldap'), 'utf-8'));
$settings->add(new admin_setting_configtext_trim_lower('enrol_ldap/pagesize', get_string('pagesize_key', 'auth_ldap'), get_string('pagesize', 'auth_ldap'), LDAP_DEFAULT_PAGESIZE, true, $pagedresults));
//--- binding settings ---
$settings->add(new admin_setting_heading('enrol_ldap_bind_settings', get_string('bind_settings', 'enrol_ldap'), ''));

View file

@ -39,9 +39,11 @@ class admin_setting_configtext_trim_lower extends admin_setting_configtext {
* @param string $description long localised info
* @param string $defaultsetting default value for the setting
* @param boolean $lowercase if true, lowercase the value before writing it to the db.
* @param boolean $enabled if true, the input field is enabled, otherwise it's disabled.
*/
public function __construct($name, $visiblename, $description, $defaultsetting, $lowercase=false) {
public function __construct($name, $visiblename, $description, $defaultsetting, $lowercase=false, $enabled=true) {
$this->lowercase = $lowercase;
$this->enabled = $enabled;
parent::__construct($name, $visiblename, $description, $defaultsetting);
}
@ -65,8 +67,24 @@ class admin_setting_configtext_trim_lower extends admin_setting_configtext {
if ($this->lowercase) {
$data = textlib::strtolower($data);
}
if (!$this->enabled) {
return '';
}
return ($this->config_write($this->name, trim($data)) ? '' : get_string('errorsetting', 'admin'));
}
/**
* Return an XHTML string for the setting
* @return string Returns an XHTML string
*/
public function output_html($data, $query='') {
$default = $this->get_defaultsetting();
$disabled = $this->enabled ? '': ' disabled="disabled"';
return format_admin_setting($this, $this->visiblename,
'<div class="form-text defaultsnext"><input type="text" size="'.$this->size.'" id="'.$this->get_id().'" name="'.$this->get_full_name().'" value="'.s($data).'" '.$disabled.' /></div>',
$this->description, true, '', $default, $query);
}
}
class admin_setting_ldap_rolemapping extends admin_setting {

View file

@ -26,6 +26,6 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2012061700; // The current plugin version (Date: YYYYMMDDXX)
$plugin->version = 2012110700; // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires = 2012061700; // Requires this Moodle version
$plugin->component = 'enrol_ldap'; // Full name of the plugin (used for diagnostics)