/**
 * Validates imported data.
 */
function validate_data($user_classes)
{
    global $purification_option_for_usernames;
    $errors = array();
    $classcodes = array();
    $usergroup = new UserGroup();
    foreach ($user_classes as $index => $user_class) {
        $user_class['line'] = $index + 1;
        // 1. Check whether mandatory fields are set.
        $mandatory_fields = array('UserName', 'ClassName');
        foreach ($mandatory_fields as $field) {
            if (!isset($user_class[$field]) || strlen($user_class[$field]) == 0) {
                $user_class['error'] = get_lang($field . 'Mandatory');
                $errors[] = $user_class;
            }
        }
        // 2. Check whether class code exists.
        if (isset($user_class['ClassName']) && strlen($user_class['ClassName']) != 0) {
            // 2.1 Check whether code has been already used in this CVS-file.
            if (!isset($classcodes[$user_class['ClassName']])) {
                // 2.1.1 Check whether code exists in DB
                $exists = $usergroup->usergroup_exists($user_class['ClassName']);
                if (!$exists) {
                    $user_class['error'] = get_lang('CodeDoesNotExists') . ': ' . $user_class['ClassName'];
                    $errors[] = $user_class;
                } else {
                    $classcodes[$user_class['CourseCode']] = 1;
                }
            }
        }
        // 3. Check username, first, check whether it is empty.
        if (!UserManager::is_username_empty($user_class['UserName'])) {
            // 3.1. Check whether username is too long.
            if (UserManager::is_username_too_long($user_class['UserName'])) {
                $user_class['error'] = get_lang('UserNameTooLong') . ': ' . $user_class['UserName'];
                $errors[] = $user_class;
            }
            $username = UserManager::purify_username($user_class['UserName'], $purification_option_for_usernames);
            // 3.2. Check whether username exists.
            if (UserManager::is_username_available($username)) {
                $user_class['error'] = get_lang('UnknownUser') . ': ' . $username;
                $errors[] = $user_class;
            }
        }
    }
    return $errors;
}
Beispiel #2
2
/**
 * Validates imported data.
 */
function validate_data($users)
{
    global $defined_auth_sources;
    $errors = array();
    $usernames = array();
    if (is_array($users)) {
        foreach ($users as $index => $user) {
            // 1. Check whether mandatory fields have been set.
            $mandatory_fields = array('LastName', 'FirstName');
            if (api_get_setting('registration', 'email') == 'true') {
                $mandatory_fields[] = 'Email';
            }
            foreach ($mandatory_fields as $key => $field) {
                if (!isset($user[$field]) || strlen($user[$field]) == 0) {
                    $user['error'] = get_lang($field . 'Mandatory');
                    $errors[] = $user;
                }
            }
            // 2. Check username.
            if (!UserManager::is_username_empty($user['UserName'])) {
                // 2.1. Check whether username was used twice in the import file.
                if (isset($usernames[$user['UserName']])) {
                    $user['error'] = get_lang('UserNameUsedTwice');
                    $errors[] = $user;
                }
                $usernames[$user['UserName']] = 1;
                // 2.2. Check whether username is allready in use in database.
                if (!UserManager::is_username_available($user['UserName'])) {
                    $user['error'] = get_lang('UserNameNotAvailable');
                    $errors[] = $user;
                }
                // 2.3. Check whether username is too long.
                if (UserManager::is_username_too_long($user['UserName'])) {
                    $user['error'] = get_lang('UserNameTooLong');
                    $errors[] = $user;
                }
            }
            // 3. Check status.
            if (isset($user['Status']) && !api_status_exists($user['Status'])) {
                $user['error'] = get_lang('WrongStatus');
                $errors[] = $user;
            }
            // 4. Check classname.
            if (isset($user['ClassName']) && strlen($user['ClassName']) != 0) {
                if (!ClassManager::class_name_exists($user['ClassName'])) {
                    $user['error'] = get_lang('ClassNameNotAvailable');
                    $errors[] = $user;
                }
            }
            // 5. Check authentication source.
            if (isset($user['AuthSource']) && strlen($user['AuthSource']) != 0) {
                if (!in_array($user['AuthSource'], $defined_auth_sources)) {
                    $user['error'] = get_lang('AuthSourceNotAvailable');
                    $errors[] = $user;
                }
            }
        }
    }
    return $errors;
}
/**
 * Validates imported data.
 */
