From 110f7fc2d12a00d5c9ac3ef2950e992b9fe93a40 Mon Sep 17 00:00:00 2001 From: jmontoyaa Date: Thu, 1 Feb 2018 08:56:13 +0100 Subject: [PATCH] Add config "generate_random_login" when importing users by csv BT#13944 --- main/admin/user_import.php | 23 ++++++++++++++++++----- main/install/configuration.dist.php | 6 ++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/main/admin/user_import.php b/main/admin/user_import.php index 1271ea3b0a..ce4fe4920a 100644 --- a/main/admin/user_import.php +++ b/main/admin/user_import.php @@ -53,13 +53,13 @@ function validate_data($users, $checkUniqueEmail = false) $errors[] = $user; } // 2.2. Check whether the username was used twice in import file. - if (isset($usernames[$user['UserName']])) { + if (isset($usernames[$username])) { $user['error'] = get_lang('UserNameUsedTwice'); $errors[] = $user; } - $usernames[$user['UserName']] = 1; + $usernames[$username] = 1; // 2.3. Check whether username is already occupied. - if (!UserManager::is_username_available($user['UserName'])) { + if (!UserManager::is_username_available($username)) { $user['error'] = get_lang('UserNameNotAvailable'); $errors[] = $user; } @@ -260,8 +260,21 @@ function save_data($users) */ function parse_csv_data($file) { - $users = Import :: csvToArray($file); + $users = Import::csvToArray($file); + $allowRandom = api_get_configuration_value('generate_random_login'); + if ($allowRandom) { + $factory = new RandomLib\Factory; + $generator = $factory->getLowStrengthGenerator(); + $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + } + foreach ($users as $index => $user) { + if (isset($user['UserName'])) { + if ($allowRandom) { + $username = $generator->generateString(10, $chars); + $user['UserName'] = $username; + } + } if (isset($user['Courses'])) { $user['Courses'] = explode('|', trim($user['Courses'])); } @@ -485,7 +498,7 @@ if (isset($_POST['formSent']) && $_POST['formSent'] && $_FILES['import_file']['s } } -Display :: display_header($tool_name); +Display::display_header($tool_name); $form = new FormValidator('user_import', 'post', api_get_self()); $form->addElement('header', '', $tool_name); diff --git a/main/install/configuration.dist.php b/main/install/configuration.dist.php index e36660b647..514f31d043 100755 --- a/main/install/configuration.dist.php +++ b/main/install/configuration.dist.php @@ -732,6 +732,12 @@ $_configuration['gradebook_badge_sidebar'] = [ // Allow teachers, drhs and admins to access blocked LP's because a prerequisite. //$_configuration['allow_teachers_to_access_blocked_lp_by_prerequisite'] = false; +// Allow connect skills with course tools +//$_configuration['allow_skill_rel_items'] = false; + +// Generate random login when importing users +//$_configuration['generate_random_login'] = false; + // ------ Custom DB changes // Add user activation by confirmation email // This option prevents the new user to login in the platform if your account is not confirmed via email