From de91d620f2854ad9b1237c26df3ad110d89b9c44 Mon Sep 17 00:00:00 2001 From: Michael Milette Date: Tue, 4 May 2021 11:32:22 -0400 Subject: [PATCH] MDL-58395 auth_ldap: User sync now brings problem details and continue MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: IƱaki Arenaza --- auth/ldap/auth.php | 16 +++++++++++++++- auth/ldap/lang/en/auth_ldap.php | 2 ++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/auth/ldap/auth.php b/auth/ldap/auth.php index 8ab37e4660e..62f4b6d7d18 100644 --- a/auth/ldap/auth.php +++ b/auth/ldap/auth.php @@ -930,6 +930,7 @@ class auth_plugin_ldap extends auth_plugin_base { if (!empty($add_users)) { print_string('userentriestoadd', 'auth_ldap', count($add_users)); + $errors = 0; $transaction = $DB->start_delegated_transaction(); foreach ($add_users as $user) { @@ -954,7 +955,14 @@ class auth_plugin_ldap extends auth_plugin_base { $user->calendartype = $CFG->calendartype; } - $id = user_create_user($user, false); + // $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; + } echo "\t"; print_string('auth_dbinsertuser', 'auth_db', array('name'=>$user->username, 'id'=>$id)); echo "\n"; $euser = $DB->get_record('user', array('id' => $id)); @@ -969,6 +977,12 @@ class auth_plugin_ldap extends auth_plugin_base { $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 { diff --git a/auth/ldap/lang/en/auth_ldap.php b/auth/ldap/lang/en/auth_ldap.php index 9edd34abb4a..484451829d8 100644 --- a/auth/ldap/lang/en/auth_ldap.php +++ b/auth/ldap/lang/en/auth_ldap.php @@ -116,6 +116,8 @@ $string['creatingtemptable'] = "Creating temporary table {\$a}\n"; $string['didntfindexpiretime'] = 'password_expire() didn\'t find expiration time.'; $string['didntgetusersfromldap'] = "Did not get any users from LDAP -- error? -- exiting\n"; $string['gotcountrecordsfromldap'] = "Got {\$a} records from LDAP\n"; +$string['invalidusererrors'] = "Warning: Skipped creation of {\$a} user accounts.\n\n"; +$string['invaliduserexception'] = "\nError: Cannot create new user account. Details and reason:\n{\$a}\nSkipping this user.\n\n"; $string['ldapnotconfigured'] = 'The LDAP host url is currently not configured'; $string['morethanoneuser'] = 'More than one user record found in LDAP. Using only the first one.'; $string['needbcmath'] = 'You need the BCMath extension to use expired password checking with Active Directory.';