From 5178e16d926d9e96f2f2a1fd2cfaf597f696c5ec Mon Sep 17 00:00:00 2001
From: Alexander Bias <alexander.bias@uni-ulm.de>
Date: Fri, 3 Mar 2017 16:18:13 +0100
Subject: [PATCH] Adopt code changes in Moodle 3.2 core auth_ldap

---
 CHANGES.md  |  4 ++++
 auth.php    |  2 +-
 config.html | 26 ++++++++++++++++++++++++--
 3 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index 198cb86..6955c97 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -4,6 +4,10 @@ moodle-auth_ldap_syncplus
 Changes
 -------
 
+### Unreleased
+
+* 2017-03-03 - Adopt code changes in Moodle 3.2 core auth_ldap
+
 ### v3.2-r1
 
 * 2017-01-13 - Check compatibility for Moodle 3.2, no functionality change
diff --git a/auth.php b/auth.php
index ff5e8cb..a1176c8 100644
--- a/auth.php
+++ b/auth.php
@@ -96,7 +96,7 @@ class auth_plugin_ldap_syncplus extends auth_plugin_ldap {
             array_push($contexts, $this->config->create_context);
         }
 
-        $ldap_pagedresults = ldap_paged_results_supported($this->config->ldap_version);
+        $ldap_pagedresults = ldap_paged_results_supported($this->config->ldap_version, $ldapconnection);
         $ldap_cookie = '';
         foreach ($contexts as $context) {
             $context = trim($context);
diff --git a/config.html b/config.html
index bd0d78c..78ec176 100644
--- a/config.html
+++ b/config.html
@@ -123,9 +123,31 @@ $fastpathoptions = array(AUTH_NTLM_FASTPATH_YESFORM => get_string('auth_ntlmsso_
                          AUTH_NTLM_FASTPATH_ATTEMPT => get_string('auth_ntlmsso_ie_fastpath_attempt', 'auth_ldap'));
 
 $disabled = '';
-if (!ldap_paged_results_supported($config->ldap_version)) {
+$pagedresultssupported = false;
+if ($config->host_url !== '') {
+    /**
+     * We try to connect each and every time we open the config, because we want to set the Page
+     * Size setting as enabled or disabled depending on the configured LDAP server supporting
+     * pagination or not, and to notify the user about it. If the user changed the LDAP server (or
+     * the LDAP protocol version) last time, it might happen that paged results are no longer
+     * available and we want to show that to the user the next time she goes to the settings page.
+     */
+    try {
+        $ldapconn = $this->ldap_connect();
+        $pagedresultssupported = ldap_paged_results_supported($config->ldap_version, $ldapconn);
+    } catch (Exception $e) {
+        // If we couldn't connect and get the supported options, we can only assume we don't support paged results.
+        $pagedresultssupported = false;
+    }
+}
+/* Make sure we only disable the paged result size setting and show the notification about it if
+ * there is a configured server that we tried to contact.  Othersiwe, if someone's LDAP server does
+ * support paged results, they won't be able to turn it on the first time they set it up (because
+ * the field will be disabled).
+ */
+if (($config->host_url !== '') && (!$pagedresultssupported)) {
     $disabled = ' disabled="disabled"';
-    echo $OUTPUT->notification(get_string('pagedresultsnotsupp', 'auth_ldap'));
+    echo $OUTPUT->notification(get_string('pagedresultsnotsupp', 'auth_ldap'), \core\output\notification::NOTIFY_INFO);
 }
 
 ?>
-- 
GitLab