From 6d8da3b3f201e1bb3a75f77dc033095d67d51269 Mon Sep 17 00:00:00 2001
From: Dennis Ahrens <dennis.ahrens@hs-hannover.de>
Date: Mon, 27 Jun 2022 11:30:13 +0200
Subject: [PATCH] Fix #30 by backporting MDL-58395

auth_ldap: User sync now brings problem details and continue
It does not include the error messages, which will appear with
moodle 4.0.
Nevertheless this version should work with moodle 3.11 as well,
but with ugly log messages.
---
 auth.php | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/auth.php b/auth.php
index 7f5fe45..f2fe15d 100644
--- a/auth.php
+++ b/auth.php
@@ -432,6 +432,7 @@ class auth_plugin_ldap_syncplus extends auth_plugin_ldap {
 
             if (!empty($add_users)) {
                 mtrace(get_string('userentriestoadd', 'auth_ldap', count($add_users)));
+                $errors = 0;
 
                 $transaction = $DB->start_delegated_transaction();
                 foreach ($add_users as $user) {
@@ -457,6 +458,14 @@ class auth_plugin_ldap_syncplus extends auth_plugin_ldap {
                     }
 
                     $id = user_create_user($user, false);
+                    try {
+                        $id = user_create_user($user, false);
+                    } catch (Exception $e) {
+                        print_string('invaliduserexception', 'auth_ldap', print_r($user, true) .  $e->getMessage());
+                        $errors++;
+                        continue;
+                    }
+
                     mtrace("\t".get_string('auth_dbinsertuser', 'auth_db', array('name'=>$user->username, 'id'=>$id)));
                     $euser = $DB->get_record('user', array('id' => $id));
 
@@ -470,6 +479,11 @@ class auth_plugin_ldap_syncplus extends auth_plugin_ldap {
                     // Add roles if needed.
                     $this->sync_roles($euser);
                 }
+
+                // Display number of user creation errors, if any.
+                if ($errors) {
+                    print_string('invalidusererrors', 'auth_ldap', $errors);
+                }
                 $transaction->allow_commit();
                 unset($add_users); // free mem
             } else {
-- 
GitLab