diff --git a/includes/activation_form.inc b/includes/activation_form.inc index fda8f02..9fcbd04 100644 --- a/includes/activation_form.inc +++ b/includes/activation_form.inc @@ -103,9 +103,15 @@ function os2intra_user_import_activate_form_validate($form, &$form_state) { } // Check if it matches the users birthday field - $birthday_field = field_get_items('user', $user, 'field_os2intra_birthdate'); - if ($input_birthday !== $birthday_field[0]['value']) { - form_set_error('form', 'Please check your input.'); + $birthday_field_name = variable_get('os2intra_user_import_birthday_field'); + if (!empty(field_info_instance('user', $birthday_field_name, 'user'))) { + $birthday_field = field_get_items('user', $user, $birthday_field_name); + if ($input_birthday !== date("dmy", strtotime($birthday_field[0]['value']))) { + form_set_error('form', 'Please check your input.'); + } + } + else { + form_set_error('form', 'Activation form has wrong configuration settings. Please contact site administrator'); } } diff --git a/includes/csv_mapping.inc b/includes/csv_mapping.inc index ffb1229..4202372 100644 --- a/includes/csv_mapping.inc +++ b/includes/csv_mapping.inc @@ -3,6 +3,13 @@ * Helper function to parse file contents to array with relevant info */ function _os2intra_user_import_process_file($file_path, $lines = FALSE) { + $identification_method = variable_get('os2intra_user_import_activate_identification', OS2INTRA_USER_IMPORT_ACTIVATE_IDENTIFICATION); + $rows = array(); + if (empty($identification_method)) { + drupal_set_message('User activation mode is not setup. Please check user import configuration /admin/config/os2intra/user_import/settings', 'error'); + return $rows; + } + // CSV field mapping // rewrite for settings page? $mapping = variable_get('os2intra_mapping'); @@ -59,22 +66,15 @@ function _os2intra_user_import_process_file($file_path, $lines = FALSE) { } // Skip if row has no ad_id and no employee_id if ($mapped_row['ad_id'] == '' && $mapped_row['employee_id'] == '' ) { + os2intra_user_import_save_log('', 'No ad_id and no employee_id found in row: ' . implode(';', $row)); continue; } - // Generate ad_id if missing + // Try to set ad_id from ad_name column. if (!isset($mapped_row['ad_id'])) { if (isset($mapped_row['ad_name']) && !empty($mapped_row['ad_name'])) { $mapped_row['ad_id'] = $mapped_row['ad_name']; } - else { - if (variable_get('os2intra_generate_username')) { - $mapped_row['ad_id'] = os2intra_user_import_generate_username($mapped_row); - } - else { - continue; - } - } } $rows[] = $mapped_row; diff --git a/includes/settings_form.inc b/includes/settings_form.inc index 710bb19..8c59d99 100644 --- a/includes/settings_form.inc +++ b/includes/settings_form.inc @@ -154,6 +154,7 @@ $csv_cols_fallback_1 = array( '#type' => 'fieldset', '#title' => t('User activation'), '#collapsible' => TRUE, + '#weight' => -100, ); $form['os2intra_user_import_activate']['activation_fields'] = array( @@ -164,7 +165,9 @@ $csv_cols_fallback_1 = array( $form['os2intra_user_import_activate']['activation_fields']['os2intra_user_import_activate_identification'] = array( '#title' => t('User identification'), '#type' => 'select', + '#required' => TRUE, '#options' => array( + '' => t('None'), 'ad_id' => t('AD id'), 'employee_id' => ('Employee number'), ), @@ -358,12 +361,6 @@ $csv_cols_fallback_1 = array( '#description' => t('If checked only membership from current import will be added'), '#default_value' => variable_get('os2intra_revoke_og_roles') ); - // Variable which contains settings to determinate how username should be generated. - $form['os2intra_structure_settings']['os2intra_generate_username'] = array( - '#type' => 'checkbox', - '#title' => t('Generate unique user name from "user full name + department name" when AD id is not present'), - '#default_value' => variable_get('os2intra_generate_username') - ); // Variable which contains birthday format. $form['os2intra_structure_settings']['os2intra_user_import_birthdate_db_format'] = array( '#type' => 'textfield', diff --git a/includes/users.inc b/includes/users.inc index 272d993..f33ea31 100644 --- a/includes/users.inc +++ b/includes/users.inc @@ -49,23 +49,59 @@ function os2intra_user_import_disable_users() { } /** - * Check if the user is already created in the system + * Check if the user is already created in the system. */ -function os2intra_user_import_check_users(&$users, $duplicate_emails, $duplicate_ids) { +function os2intra_user_import_check_users(&$users) { $update_users = array(); + + // Check duplicated emails. Drupal users should have unique email. + $dup_emails = array(); + $user_email = array_column($users, 'email'); + if (!empty($user_email) && count($users) !== count(array_unique($user_email))) { + $dup_emails = array_unique(array_diff_assoc($user_email, array_unique($user_email))); + os2intra_user_import_save_log('', 'Duplicate email found in import file: ' . implode(', ', $dup_emails)); + } + + // Check duplicated AD id. AD id values should be unique. + $ad_id = array_column($users, 'ad_id'); + $dup_id = array(); + if (!empty($ad_id) && count($users) !== count(array_unique($ad_id))) { + $dup_id = array_unique(array_diff_assoc($ad_id, array_unique($ad_id))); + os2intra_user_import_save_log('', 'Duplicate User AD id found in import file: ' . implode(', ', $dup_id)); + } + + $identification_method = variable_get('os2intra_user_import_activate_identification', OS2INTRA_USER_IMPORT_ACTIVATE_IDENTIFICATION); + // For employee_id activation mode employee_id values should be unique. + if ($identification_method == 'employee_id') { + $employee_id = array_column($users, 'employee_id'); + $dup_em_id = array(); + if (!empty($employee_id) && count($users) !== count(array_unique($employee_id))) { + $dup_em_id = array_unique(array_diff_assoc($employee_id, array_unique($employee_id))); + os2intra_user_import_save_log('', 'Duplicate employee id found in import file: ' . implode(', ', $dup_em_id)); + } + } + foreach ($users as $key => $user) { - if (array_search($user['email'], $duplicate_emails) !== false) { + if (array_search($user['email'], $dup_emails) !== FALSE) { os2intra_user_import_save_log('', 'Duplicate email found: Skip user ' . $user['email']); unset($users[$key]); continue; } - if (array_search($user['ad_id'], $duplicate_ids) !== false) { + if (array_search($user['ad_id'], $dup_id) !== FALSE) { unset($users[$key]); os2intra_user_import_save_log('', 'Duplicate user AD found: Skip user ' . $user['ad_id']); continue; } + if ($identification_method == 'employee_id') { + if (array_search($user['employee_id'], $dup_em_id) !== FALSE) { + unset($users[$key]); + os2intra_user_import_save_log('', 'Duplicate user employee id found: Skip user ' . $user['employee_id']); + continue; + } + } + $result = NULL; $identification_method = variable_get('os2intra_user_import_activate_identification', OS2INTRA_USER_IMPORT_ACTIVATE_IDENTIFICATION); @@ -217,7 +253,7 @@ function os2intra_user_import_save_user($user, $uid = '') { // This is only done in order to migrate user groups, from users before the // import groups field was added. if ($no_import_groups) { - $user_groups = array(); + $user_groups = array(LANGUAGE_NONE => array()); } // Add back the groups we want to keep. @@ -362,26 +398,31 @@ function os2intra_user_import_save_user($user, $uid = '') { // Handle whether we're updating or creating a new user // if we're updating we don't generate username and sets password if (!is_numeric($uid)) { - $password = user_password(8); - // At this step even if the ad_id was empty initially it must have been generated + // By default user name comes from ad_id. $fields['name'] = $user['ad_id']; + + // For empty ad_id generate username. + if (empty($user['ad_id'])) { + $fields['name'] = os2intra_user_import_generate_username($user); + } + + $password = user_password(8); $fields['pass'] = $password; // Before create check if user has correct name. if (empty($fields['name'])) { - os2intra_user_import_save_log($user['employee_id'], 'Cannot add user with empty name/AD_id. Employee id: ' . $user['employee_id'] . ' Ad id: ' . $user['ad_id']); + os2intra_user_import_save_log($user['employee_id'], 'Cannot add user with empty name. Employee id: ' . $user['employee_id'] . ' Ad id: ' . $user['ad_id']); return; } // Before create check if user with this name already exist. if (!empty(user_load_by_name($fields['name']))) { - os2intra_user_import_save_log($user['employee_id'], 'User with name/AD_id already exists Employee id: ' . $user['employee_id'] . ' Ad id: ' . $user['ad_id']); + os2intra_user_import_save_log($user['employee_id'], 'User with name already exists Employee id: ' . $user['employee_id'] . ' Ad id: ' . $user['ad_id']); return; } } - // Set Opus Roles if (field_info_instance('user', 'os2intra_users_opus_roles', 'user')) { $fields['os2intra_users_opus_roles'][LANGUAGE_NONE] = array(); @@ -431,7 +472,7 @@ function os2intra_user_import_save_user($user, $uid = '') { ); } - if (!user_get_authmaps($authmap)) { + if (!empty($authmap) && !user_get_authmaps($authmap)) { user_set_authmaps($account, $authmap); } // Add users to parent departments diff --git a/os2intra_user_import.module b/os2intra_user_import.module index eb1c107..1924a83 100644 --- a/os2intra_user_import.module +++ b/os2intra_user_import.module @@ -20,7 +20,7 @@ module_load_include('inc', 'os2intra_user_import', 'includes/users'); module_load_include('inc', 'os2intra_user_import', 'includes/utilities'); // User activation default variable. -define('OS2INTRA_USER_IMPORT_ACTIVATE_IDENTIFICATION', 'employee_id'); +define('OS2INTRA_USER_IMPORT_ACTIVATE_IDENTIFICATION', NULL); define('OS2INTRA_USER_IMPORT_ACTIVATE_BIRTHDAY', TRUE); define('OS2INTRA_USER_IMPORT_ACTIVATE_EMAIL', TRUE); /** @@ -128,9 +128,16 @@ function os2intra_user_import_overview() { } /** - * User import wrapper function + * User import wrapper function. */ function os2intra_user_import_run_with_drush() { + if (empty(variable_get('os2intra_user_import_activate_identification', OS2INTRA_USER_IMPORT_ACTIVATE_IDENTIFICATION))) { + $message = 'User activation mode is not setup. Please check user import configuration /admin/config/os2intra/user_import/settings'; + os2intra_user_import_save_log('', $message); + drupal_set_message($message, 'error'); + return; + } + if (variable_get('os2intra_user_import_enable', FALSE)) { $current_timestamp = &drupal_static('os2intra_user_import_current_timestamp'); $current_timestamp = time(); @@ -194,24 +201,8 @@ function os2intra_user_import_run_with_drush() { if ($current_timestamp != $last_import_temp_users) { os2intra_user_import_save_log('', 'Starting User Loop'); - // Checking for duplicates - $dup_emails = array(); - $user_email = array_column($users, 'email'); - if (!empty($user_email) && count($users) !== count(array_unique($user_email))) { - $dup_emails = array_unique(array_diff_assoc($user_email,array_unique($user_email))); - os2intra_user_import_save_log('', 'Duplicate email found in import file: '. implode(', ', $dup_emails)); - } - - $dup_id = array(); - $user_id = array_column($users, 'ad_id'); - if (!empty($user_id) && count($users) !== count(array_unique($user_id))) { - $dup_id = array_unique(array_diff_assoc($user_id, array_unique($user_id))); - os2intra_user_import_save_log('', 'Duplicate User AD id found in import file: '. implode(', ', $dup_id)); - } - - - $update_users = os2intra_user_import_check_users($users, $dup_emails, $dup_id); - // Loop over users and create + $update_users = os2intra_user_import_check_users($users); + // Loop over users and create. os2intra_user_import_save_log('', 'Start Creating Users'); foreach ($users as $user) {