function validate_data($user_classes)
{
    global $purification_option_for_usernames;
    $errors = array();
    $classcodes = array();
    if (!isset($_POST['subscribe']) && !isset($_POST['subscribe'])) {
        $user_class['error'] = get_lang('SelectAnAction');
        $errors[] = $user_class;
        return $errors;
    }
    foreach ($user_classes as $index => $user_class) {
        $user_class['line'] = $index + 1;
        // 1. Check whether mandatory fields are set.
        $mandatory_fields = array('UserName', 'ClassName');
        foreach ($mandatory_fields as $key => $field) {
            if (!isset($user_class[$field]) || strlen($user_class[$field]) == 0) {
                $user_class['error'] = get_lang($field . 'Mandatory');
                $errors[] = $user_class;
            }
        }
        // 2. Check whether classcode exists.
        if (isset($user_class['ClassName']) && strlen($user_class['ClassName']) != 0) {
            // 2.1 Check whether code has been allready used in this CVS-file.
            if (!isset($classcodes[$user_class['ClassName']])) {
                // 2.1.1 Check whether code exists in DB.
                $class_table = Database::get_main_table(TABLE_MAIN_CLASS);
                $sql = "SELECT * FROM {$class_table} WHERE name = '" . Database::escape_string($user_class['ClassName']) . "'";
                $res = Database::query($sql);
                if (Database::num_rows($res) == 0) {
                    $user_class['error'] = get_lang('CodeDoesNotExists') . ': ' . $user_class['ClassName'];
                    $errors[] = $user_class;
                } else {
                    $classcodes[$user_class['CourseCode']] = 1;
                }
            }
        }
        // 3. Check username, first, check whether it is empty.
        if (!UserManager::is_username_empty($user_class['UserName'])) {
            // 3.1. Check whether username is too long.
            if (UserManager::is_username_too_long($user_class['UserName'])) {
                $user_class['error'] = get_lang('UserNameTooLong') . ': ' . $user_class['UserName'];
                $errors[] = $user_class;
            }
            $username = UserManager::purify_username($user_class['UserName'], $purification_option_for_usernames);
            // 3.2. Check whether username exists.
            if (UserManager::is_username_available($username)) {
                $user_class['error'] = get_lang('UnknownUser') . ': ' . $username;
                $errors[] = $user_class;
            }
        }
    }
    return $errors;
}
/**
 * Validates the imported data.
 */
function validate_data($users_courses)
{
    $errors = array();
    $coursecodes = array();
    foreach ($users_courses as $index => $user_course) {
        $user_course['line'] = $index + 1;
        // 1. Check whether mandatory fields are set.
        $mandatory_fields = array('UserName', 'CourseCode', 'Status');
        foreach ($mandatory_fields as $key => $field) {
            if (!isset($user_course[$field]) || strlen($user_course[$field]) == 0) {
                $user_course['error'] = get_lang($field . 'Mandatory');
                $errors[] = $user_course;
            }
        }
        // 2. Check whether coursecode exists.
        if (isset($user_course['CourseCode']) && strlen($user_course['CourseCode']) != 0) {
            // 2.1 Check whethher code has been allready used by this CVS-file.
            if (!isset($coursecodes[$user_course['CourseCode']])) {
                // 2.1.1 Check whether course with this code exists in the system.
                $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
                $sql = "SELECT * FROM {$course_table}\n                        WHERE code = '" . Database::escape_string($user_course['CourseCode']) . "'";
                $res = Database::query($sql);
                if (Database::num_rows($res) == 0) {
                    $user_course['error'] = get_lang('CodeDoesNotExists');
                    $errors[] = $user_course;
                } else {
                    $coursecodes[$user_course['CourseCode']] = 1;
                }
            }
        }
        // 3. Check whether username exists.
        if (isset($user_course['UserName']) && strlen($user_course['UserName']) != 0) {
            if (UserManager::is_username_available($user_course['UserName'])) {
                $user_course['error'] = get_lang('UnknownUser');
                $errors[] = $user_course;
            }
        }
        // 4. Check whether status is valid.
        if (isset($user_course['Status']) && strlen($user_course['Status']) != 0) {
            if ($user_course['Status'] != COURSEMANAGER && $user_course['Status'] != STUDENT) {
                $user_course['error'] = get_lang('UnknownStatus');
                $errors[] = $user_course;
            }
        }
    }
    return $errors;
}
/**
 * Validates the imported data.
 */
