/** * Open user account using SREG & AX data if available * If no matching user found and create flag is true, creates new user account * * @access private * @param object &$resp An OpenID consumer response object * @param boolean $create_flag - set if account creation permitted, default: true * @uses $CFG * @uses $USER * @uses $openid_tmp_login * @return object The new user */ function _open_account(&$resp, $create_flag = true) { global $CFG, $USER, $openid_tmp_login; $url = $resp->identity_url; $password = hash_internal_user_password('openid'); $server = $resp->endpoint->server_url; $user = openid_resp_to_user($resp); if ($user == false) { // multiple matches to users! Don't know which user to pick. print_error('auth_openid_multiple_matches', 'auth_openid'); return false; // won't get here. } if (isset($user->id)) { $openid_tmp_login = true; $openid_action = 'change'; if ($user->auth == 'openid') { if (empty($this->config->auth_openid_allow_muliple)) { print_error('auth_openid_no_multiple', 'auth_openid'); return false; } $openid_action = 'append'; } else { if (empty($this->config->auth_openid_confirm_switch)) { openid_if_unique_change_account($user, $url); return $USER; } } $USER = clone $user; // To clone or not to clone //$mode = optional_param('openid_mode', null); //error_log("auth/openid/auth.php::_open_account() setting openid_mode={$mode} (openid_process_url={$openid_process_url})"); redirect("{$CFG->wwwroot}/auth/openid/actions.php?openid_tmp_login=1&openid_action={$openid_action}&openid_url={$url}"); // Try to get it not to make second request to be accepted, double confirm - TBD: openid_mode=??? } if (!$create_flag) { // Error: This site is configured to disallow new users via OpenID print_error('auth_openid_require_account', 'auth_openid'); return false; // won't get here. } $usertmp = create_user_record($user->username, $password, 'openid'); $user->id = $usertmp->id; openid_append_url($user, $url); if (!isset($user->city) || $user->city == '') { //use "*" as the default city name $user->city = '*'; } if (empty($user->country) && !empty($CFG->country)) { //use the configured default country code $user->country = $CFG->country; } if (empty($user->country)) { //out of other options, to try to copy the admin's country if ($admin = get_admin()) { $user->country = $admin->country; } } update_record('user', $user); $user = get_complete_user_data('id', $user->id); events_trigger('user_created', $user); // BJB120125 - moved from below redirect for alfresco, etc... if (function_exists('on_openid_create_account')) { on_openid_create_account($resp, $user); } // Redirect the user to their profile page if not set up properly if (!empty($user) && user_not_fully_set_up($user)) { $USER = clone $user; $urltogo = $CFG->wwwroot . '/user/edit.php'; redirect($urltogo); } if (openid_server_requires_confirm($server, $this->config)) { $secret = random_string(15); set_field('user', 'secret', $secret, 'id', $user->id); $user->secret = $secret; set_field('user', 'confirmed', 0, 'id', $user->id); $user->confirmed = 0; openid_send_confirmation_email($user); } return $user; }
/** * Create a new account using simple registration data if available * * @access private * @param object &$resp An OpenID consumer response object * @return object The new user */ function _create_account(&$resp) { global $CFG, $USER; $url = $resp->identity_url; $password = hash_internal_user_password('openid'); $server = $resp->endpoint->server_url; $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($resp); $sreg = $sreg_resp->contents(); // We'll attempt to use the user's nickname to set their username if (isset($sreg['nickname']) && !empty($sreg['nickname']) && !record_exists('users', 'username', $sreg['nickname'])) { $username = $sreg['nickname']; } else { $username = openid_normalize_url_as_username($url); } create_user_record($username, $password, 'openid'); $user = get_complete_user_data('username', $username); openid_append_url($user, $url); // SREG fullname if (isset($sreg['fullname']) && !empty($sreg['fullname'])) { $name = openid_parse_full_name($sreg['fullname']); $user->firstname = $name['first']; $user->lastname = $name['last']; } // SREG email if (isset($sreg['email']) && !empty($sreg['email']) && !record_exists('user', 'email', $sreg['email'])) { $user->email = $sreg['email']; } // SREG country if (isset($sreg['country']) && !empty($sreg['country'])) { $country = $sreg['country']; $country_code = strtoupper($country); $countries = get_list_of_countries(); if (strlen($country) != 2 || !isset($countries[$country_code])) { $countries_keys = array_keys($countries); $countries_vals = array_values($countries); $country_code = array_search($country, $countries_vals); if ($country_code > 0) { $country_code = $countries_keys[$country_code]; } else { $country_code = ''; } } if (!empty($country_code)) { $user->country = $country_code; } } /* We're currently not attempting to get language and timezone values // SREG language if (isset($sreg['language']) && !empty($sreg['language'])) { } // SREG timezone if (isset($sreg['timezone']) && !empty($sreg['timezone'])) { } */ if (function_exists('on_openid_create_account')) { on_openid_create_account($resp, $user); } update_record('user', $user); $user = get_complete_user_data('id', $user->id); // Redirect the user to their profile page if not set up properly if (!empty($user) && user_not_fully_set_up($user)) { $USER = clone $user; $urltogo = $CFG->wwwroot . '/user/edit.php'; redirect($urltogo); } return $user; }