function validate_data($users_courses)
{
    $errors = array();
    $coursecodes = array();
    foreach ($users_courses as $index => $user_course) {
        $user_course['line'] = $index + 1;
        // 1. Check whether mandatory fields are set.
        $mandatory_fields = array('UserName', 'CourseCode', 'Status');
        foreach ($mandatory_fields as $field) {
            if (!isset($user_course[$field]) || strlen($user_course[$field]) == 0) {
                $user_course['error'] = get_lang($field . 'Mandatory');
                $errors[] = $user_course;
            }
        }
        // 2. Check whether coursecode exists.
        if (isset($user_course['CourseCode']) && strlen($user_course['CourseCode']) != 0) {
            // 2.1 Check whethher code has been allready used by this CVS-file.
            if (!isset($coursecodes[$user_course['CourseCode']])) {
                // 2.1.1 Check whether course with this code exists in the system.
                $courseInfo = api_get_course_info($user_course['CourseCode']);
                if (empty($courseInfo)) {
                    $user_course['error'] = get_lang('CodeDoesNotExists');
                    $errors[] = $user_course;
                } else {
                    $coursecodes[$user_course['CourseCode']] = 1;
                }
            }
        }
        // 3. Check whether username exists.
        if (isset($user_course['UserName']) && strlen($user_course['UserName']) != 0) {
            if (UserManager::is_username_available($user_course['UserName'])) {
                $user_course['error'] = get_lang('UnknownUser');
                $errors[] = $user_course;
            }
        }
        // 4. Check whether status is valid.
        if (isset($user_course['Status']) && strlen($user_course['Status']) != 0) {
            if ($user_course['Status'] != COURSEMANAGER && $user_course['Status'] != STUDENT) {
                $user_course['error'] = get_lang('UnknownStatus');
                $errors[] = $user_course;
            }
        }
    }
    return $errors;
}
function WSCreateUserPasswordCrypted($params)
{
    global $_user, $_configuration, $debug;
    $debug = 1;
    if ($debug) {
        error_log('WSCreateUserPasswordCrypted');
    }
    if ($debug) {
        error_log(print_r($params, 1));
    }
    if (!WSHelperVerifyKey($params)) {
        return return_error(WS_ERROR_SECRET_KEY);
    }
    // Database table definition.
    $table_user = Database::get_main_table(TABLE_MAIN_USER);
    $orig_user_id_value = array();
    $password = $params['password'];
    $encrypt_method = $params['encrypt_method'];
    $firstName = $params['firstname'];
    $lastName = $params['lastname'];
    $status = $params['status'];
    $email = $params['email'];
    $loginName = $params['loginname'];
    $official_code = isset($params['official_code']) ? $params['official_code'] : '';
    $language = '';
    $phone = $params['phone'];
    $picture_uri = '';
    $auth_source = PLATFORM_AUTH_SOURCE;
    $expiration_date = '';
    $active = 1;
    $hr_dept_id = 0;
    $extra = null;
    $original_user_id_name = $params['original_user_id_name'];
    $original_user_id_value = $params['original_user_id_value'];
    $orig_user_id_value[] = $params['original_user_id_value'];
    $extra_list = isset($params['extra']) ? $params['extra'] : '';
    if (!empty($_configuration['password_encryption'])) {
        if ($_configuration['password_encryption'] === $encrypt_method) {
            if ($encrypt_method == 'md5' && !preg_match('/^[A-Fa-f0-9]{32}$/', $password)) {
                $msg = "Encryption {$encrypt_method} is invalid";
                if ($debug) {
                    error_log($msg);
                }
                return $msg;
            } else {
                if ($encrypt_method == 'sha1' && !preg_match('/^[A-Fa-f0-9]{40}$/', $password)) {
                    $msg = "Encryption {$encrypt_method} is invalid";
                    if ($debug) {
                        error_log($msg);
                    }
                    return $msg;
                }
            }
        } else {
            $msg = "This encryption {$encrypt_method} is not configured";
            if ($debug) {
                error_log($msg);
            }
            return $msg;
        }
    } else {
        $msg = 'The chamilo setting $_configuration["password_encryption"] is not configured';
        if ($debug) {
            error_log($msg);
        }
        return $msg;
    }
    if (!empty($params['language'])) {
        $language = $params['language'];
    }
    if (!empty($params['phone'])) {
        $phone = $params['phone'];
    }
    if (!empty($params['expiration_date'])) {
        $expiration_date = $params['expiration_date'];
    }
    // Check whether x_user_id exists into user_field_values table.
    $user_id = UserManager::get_user_id_from_original_id($original_user_id_value, $original_user_id_name);
    if ($debug) {
        error_log('Ready to create user');
    }
    if ($user_id > 0) {
        if ($debug) {
            error_log('User found with id: ' . $user_id);
        }
        // Check whether user is not active
        //@todo why this condition exists??
        $sql = "SELECT user_id FROM {$table_user}\n                WHERE user_id ='" . $user_id . "' AND active= '0' ";
        $resu = Database::query($sql);
        $r_check_user = Database::fetch_row($resu);
        $count_check_user = Database::num_rows($resu);
        if ($count_check_user > 0) {
            if ($debug) {
                error_log('User id: ' . $user_id . ' exists and is NOT active. Updating user and setting setting active = 1');
            }
            $sql = "UPDATE {$table_user} SET\n                    lastname='" . Database::escape_string($lastName) . "',\n                    firstname='" . Database::escape_string($firstName) . "',\n                    username='******',";
            if (!is_null($auth_source)) {
                $sql .= " auth_source='" . Database::escape_string($auth_source) . "',";
            }
            $sql .= "\n                    password='******',\n                    email='" . Database::escape_string($email) . "',\n                    status='" . Database::escape_string($status) . "',\n                    official_code='" . Database::escape_string($official_code) . "',\n                    phone='" . Database::escape_string($phone) . "',\n                    expiration_date='" . Database::escape_string($expiration_date) . "',\n                    active='1',\n                    hr_dept_id=" . intval($hr_dept_id);
            $sql .= " WHERE user_id='" . $r_check_user[0] . "'";
            if ($debug) {
                error_log($sql);
            }
            Database::query($sql);
            if (is_array($extra_list) && count($extra_list) > 0) {
                foreach ($extra_list as $extra) {
                    $extra_field_name = $extra['field_name'];
                    $extra_field_value = $extra['field_value'];
                    // Save the external system's id into user_field_value table.
                    UserManager::update_extra_field_value($r_check_user[0], $extra_field_name, $extra_field_value);
                }
            }
            return $r_check_user[0];
        } else {
            if ($debug) {
                error_log('User exists but is active. Cant be updated');
            }
            return 0;
        }
    } else {
        if ($debug) {
            error_log("User not found with original_id = {$original_user_id_value} and original_name = {$original_user_id_name}");
        }
    }
    // Default language.
    if (empty($language)) {
        $language = api_get_setting('platformLanguage');
    }
    if (!empty($_user['user_id'])) {
        $creator_id = $_user['user_id'];
    } else {
        $creator_id = '';
    }
    // First check wether the login already exists
    if (!UserManager::is_username_available($loginName)) {
        if ($debug) {
            error_log("Username {$loginName} is not available");
        }
        return 0;
    }
    $sql = "INSERT INTO {$table_user} SET\n            lastname            = '" . Database::escape_string(trim($lastName)) . "',\n            firstname           = '" . Database::escape_string(trim($firstName)) . "',\n            username            = '******',\n            status              = '" . Database::escape_string($status) . "',\n            password            = '******',\n            email               = '" . Database::escape_string($email) . "',\n            official_code       = '" . Database::escape_string($official_code) . "',\n            picture_uri         = '" . Database::escape_string($picture_uri) . "',\n            creator_id          = '" . Database::escape_string($creator_id) . "',\n            auth_source         = '" . Database::escape_string($auth_source) . "',\n            phone               = '" . Database::escape_string($phone) . "',\n            language            = '" . Database::escape_string($language) . "',\n            registration_date   = '" . api_get_utc_datetime() . "',\n            expiration_date     = '" . Database::escape_string($expiration_date) . "',\n            hr_dept_id          = '" . Database::escape_string($hr_dept_id) . "',\n            active              = '" . Database::escape_string($active) . "'";
    if ($debug) {
        error_log($sql);
    }
    $result = Database::query($sql);
    if ($result) {
        $return = Database::insert_id();
        $sql = "UPDATE {$table_user} SET user_id = id WHERE id = {$return}";
        Database::query($sql);
        $url_id = api_get_current_access_url_id();
        UrlManager::add_user_to_url($return, $url_id);
        if ($debug) {
            error_log("Adding user_id = {$return} to URL id {$url_id} ");
        }
        // Save new fieldlabel into user_field table.
        $field_id = UserManager::create_extra_field($original_user_id_name, 1, $original_user_id_name, '');
        // Save the remote system's id into user_field_value table.
        UserManager::update_extra_field_value($return, $original_user_id_name, $original_user_id_value);
        if (is_array($extra_list) && count($extra_list) > 0) {
            foreach ($extra_list as $extra) {
                $extra_field_name = $extra['field_name'];
                $extra_field_value = $extra['field_value'];
                // save new fieldlabel into user_field table
                $field_id = UserManager::create_extra_field($extra_field_name, 1, $extra_field_name, '');
                // save the external system's id into user_field_value table'
                UserManager::update_extra_field_value($return, $extra_field_name, $extra_field_value);
            }
        }
    } else {
        return 0;
    }
    return $return;
}
function validate_data($users)
{
    global $defined_auth_sources;
    $errors = array();
    $usernames = array();
    // 1. Check if mandatory fields are set.
    $mandatory_fields = array('LastName', 'FirstName');
    if (api_get_setting('registration', 'email') == 'true') {
        $mandatory_fields[] = 'Email';
    }
    $classExistList = array();
    $usergroup = new UserGroup();
    foreach ($users as $user) {
        foreach ($mandatory_fields as $field) {
            if (isset($user[$field])) {
                if (empty($user[$field])) {
                    $user['error'] = get_lang($field . 'Mandatory');
                    $errors[] = $user;
                }
            }
        }
        // 2. Check username, first, check whether it is empty.
        if (isset($user['NewUserName'])) {
            if (!UserManager::is_username_empty($user['NewUserName'])) {
                // 2.1. Check whether username is too long.
                if (UserManager::is_username_too_long($user['NewUserName'])) {
                    $user['error'] = get_lang('UserNameTooLong');
                    $errors[] = $user;
                }
                // 2.2. Check whether the username was used twice in import file.
                if (isset($usernames[$user['NewUserName']])) {
                    $user['error'] = get_lang('UserNameUsedTwice');
                    $errors[] = $user;
                }
                $usernames[$user['UserName']] = 1;
                // 2.3. Check whether username is allready occupied.
                if (!UserManager::is_username_available($user['NewUserName']) && $user['NewUserName'] != $user['UserName']) {
                    $user['error'] = get_lang('UserNameNotAvailable');
                    $errors[] = $user;
                }
            }
        }
        // 3. Check status.
        if (isset($user['Status']) && !api_status_exists($user['Status'])) {
            $user['error'] = get_lang('WrongStatus');
            $errors[] = $user;
        }
        // 4. Check ClassId
        if (!empty($user['ClassId'])) {
            $classId = explode('|', trim($user['ClassId']));
            foreach ($classId as $id) {
                if (in_array($id, $classExistList)) {
                    continue;
                }
                $info = $usergroup->get($id);
                if (empty($info)) {
                    $user['error'] = sprintf(get_lang('ClassIdDoesntExists'), $id);
                    $errors[] = $user;
                } else {
                    $classExistList[] = $info['id'];
                }
            }
        }
        // 5. Check authentication source
        if (!empty($user['AuthSource'])) {
            if (!in_array($user['AuthSource'], $defined_auth_sources)) {
                $user['error'] = get_lang('AuthSourceNotAvailable');
                $errors[] = $user;
            }
        }
    }
    return $errors;
}
Beispiel #8
0
/**
 * Adds a user to the Dokeos database or updates its data
 * @param	string	username (and uid inside LDAP)
 * @author	Mustapha Alouani
 */
function ldap_add_user($login)
{
    global $ldap_basedn, $ldap_host, $ldap_port, $ldap_rdn, $ldap_pass;
    $ds = ldap_connect($ldap_host, $ldap_port);
    ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
    ldap_set_option($ds, LDAP_OPT_REFERRALS, 0);
    if ($ds) {
        $str_query = "(uid=" . $login . ")";
        $r = false;
        $res = ldap_handle_bind($ds, $r);
        $sr = ldap_search($ds, $ldap_basedn, $str_query);
        //echo "Le nombre de resultats est : ".ldap_count_entries($ds,$sr)."<p>";
        $info = ldap_get_entries($ds, $sr);
        for ($key = 0; $key < $info['count']; $key++) {
            $lastname = api_convert_encoding($info[$key]['sn'][0], api_get_system_encoding(), 'UTF-8');
            $firstname = api_convert_encoding($info[$key]['givenname'][0], api_get_system_encoding(), 'UTF-8');
            $email = $info[$key]['mail'][0];
            // Get uid from dn
            $dn_array = ldap_explode_dn($info[$key]['dn'], 1);
            $username = $dn_array[0];
            // uid is first key
            $outab[] = $info[$key]['edupersonprimaryaffiliation'][0];
            // Ici "student"
            //$val = ldap_get_values_len($ds, $entry, "userPassword");
            //$val = ldap_get_values_len($ds, $info[$key], "userPassword");
            //$password = $val[0];
            // TODO the password, if encrypted at the source, will be encrypted twice, which makes it useless. Try to fix that.
            $password = $info[$key]['userPassword'][0];
            $structure = $info[$key]['edupersonprimaryorgunitdn'][0];
            $array_structure = explode(",", $structure);
            $array_val = explode("=", $array_structure[0]);
            $etape = $array_val[1];
            $array_val = explode("=", $array_structure[1]);
            $annee = $array_val[1];
            // Pour faciliter la gestion on ajoute le code "etape-annee"
            $official_code = $etape . "-" . $annee;
            $auth_source = 'ldap';
            // Pas de date d'expiration d'etudiant (a recuperer par rapport au shadow expire LDAP)
            $expiration_date = '0000-00-00 00:00:00';
            $active = 1;
            if (empty($status)) {
                $status = 5;
            }
            if (empty($phone)) {
                $phone = '';
            }
            if (empty($picture_uri)) {
                $picture_uri = '';
            }
            // Ajout de l'utilisateur
            if (UserManager::is_username_available($username)) {
                $user_id = UserManager::create_user($firstname, $lastname, $status, $email, $username, $password, $official_code, api_get_setting('platformLanguage'), $phone, $picture_uri, $auth_source, $expiration_date, $active);
            } else {
                $user = UserManager::get_user_info($username);
                $user_id = $user['user_id'];
                UserManager::update_user($user_id, $firstname, $lastname, $username, null, null, $email, $status, $official_code, $phone, $picture_uri, $expiration_date, $active);
            }
        }
    } else {
        Display::display_error_message(get_lang('LDAPConnectionError'));
    }
    return $user_id;
}
 // For avoiding complications we go some sort of "PHP4 way" - we convert the input xml-file into UTF-8 before passing it to the parser.
 // Instead of:
 // $root = @simplexml_load_file($_FILES['import_file']['tmp_name']);
 // we may use the following construct:
 // $root = @simplexml_load_string(api_utf8_encode_xml(file_get_contents($_FILES['import_file']['tmp_name'])));
 // To ease debugging let us use:
 $content = file_get_contents($_FILES['import_file']['tmp_name']);
 $content = api_utf8_encode_xml($content);
 $root = @simplexml_load_string($content);
 unset($content);
 if (is_object($root)) {
     if (count($root->Users->User) > 0) {
         // Creating/updating users from <Sessions> <Users> base node.
         foreach ($root->Users->User as $node_user) {
             $username = $username_old = trim(api_utf8_decode($node_user->Username));
             if (UserManager::is_username_available($username)) {
                 $password = api_utf8_decode($node_user->Password);
                 if (empty($password)) {
                     $password = api_generate_password();
                 }
                 switch ($node_user->Status) {
                     case 'student':
                         $status = 5;
                         break;
                     case 'teacher':
                         $status = 1;
                         break;
                     default:
                         $status = 5;
                         $error_message .= get_lang('StudentStatusWasGivenTo') . ' : ' . $username . '<br />';
                 }
Beispiel #10
0
function ldap_add_user_by_array($data, $update_if_exists = true)
{
    $lastname = api_convert_encoding($data['sn'][0], api_get_system_encoding(), 'UTF-8');
    $firstname = api_convert_encoding($data['cn'][0], api_get_system_encoding(), 'UTF-8');
    $email = $data['mail'][0];
    // Get uid from dn
    $dn_array = ldap_explode_dn($data['dn'], 1);
    $username = $dn_array[0];
    // uid is first key
    $outab[] = $data['edupersonprimaryaffiliation'][0];
    // Here, "student"
    //$val = ldap_get_values_len($ds, $entry, "userPassword");
    //$val = ldap_get_values_len($ds, $data, "userPassword");
    //$password = $val[0];
    // TODO the password, if encrypted at the source, will be encrypted twice, which makes it useless. Try to fix that.
    $password = $data['userPassword'][0];
    $structure = $data['edupersonprimaryorgunitdn'][0];
    $array_structure = explode(",", $structure);
    $array_val = explode("=", $array_structure[0]);
    $etape = $array_val[1];
    $array_val = explode("=", $array_structure[1]);
    $annee = $array_val[1];
    // To ease management, we add the step-year (etape-annee) code
    $official_code = $etape . "-" . $annee;
    $auth_source = 'ldap';
    // No expiration date for students (recover from LDAP's shadow expiry)
    $expiration_date = '0000-00-00 00:00:00';
    $active = 1;
    if (empty($status)) {
        $status = 5;
    }
    if (empty($phone)) {
        $phone = '';
    }
    if (empty($picture_uri)) {
        $picture_uri = '';
    }
    // Adding user
    $user_id = 0;
    if (UserManager::is_username_available($username)) {
        $user_id = UserManager::create_user($firstname, $lastname, $status, $email, $username, $password, $official_code, api_get_setting('platformLanguage'), $phone, $picture_uri, $auth_source, $expiration_date, $active);
    } else {
        if ($update_if_exists) {
            $user = UserManager::get_user_info($username);
            $user_id = $user['user_id'];
            UserManager::update_user($user_id, $firstname, $lastname, $username, null, null, $email, $status, $official_code, $phone, $picture_uri, $expiration_date, $active);
        }
    }
    return $user_id;
}
Beispiel #11
0
 /**
  * This function checks whether some users in the uploaded file
  * repeated and creates unique usernames if necesary.
  * A case: Within the file there is an user repeted twice (Julio Montoya / Julio Montoya)
  * and the username fields are empty.
  * Then, this function would create unique usernames based on the first and the last name.
  * Two users wiould be created - jmontoya and jmontoya2.
  * Of course, if in the database there is a user with the name jmontoya,
  * the newly created two users registered would be jmontoya2 and jmontoya3.
  * @param $users list of users
  * @author Julio Montoya Armas
  */
 function check_all_usernames($users, $course_list, $id_session)
 {
     $table_user = Database::get_main_table(TABLE_MAIN_USER);
     $usernames = array();
     $new_users = array();
     foreach ($users as $index => $user) {
         $desired_username = array();
         if (empty($user['UserName'])) {
             $desired_username = MySpace::make_username($user['FirstName'], $user['LastName'], '');
             $pre_username = $desired_username['username'] . $desired_username['sufix'];
             $user['UserName'] = $pre_username;
             $user['create'] = '1';
         } else {
             if (UserManager::is_username_available($user['UserName'])) {
                 $desired_username = MySpace::make_username($user['FirstName'], $user['LastName'], $user['UserName']);
                 $user['UserName'] = $desired_username['username'] . $desired_username['sufix'];
                 $user['create'] = '1';
             } else {
                 $is_session_avail = MySpace::user_available_in_session($user['UserName'], $course_list, $id_session);
                 if ($is_session_avail == 0) {
                     $user_name = $user['UserName'];
                     $sql_select = "SELECT user_id FROM {$table_user} WHERE username ='******' ";
                     $rs = Database::query($sql_select);
                     $user['create'] = Database::result($rs, 0, 0);
                     // This should be the ID because the user exists.
                 } else {
                     $user['create'] = $is_session_avail;
                 }
             }
         }
         // Usernames is the current list of users in the file.
         $result_array = MySpace::check_user_in_array($usernames, $desired_username);
         $usernames = $result_array[0];
         $desired_username = $result_array[1];
         $user['UserName'] = $desired_username['username'] . $desired_username['sufix'];
         $new_users[] = $user;
     }
     return $new_users;
 }
 /**
  * Manage the user creation, including checking if the user hasn't been
  * created previously
  * @param array User data
  * @param object List of migrated things
  * @return array User info (from Chamilo DB)
  */
 static function create_user($data, &$omigrate = null)
 {
     //error_log('In create_user, receiving '.print_r($data,1));
     if (empty($data['uidIdPersona'])) {
         error_log('User does not have a uidIdPersona');
         error_log(print_r($data, 1));
         return false;
         //exit;
     }
     $data['uidIdPersona'] = strtoupper($data['uidIdPersona']);
     $data['status'] = STUDENT;
     if (isset($data['uidIdEmpleado'])) {
         $data['status'] = COURSEMANAGER;
     }
     if (!isset($data['username']) || empty($data['username'])) {
         $data['firstname'] = (string) trim($data['firstname']);
         $data['lastname'] = (string) trim($data['lastname']);
         if (empty($data['firstname']) && empty($data['lastname'])) {
             $wanted_user_name = UserManager::purify_username($data['uidIdPersona']);
             //$wanted_user_name = UserManager::create_unique_username(null, null);
         } else {
             $wanted_user_name = UserManager::create_username($data['firstname'], $data['lastname']);
         }
         $extra_data = UserManager::get_extra_user_data_by_value('uidIdPersona', $data['uidIdPersona']);
         if ($extra_data) {
             $user_info = api_get_user_info($extra_data[0]);
             //print_r($extra_data);
             //error_log("User_already_added - {$user_info['user_id']}  - {$user_info['username']} - {$user_info['firstname']} - {$user_info['lastname']}");
             return $user_info;
         }
         if (UserManager::is_username_available($wanted_user_name)) {
             $data['username'] = $wanted_user_name;
             error_log("username available  {$wanted_user_name}");
         } else {
             //the user already exists?
             $user_info = UserManager::get_user_info_simple($wanted_user_name);
             $user_persona = UserManager::get_extra_user_data_by_field($user_info['user_id'], 'uidIdPersona');
             if (isset($user_persona['uidIdPersona']) && $data['uidIdPersona'] == $user_persona['uidIdPersona']) {
                 error_log("Skip user already added: {$user_info['username']}");
                 return $user_info;
             } else {
                 error_log("Homonym - wanted_username: {$wanted_user_name} - uidIdPersona: {$user_persona['uidIdPersona']} - username: {$user_info['username']}");
                 //print_r($data);
                 //The user has the same firstname and lastname but it has another uiIdPersona could by an homonym
                 $data['username'] = UserManager::create_unique_username($data['firstname'], $data['lastname']);
                 error_log("homonym username created " . $data['username']);
             }
         }
         if (empty($data['username'])) {
             //Last chance to have a nice username
             if (empty($data['firstname']) && empty($data['lastname'])) {
                 $data['username'] = UserManager::create_unique_username(uniqid());
                 error_log("username empty 1" . $data['username']);
             } else {
                 $data['username'] = UserManager::create_unique_username($data['firstname'], $data['lastname']);
                 error_log("username empty 2" . $data['username']);
             }
         }
     } else {
         if (UserManager::is_username_available($data['username'])) {
             //error_log("username available {$data['username']} ");
         } else {
             //the user already exists?
             $user_info = UserManager::get_user_info_simple($data['username']);
             $user_persona = UserManager::get_extra_user_data_by_field($user_info['user_id'], 'uidIdPersona');
             if (isset($user_persona['uidIdPersona']) && (string) $data['uidIdPersona'] == (string) $user_persona['uidIdPersona']) {
                 //error_log("2 Skip user already added: {$user_info['username']}");
                 return $user_info;
             } else {
                 //print_r($user_persona);
                 //error_log("2 homonym - wanted_username: {$data['username']} - uidIdPersona: {$user_persona['uidIdPersona']} - username: {$user_info['username']}");
                 //print_r($data);
                 //The user has the same firstname and lastname but it has another uiIdPersona could by an homonym
                 $data['username'] = UserManager::create_unique_username($data['firstname'], $data['lastname']);
                 //error_log("2 homonym username created ". $data['username']);
             }
         }
     }
     if (empty($data['username'])) {
         error_log('No Username provided');
         error_log(print_r($data, 1));
         return false;
         //exit;
     }
     $id_persona = $data['uidIdPersona'];
     unset($data['uidIdPersona']);
     unset($data['uidIdAlumno']);
     unset($data['uidIdEmpleado']);
     $data['encrypt_method'] = 'sha1';
     global $api_failureList;
     $api_failureList = array();
     //error_log(print_r($data, 1));
     $user_info = UserManager::add($data);
     if (!$user_info) {
         error_log('User ' . $id_persona . ' could not be inserted (maybe duplicate?)');
     } else {
         //error_log('User '.$id_persona.' was created as user '.$user_info['user_id']);
     }
     if (is_array($omigrate) && isset($omigrate) && $omigrate['boost_users']) {
         $omigrate['users'][$id_persona] = $user_info['user_id'];
     }
     UserManager::update_extra_field_value($user_info['user_id'], 'uidIdPersona', $id_persona);
     return $user_info;
 }
Beispiel #13
0
/**
 * Insert users from an array of user fields
 */
function extldap_add_user_by_array($data, $update_if_exists = true)
{
    global $extldap_user_correspondance;
    $lastname = api_convert_encoding($data[$extldap_user_correspondance['lastname']][0], api_get_system_encoding(), 'UTF-8');
    $firstname = api_convert_encoding($data[$extldap_user_correspondance['firstname']][0], api_get_system_encoding(), 'UTF-8');
    $email = $data[$extldap_user_correspondance['email']][0];
    $username = $data[$extldap_user_correspondance['username']][0];
    // TODO the password, if encrypted at the source, will be encrypted twice, which makes it useless. Try to fix that.
    $passwordKey = isset($extldap_user_correspondance['password']) ? $extldap_user_correspondance['password'] : '******';
    $password = $data[$passwordKey][0];
    // To ease management, we add the step-year (etape-annee) code
    //$official_code = $etape."-".$annee;
    $official_code = api_convert_encoding($data[$extldap_user_correspondance['official_code']][0], api_get_system_encoding(), 'UTF-8');
    $auth_source = 'ldap';
    // No expiration date for students (recover from LDAP's shadow expiry)
    $expiration_date = '0000-00-00 00:00:00';
    $active = 1;
    if (empty($status)) {
        $status = 5;
    }
    if (empty($phone)) {
        $phone = '';
    }
    if (empty($picture_uri)) {
        $picture_uri = '';
    }
    // Adding user
    $user_id = 0;
    if (UserManager::is_username_available($username)) {
        //echo "$username\n";
        $user_id = UserManager::create_user($firstname, $lastname, $status, $email, $username, $password, $official_code, api_get_setting('platformLanguage'), $phone, $picture_uri, $auth_source, $expiration_date, $active);
    } else {
        if ($update_if_exists) {
            $user = UserManager::get_user_info($username);
            $user_id = $user['user_id'];
            //echo "$username\n";
            UserManager::update_user($user_id, $firstname, $lastname, $username, null, null, $email, $status, $official_code, $phone, $picture_uri, $expiration_date, $active);
        }
    }
    return $user_id;
}
 /**
  * Generated from @assert ('xyzxyzxyz') === true.
  *
  * @covers UserManager::is_username_available
  */
 public function testIs_username_available2()
 {
     $this->assertSame(true, UserManager::is_username_available('xyzxyzxyz'));
 }