本文整理汇总了PHP中create_user_record函数的典型用法代码示例。如果您正苦于以下问题:PHP create_user_record函数的具体用法?PHP create_user_record怎么用?PHP create_user_record使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了create_user_record函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: create_demostudent_account
/**
* Create a DemoStudent account which cannot log in manually, using the given username,
* @param string demostudentusername username of account to create
* @return object the user object
*/
function create_demostudent_account($demostudentusername)
{
global $USER, $DB, $passwordfiller;
$demostudentuser = create_user_record($demostudentusername, '');
if ($demostudentuser) {
$demostudentuser->firstname = "DemoStudent";
$demostudentuser->lastname = $USER->lastname;
$demostudentuser->description = "DemoStudent account.";
$demostudentuser->email = $USER->email;
$demostudentuser->password = $passwordfiller;
$DB->update_record('user', $demostudentuser);
}
return $demostudentuser;
}
示例2: loginpage_hook
/**
* Provides a hook into the login page.
*
* @param object &$frm Form object.
* @param object &$user User object.
*/
public function loginpage_hook(&$frm, &$user)
{
global $DB;
if (empty($frm)) {
$frm = data_submitted();
}
if (empty($frm)) {
return true;
}
$autoappend = get_config('auth_oidc', 'autoappend');
if (empty($autoappend)) {
// If we're not doing autoappend, just let things flow naturally.
return true;
}
$username = $frm->username;
$password = $frm->password;
$auth = 'oidc';
$existinguser = $DB->get_record('user', ['username' => $username]);
if (!empty($existinguser)) {
// We don't want to prevent access to existing accounts.
return true;
}
$username .= $autoappend;
$success = $this->user_login($username, $password);
if ($success !== true) {
// No o365 user, continue normally.
return false;
}
$existinguser = $DB->get_record('user', ['username' => $username]);
if (!empty($existinguser)) {
$user = $existinguser;
return true;
}
// The user is authenticated but user creation may be disabled.
if (!empty($CFG->authpreventaccountcreation)) {
$failurereason = AUTH_LOGIN_UNAUTHORISED;
// Trigger login failed event.
$event = \core\event\user_login_failed::create(array('other' => array('username' => $username, 'reason' => $failurereason)));
$event->trigger();
error_log('[client ' . getremoteaddr() . "] {$CFG->wwwroot} Unknown user, can not create new accounts: {$username} " . $_SERVER['HTTP_USER_AGENT']);
return false;
}
$user = create_user_record($username, $password, $auth);
return true;
}
示例3: process_turnitintool_courses
protected function process_turnitintool_courses($data)
{
global $CFG, $DB;
$data = (object) $data;
$oldid = $data->id;
$data->courseid = $this->get_courseid();
$owneremail = empty($data->owneremail) ? join(array_splice(explode(".", $data->ownerun), 0, -1)) : $data->owneremail;
$owner = $DB->get_record('user', array('email' => $owneremail));
if ($owner) {
$data->ownerid = $owner->id;
} else {
// Turnitin class owner not found from email address etc create user account
$i = 0;
$newuser = false;
while (!is_object($newuser)) {
// Keep trying to create a new username
$username = $i == 0 ? $data->ownerun : $data->ownerun . '_' . $i;
// Append number if username exists
$i++;
$newuser = create_user_record($username, substr($i . "-" . md5($username), 0, 8));
if (is_object($newuser)) {
$newuser->email = $owneremail;
$newuser->firstname = $data->ownerfn;
$newuser->lastname = $data->ownerln;
if (!$DB->update_record("user", $newuser)) {
turnitintool_print_error('userupdateerror', 'turnitintool');
}
}
}
$data->ownerid = $newuser->id;
}
$tiiowner = new object();
$tiiowner->userid = $data->ownerid;
$tiiowner->turnitin_uid = $data->ownertiiuid;
if (!($tiiuser = $DB->get_record('turnitintool_users', array('userid' => $data->ownerid)))) {
$DB->insert_record('turnitintool_users', $tiiowner);
}
if (!$DB->get_records_select('turnitintool_courses', 'courseid=' . $data->courseid)) {
$newitemid = $DB->insert_record('turnitintool_courses', $data);
$this->set_mapping('turnitintool_courses', $oldid, $newitemid);
}
}
示例4: auth_onelogin_saml_authenticate_user_login
/**
* Copied from moodlelib:authenticate_user_login()
*
* WHY? because I need to hard code the plugins to auth_saml, and this user
* may be set to any number of other types of login method
*
* First of all - make sure that they aren't nologin - we don't mess with that!
*
*
* Given a username and password, this function looks them
* up using the currently selected authentication mechanism,
* and if the authentication is successful, it returns a
* valid $user object from the 'user' table.
*
* Uses auth_ functions from the currently active auth module
*
* After authenticate_user_login() returns success, you will need to
* log that the user has logged in, and call complete_user_login() to set
* the session up.
*
* @uses $CFG
* @param string $saml_account_matcher Field will be used in order to find the user account.
* @param array $user_saml User's info (with system magic quotes)
* @param boolean $saml_create Auto-provision user
* @param boolean $saml_update Auto-update user
* @return user|flase A {@link $USER} object or false if error
*/
function auth_onelogin_saml_authenticate_user_login($saml_account_matcher, $user_saml, $saml_create = false, $saml_update = false)
{
global $CFG, $DB;
// ensure that only saml auth module is chosen
$authsenabled = get_enabled_auth_plugins();
$password = time();
$created = false;
if ($user = get_complete_user_data($saml_account_matcher, $user_saml[$saml_account_matcher])) {
$auth = empty($user->auth) ? 'manual' : $user->auth;
// use manual if auth not set
if ($auth == 'nologin' or !is_enabled_auth($auth)) {
add_to_log(0, 'login', 'error', 'index.php', $user_saml[$saml_account_matcher]);
print_error('[client ' . getremoteaddr() . '] ' . $CFG->wwwroot . ' ---> DISABLED LOGIN: ' . $user_saml[$saml_account_matcher] . ' ' . $_SERVER['HTTP_USER_AGENT']);
return false;
}
} else {
// check if there's a deleted record (cheaply)
$query_conditions[$saml_account_matcher] = $user_saml[$saml_account_matcher];
$query_conditions['deleted'] = 1;
if ($DB->get_field('user', 'id', $query_conditions)) {
print_error('[client ' . $_SERVER['REMOTE_ADDR'] . '] ' . $CFG->wwwroot . ' ---> DELETED LOGIN: ' . $user_saml[$saml_account_matcher] . ' ' . $_SERVER['HTTP_USER_AGENT']);
return false;
}
$auths = $authsenabled;
$user = new object();
$user->id = 0;
// User does not exist
}
// hard code SAML
$auths = array('onelogin_saml');
foreach ($auths as $auth) {
$authplugin = get_auth_plugin($auth);
// on auth fail fall through to the next plugin
if (!$authplugin->user_login($user_saml[$saml_account_matcher], $password)) {
continue;
}
if (!$user->id) {
// if user not found,
// create him
if ($saml_create) {
$user = create_user_record($user_saml[$saml_account_matcher], $password, $auth);
$authplugin->sync_roles($user);
$created = true;
}
}
if ($user->id && !$created) {
if (empty($user->auth)) {
// For some reason auth isn't set yet
$query_conditions['id'] = $user->id;
$DB->set_field('user', 'auth', $auth, $query_conditions);
$user->auth = $auth;
}
// User already exists in database
if ($saml_update) {
if (empty($user->firstaccess)) {
//prevent firstaccess from remaining 0 for manual account that never required confirmation
$query_conditions['id'] = $user->id;
$DB->set_field('user', 'firstaccess', $user->timemodified, $query_conditions);
$user->firstaccess = $user->timemodified;
}
if (!empty($user_saml['username']) && $user->username != $user_saml['username']) {
$query_conditions['id'] = $user->id;
$DB->set_field('user', 'username', $user_saml['username'], $query_conditions);
$user->email = $user_saml['username'];
}
if (!empty($user_saml['email']) && $user->email != $user_saml['email']) {
$query_conditions['id'] = $user->id;
$DB->set_field('user', 'email', $user_saml['email'], $query_conditions);
$user->email = $user_saml['email'];
}
if (!empty($user_saml['firstname']) && $user->firstname != $user_saml['firstname']) {
$query_conditions['id'] = $user->id;
$DB->set_field('user', 'firstname', $user_saml['firstname'], $query_conditions);
//.........这里部分代码省略.........
示例5: authenticate_user_login
//.........这里部分代码省略.........
// User does not exist.
$auths = $authsenabled;
$user = new stdClass();
$user->id = 0;
}
if ($ignorelockout) {
// Some other mechanism protects against brute force password guessing, for example login form might include reCAPTCHA
// or this function is called from a SSO script.
} else {
if ($user->id) {
// Verify login lockout after other ways that may prevent user login.
if (login_is_lockedout($user)) {
$failurereason = AUTH_LOGIN_LOCKOUT;
// Trigger login failed event.
$event = \core\event\user_login_failed::create(array('userid' => $user->id, 'other' => array('username' => $username, 'reason' => $failurereason)));
$event->trigger();
error_log('[client ' . getremoteaddr() . "] {$CFG->wwwroot} Login lockout: {$username} " . $_SERVER['HTTP_USER_AGENT']);
return false;
}
} else {
// We can not lockout non-existing accounts.
}
}
foreach ($auths as $auth) {
$authplugin = get_auth_plugin($auth);
// On auth fail fall through to the next plugin.
if (!$authplugin->user_login($username, $password)) {
continue;
}
// Successful authentication.
if ($user->id) {
// User already exists in database.
if (empty($user->auth)) {
// For some reason auth isn't set yet.
$DB->set_field('user', 'auth', $auth, array('id' => $user->id));
$user->auth = $auth;
}
// If the existing hash is using an out-of-date algorithm (or the legacy md5 algorithm), then we should update to
// the current hash algorithm while we have access to the user's password.
update_internal_user_password($user, $password);
if ($authplugin->is_synchronised_with_external()) {
// Update user record from external DB.
$user = update_user_record_by_id($user->id);
}
} else {
// The user is authenticated but user creation may be disabled.
if (!empty($CFG->authpreventaccountcreation)) {
$failurereason = AUTH_LOGIN_UNAUTHORISED;
// Trigger login failed event.
$event = \core\event\user_login_failed::create(array('other' => array('username' => $username, 'reason' => $failurereason)));
$event->trigger();
error_log('[client ' . getremoteaddr() . "] {$CFG->wwwroot} Unknown user, can not create new accounts: {$username} " . $_SERVER['HTTP_USER_AGENT']);
return false;
} else {
$user = create_user_record($username, $password, $auth);
}
}
$authplugin->sync_roles($user);
foreach ($authsenabled as $hau) {
$hauth = get_auth_plugin($hau);
$hauth->user_authenticated_hook($user, $username, $password);
}
if (empty($user->id)) {
$failurereason = AUTH_LOGIN_NOUSER;
// Trigger login failed event.
$event = \core\event\user_login_failed::create(array('other' => array('username' => $username, 'reason' => $failurereason)));
$event->trigger();
return false;
}
if (!empty($user->suspended)) {
// Just in case some auth plugin suspended account.
$failurereason = AUTH_LOGIN_SUSPENDED;
// Trigger login failed event.
$event = \core\event\user_login_failed::create(array('userid' => $user->id, 'other' => array('username' => $username, 'reason' => $failurereason)));
$event->trigger();
error_log('[client ' . getremoteaddr() . "] {$CFG->wwwroot} Suspended Login: {$username} " . $_SERVER['HTTP_USER_AGENT']);
return false;
}
login_attempt_valid($user);
$failurereason = AUTH_LOGIN_OK;
return $user;
}
// Failed if all the plugins have failed.
if (debugging('', DEBUG_ALL)) {
error_log('[client ' . getremoteaddr() . "] {$CFG->wwwroot} Failed Login: {$username} " . $_SERVER['HTTP_USER_AGENT']);
}
if ($user->id) {
login_attempt_failed($user);
$failurereason = AUTH_LOGIN_FAILED;
// Trigger login failed event.
$event = \core\event\user_login_failed::create(array('userid' => $user->id, 'other' => array('username' => $username, 'reason' => $failurereason)));
$event->trigger();
} else {
$failurereason = AUTH_LOGIN_NOUSER;
// Trigger login failed event.
$event = \core\event\user_login_failed::create(array('other' => array('username' => $username, 'reason' => $failurereason)));
$event->trigger();
}
return false;
}
示例6: define
require_once $CFG->libdir . '/gdlib.php';
// True to download a gravatar.
define('MDK_AVATAR', true);
$data = "s1,test,Eric,Cartman,s1@example.com\ns2,test,Stan,Marsh,s2@example.com\ns3,test,Kyle,Broflovski,s3@example.com\ns4,test,Kenny,McCormick,s4@example.com\ns5,test,Butters,Stotch,s5@example.com\ns6,test,Clyde,Donovan,s6@example.com\ns7,test,Jimmy,Valmer,s7@example.com\ns8,test,Timmy,Burch,s8@example.com\ns9,test,Wendy,Testaburger,s9@example.com\ns10,test,Bebe,Stevens,s10@example.com\nt1,test,Herbert,Garrison,t1@example.com\nt2,test,Sheila,Brovslovski,t2@example.com\nt3,test,Liane,Cartman,t3@example.com\nm1,test,Officer,Barbady,m1@example.com\nm2,test,Principal,Victoria,m2@example.com\nm3,test,Randy,Marsh,m3@example.com";
$users = explode("\n", $data);
// Create all the users.
foreach ($users as $user) {
if (empty($user)) {
continue;
}
$user = explode(',', $user);
if ($DB->record_exists('user', array('username' => $user[0], 'deleted' => 0))) {
continue;
}
mtrace('Creating user ' . $user[0]);
$u = create_user_record($user[0], $user[1]);
$u->firstname = $user[2];
$u->lastname = $user[3];
$u->email = $user[4];
$u->city = 'Perth';
$u->country = 'AU';
$u->lang = 'en';
$u->description = 'Who\'s your daddy?';
$u->url = 'http://moodle.org';
$u->idnumber = '';
$u->institution = 'Moodle HQ';
$u->department = 'Rock on!';
$u->phone1 = '';
$u->phone2 = '';
$u->address = '';
// Adds an avatar to the user. Will slow down the process.
示例7: authenticate_user_login
/**
* Authenticates a user against the chosen authentication mechanism
*
* Given a username and password, this function looks them
* up using the currently selected authentication mechanism,
* and if the authentication is successful, it returns a
* valid $user object from the 'user' table.
*
* Uses auth_ functions from the currently active auth module
*
* After authenticate_user_login() returns success, you will need to
* log that the user has logged in, and call complete_user_login() to set
* the session up.
*
* Note: this function works only with non-mnet accounts!
*
* @param string $username User's username
* @param string $password User's password
* @return user|flase A {@link $USER} object or false if error
*/
function authenticate_user_login($username, $password)
{
global $CFG, $DB;
$authsenabled = get_enabled_auth_plugins();
if ($user = get_complete_user_data('username', $username, $CFG->mnet_localhost_id)) {
$auth = empty($user->auth) ? 'manual' : $user->auth;
// use manual if auth not set
if (!empty($user->suspended)) {
add_to_log(SITEID, 'login', 'error', 'index.php', $username);
error_log('[client ' . getremoteaddr() . "] {$CFG->wwwroot} Suspended Login: {$username} " . $_SERVER['HTTP_USER_AGENT']);
return false;
}
if ($auth == 'nologin' or !is_enabled_auth($auth)) {
add_to_log(SITEID, 'login', 'error', 'index.php', $username);
error_log('[client ' . getremoteaddr() . "] {$CFG->wwwroot} Disabled Login: {$username} " . $_SERVER['HTTP_USER_AGENT']);
return false;
}
$auths = array($auth);
} else {
// check if there's a deleted record (cheaply)
if ($DB->get_field('user', 'id', array('username' => $username, 'deleted' => 1))) {
error_log('[client ' . getremoteaddr() . "] {$CFG->wwwroot} Deleted Login: {$username} " . $_SERVER['HTTP_USER_AGENT']);
return false;
}
// User does not exist
$auths = $authsenabled;
$user = new stdClass();
$user->id = 0;
}
foreach ($auths as $auth) {
$authplugin = get_auth_plugin($auth);
// on auth fail fall through to the next plugin
if (!$authplugin->user_login($username, $password)) {
continue;
}
// successful authentication
if ($user->id) {
// User already exists in database
if (empty($user->auth)) {
// For some reason auth isn't set yet
$DB->set_field('user', 'auth', $auth, array('username' => $username));
$user->auth = $auth;
}
if (empty($user->firstaccess)) {
//prevent firstaccess from remaining 0 for manual account that never required confirmation
$DB->set_field('user', 'firstaccess', $user->timemodified, array('id' => $user->id));
$user->firstaccess = $user->timemodified;
}
update_internal_user_password($user, $password);
// just in case salt or encoding were changed (magic quotes too one day)
if ($authplugin->is_synchronised_with_external()) {
// update user record from external DB
$user = update_user_record($username);
}
} else {
// if user not found, create him
$user = create_user_record($username, $password, $auth);
}
$authplugin->sync_roles($user);
foreach ($authsenabled as $hau) {
$hauth = get_auth_plugin($hau);
$hauth->user_authenticated_hook($user, $username, $password);
}
if (empty($user->id)) {
return false;
}
if (!empty($user->suspended)) {
// just in case some auth plugin suspended account
add_to_log(SITEID, 'login', 'error', 'index.php', $username);
error_log('[client ' . getremoteaddr() . "] {$CFG->wwwroot} Suspended Login: {$username} " . $_SERVER['HTTP_USER_AGENT']);
return false;
}
return $user;
}
// failed if all the plugins have failed
add_to_log(SITEID, 'login', 'error', 'index.php', $username);
if (debugging('', DEBUG_ALL)) {
error_log('[client ' . getremoteaddr() . "] {$CFG->wwwroot} Failed Login: {$username} " . $_SERVER['HTTP_USER_AGENT']);
}
return false;
//.........这里部分代码省略.........
示例8: create_user
/**
* Creates an User with given information. Required fields are:
* -username
* -idnumber
* -firstname
* -lastname
* -email
*
* And there's some interesting fields:
* -password
* -auth
* -confirmed
* -timezone
* -country
* -emailstop
* -theme
* -lang
* -mailformat
*
* @param assoc array or object $user
*
* @return string or thrown exceptions
*/
function create_user($user)
{
global $CFG, $DB;
/// WS: convert user array into an user object
if (is_array($user)) {
$user = (object) $user;
}
/// check auth fields
if (!isset($user->auth)) {
$user->auth = 'manual';
} else {
/// check that the auth value exists
$authplugin = get_directory_list($CFG->dirroot . "/auth", '', false, true, false);
if (array_search($user->auth, $authplugin) === false) {
throw new moodle_exception('authnotexisting');
}
}
$required = array('username', 'firstname', 'lastname', 'email', 'password');
foreach ($required as $req) {
if (!isset($user->{$req})) {
throw new moodle_exception('missingrequiredfield');
}
}
$password = hash_internal_user_password($user->password);
$record = create_user_record($user->username, $password, $user->auth);
if ($record) {
$user->id = $record->id;
if ($DB->update_record('user', $user)) {
return $record->id;
} else {
//we could not update properly the newly created user, we need to delete it
$DB->delete_record('user', array('id' => $record->id));
throw new moodle_exception('usernotcreated');
}
}
throw new moodle_exception('usernotcreated');
}
示例9: create_moodle_only_user
function create_moodle_only_user($user_data)
{
global $CFG, $DB;
$username = $user_data['username'];
$username = utf8_decode($username);
$username = strtolower($username);
/* Creamos el nuevo usuario de Moodle si no está creado */
$conditions = array('username' => $username);
$user = $DB->get_record('user', $conditions);
if (!$user) {
$user = create_user_record($username, "", "manual");
}
if (array_key_exists('password', $user_data)) {
$password = utf8_decode($user_data['password']);
} else {
$password = '';
}
if (array_key_exists('email', $user_data)) {
$email = utf8_decode($user_data['email']);
} else {
$email = '';
}
if (array_key_exists('firstname', $user_data)) {
$firstname = utf8_decode($user_data['firstname']);
} else {
$firstname = '';
}
if (array_key_exists('lastname', $user_data)) {
$lastname = utf8_decode($user_data['lastname']);
} else {
$lastname = '';
}
if (array_key_exists('city', $user_data)) {
$city = utf8_decode($user_data['city']);
} else {
$city = '';
}
if (array_key_exists('country', $user_data)) {
$country = utf8_decode($user_data['country']);
} else {
$country = '';
}
$conditions = array('id' => $user->id);
if ($firstname) {
$DB->set_field('user', 'firstname', $firstname, $conditions);
}
if ($lastname) {
$DB->set_field('user', 'lastname', $lastname, $conditions);
}
if ($email) {
$DB->set_field('user', 'email', $email, $conditions);
}
if ($city) {
$DB->set_field('user', 'city', $city, $conditions);
}
if ($country) {
$DB->set_field('user', 'country', $country, $conditions);
}
update_internal_user_password($user, $password);
}
示例10: loginpage_hook
function loginpage_hook()
{
global $CFG, $SESSION, $DB, $USER;
require_once $CFG->dirroot . '/auth/vatsim/config.php';
// initiate the SSO class with consumer details and encryption details
$SSO = new SSO($sso['base'], $sso['key'], $sso['secret'], $sso['method'], $sso['cert']);
// return variable is needed later in this script
$sso_return = $sso['return'];
// remove other config variables
unset($sso);
// if VATSIM has redirected the member back
if (isset($_GET['oauth_verifier']) && !isset($_GET['oauth_cancel'])) {
// check to make sure there is a saved token for this user
if (isset($_SESSION[SSO_SESSION]) && isset($_SESSION[SSO_SESSION]['key']) && isset($_SESSION[SSO_SESSION]['secret'])) {
if (@$_GET['oauth_token'] != $_SESSION[SSO_SESSION]['key']) {
throw new moodle_exception("An error occurred with the login process - please try again", 'auth_vatsim');
}
if (@(!isset($_GET['oauth_verifier']))) {
throw new moodle_exception("An error occurred with the login process", 'auth_vatsim');
}
// obtain the details of this user from VATSIM
$vatsimUser = $SSO->checkLogin($_SESSION[SSO_SESSION]['key'], $_SESSION[SSO_SESSION]['secret'], @$_GET['oauth_verifier']);
if ($vatsimUser) {
// One-time use of tokens, token no longer valid
unset($_SESSION[SSO_SESSION]);
$vatsim = $vatsimUser->user;
//print_r($user->user);
$username = $vatsim->id;
// plugin only designed where email address is returned, if no email specified,
if (@empty($vatsim->email)) {
throw new moodle_exception('noemail', "auth_vatsim");
}
$useremail = $vatsim->email;
// find the user in the current database, by CID, not email
$user = $DB->get_record('user', array('username' => $username, 'deleted' => 0, 'mnethostid' => $CFG->mnet_localhost_id));
// create the user if it doesn't exist
if (empty($user)) {
// deny login if setting "Prevent account creation when authenticating" is on
if ($CFG->authpreventaccountcreation) {
throw new moodle_exception("noaccountyet", "auth_vatsim");
}
//retrieve more information from the provider
$newuser = new stdClass();
$newuser->email = $useremail;
$newuser->firstname = $vatsim->name_first;
$newuser->lastname = $vatsim->name_last;
$newuser->country = $vatsim->country->code;
create_user_record($username, '', 'vatsim');
} else {
$username = $user->username;
}
add_to_log(SITEID, 'auth_vatsim', '', '', $username . '/' . $useremail);
$user = authenticate_user_login($username, null);
if ($user) {
//prefill more user information if new user
if (!empty($newuser)) {
$newuser->id = $user->id;
$DB->update_record('user', $newuser);
$user = (object) array_merge((array) $user, (array) $newuser);
}
complete_user_login($user);
// Redirection
if (user_not_fully_set_up($USER)) {
$urltogo = $CFG->wwwroot . '/user/edit.php';
// We don't delete $SESSION->wantsurl yet, so we get there later
} else {
if (isset($SESSION->wantsurl) and strpos($SESSION->wantsurl, $CFG->wwwroot) === 0) {
$urltogo = $SESSION->wantsurl;
// Because it's an address in this site
unset($SESSION->wantsurl);
} else {
// No wantsurl stored or external - go to homepage
$urltogo = $CFG->wwwroot . '/';
unset($SESSION->wantsurl);
}
}
redirect($urltogo);
}
} else {
// OAuth or cURL errors have occurred
//$error = $SSO->error();
throw new moodle_exception("An error occurred with the login process", 'auth_vatsim');
}
}
// the user cancelled their login and were sent back
} else {
if (isset($_GET['oauth_cancel'])) {
throw new moodle_exception("You cancelled your login", 'auth_vatsim');
}
}
// create a request token for this login. Provides return URL and suspended/inactive settings
$token = $SSO->requestToken($sso_return, false, false);
if ($token) {
// store the token information in the session so that we can retrieve it when the user returns
$_SESSION[SSO_SESSION] = array('key' => (string) $token->token->oauth_token, 'secret' => (string) $token->token->oauth_token_secret);
// redirect the member to VATSIM
$SSO->sendToVatsim();
} else {
throw new moodle_exception("An error occurred with the login process", 'auth_vatsim');
}
//.........这里部分代码省略.........
示例11: loginpage_hook
//.........这里部分代码省略.........
//retrieve more information from the provider
$newuser = new stdClass();
$newuser->email = $useremail;
switch ($authprovider) {
case 'google':
$params = array();
$params['access_token'] = $accesstoken;
$params['alt'] = 'json';
$userinfo = $curl->get('https://www.googleapis.com/oauth2/v1/userinfo', $params);
$userinfo = json_decode($userinfo);
//email, id, name, verified_email, given_name, family_name, link, gender, locale
$newuser->auth = 'googleoauth2';
if (!empty($userinfo->given_name)) {
$newuser->firstname = $userinfo->given_name;
}
if (!empty($userinfo->family_name)) {
$newuser->lastname = $userinfo->family_name;
}
if (!empty($userinfo->locale)) {
//$newuser->lang = $userinfo->locale;
//TODO: convert the locale into correct Moodle language code
}
break;
case 'facebook':
$newuser->firstname = $facebookuser->first_name;
$newuser->lastname = $facebookuser->last_name;
break;
case 'messenger':
$newuser->firstname = $messengeruser->first_name;
$newuser->lastname = $messengeruser->last_name;
break;
case 'github':
//As Github doesn't provide firstname/lastname, we'll split the name at the first whitespace.
$githubusername = explode(' ', $githubuser->name, 2);
$newuser->firstname = $githubusername[0];
$newuser->lastname = $githubusername[1];
break;
case 'linkedin':
$newuser->firstname = $linkedinuser->firstName;
$newuser->lastname = $linkedinuser->lastName;
$newuser->country = $linkedinuser->country->code;
$newuser->city = $linkedinuser->name;
break;
default:
break;
}
//retrieve country and city if the provider failed to give it
if (!isset($newuser->country) or !isset($newuser->city)) {
$googleipinfodbkey = get_config('auth/googleoauth2', 'googleipinfodbkey');
if (!empty($googleipinfodbkey)) {
$locationdata = $curl->get('http://api.ipinfodb.com/v3/ip-city/?key=' . $googleipinfodbkey . '&ip=' . getremoteaddr() . '&format=json');
$locationdata = json_decode($locationdata);
}
if (!empty($locationdata)) {
//TODO: check that countryCode does match the Moodle country code
$newuser->country = isset($newuser->country) ? isset($newuser->country) : $locationdata->countryCode;
$newuser->city = isset($newuser->city) ? isset($newuser->city) : $locationdata->cityName;
}
}
create_user_record($username, '', 'googleoauth2');
} else {
$username = $user->username;
}
//authenticate the user
//TODO: delete this log later
$userid = empty($user) ? 'new user' : $user->id;
add_to_log(SITEID, 'auth_googleoauth2', '', '', $username . '/' . $useremail . '/' . $userid);
$user = authenticate_user_login($username, null);
if ($user) {
//set a cookie to remember what auth provider was selected
setcookie('MOODLEGOOGLEOAUTH2_' . $CFG->sessioncookie, $authprovider, time() + DAYSECS * 60, $CFG->sessioncookiepath, $CFG->sessioncookiedomain, $CFG->cookiesecure, $CFG->cookiehttponly);
//prefill more user information if new user
if (!empty($newuser)) {
$newuser->id = $user->id;
$DB->update_record('user', $newuser);
$user = (object) array_merge((array) $user, (array) $newuser);
}
complete_user_login($user);
// Redirection
if (user_not_fully_set_up($USER)) {
$urltogo = $CFG->wwwroot . '/user/edit.php';
// We don't delete $SESSION->wantsurl yet, so we get there later
} else {
if (isset($SESSION->wantsurl) and strpos($SESSION->wantsurl, $CFG->wwwroot) === 0) {
$urltogo = $SESSION->wantsurl;
// Because it's an address in this site
unset($SESSION->wantsurl);
} else {
// No wantsurl stored or external - go to homepage
$urltogo = $CFG->wwwroot . '/';
unset($SESSION->wantsurl);
}
}
redirect($urltogo);
}
} else {
throw new moodle_exception('couldnotgetgoogleaccesstoken', 'auth_googleoauth2');
}
}
}
示例12: authenticate_user_login
/**
* Authenticates a user against the chosen authentication mechanism
*
* Given a username and password, this function looks them
* up using the currently selected authentication mechanism,
* and if the authentication is successful, it returns a
* valid $user object from the 'user' table.
*
* Uses auth_ functions from the currently active auth module
*
* After authenticate_user_login() returns success, you will need to
* log that the user has logged in, and call complete_user_login() to set
* the session up.
*
* Note: this function works only with non-mnet accounts!
*
* @param string $username User's username
* @param string $password User's password
* @return user|flase A {@link $USER} object or false if error
*/
function authenticate_user_login($username, $password) {
global $CFG, $DB;
$authsenabled = get_enabled_auth_plugins();
if ($user = get_complete_user_data('username', $username, $CFG->mnet_localhost_id)) {
$auth = empty($user->auth) ? 'manual' : $user->auth; // use manual if auth not set
if (!empty($user->suspended)) {
add_to_log(SITEID, 'login', 'error', 'index.php', $username);
error_log('[client '.getremoteaddr()."] $CFG->wwwroot Suspended Login: $username ".$_SERVER['HTTP_USER_AGENT']);
return false;
}
if ($auth=='nologin' or !is_enabled_auth($auth)) {
add_to_log(SITEID, 'login', 'error', 'index.php', $username);
error_log('[client '.getremoteaddr()."] $CFG->wwwroot Disabled Login: $username ".$_SERVER['HTTP_USER_AGENT']);
return false;
}
$auths = array($auth);
} else {
// Check if there's a deleted record (cheaply), this should not happen because we mangle usernames in delete_user().
if ($DB->get_field('user', 'id', array('username'=>$username, 'mnethostid'=>$CFG->mnet_localhost_id, 'deleted'=>1))) {
error_log('[client '.getremoteaddr()."] $CFG->wwwroot Deleted Login: $username ".$_SERVER['HTTP_USER_AGENT']);
return false;
}
// Do not try to authenticate non-existent accounts when user creation is not disabled.
if (!empty($CFG->authpreventaccountcreation)) {
add_to_log(SITEID, 'login', 'error', 'index.php', $username);
error_log('[client '.getremoteaddr()."] $CFG->wwwroot Unknown user, can not create new accounts: $username ".$_SERVER['HTTP_USER_AGENT']);
return false;
}
// User does not exist
$auths = $authsenabled;
$user = new stdClass();
$user->id = 0;
}
foreach ($auths as $auth) {
$authplugin = get_auth_plugin($auth);
// on auth fail fall through to the next plugin
if (!$authplugin->user_login($username, $password)) {
continue;
}
// successful authentication
if ($user->id) { // User already exists in database
if (empty($user->auth)) { // For some reason auth isn't set yet
$DB->set_field('user', 'auth', $auth, array('username'=>$username));
$user->auth = $auth;
}
update_internal_user_password($user, $password); // just in case salt or encoding were changed (magic quotes too one day)
if ($authplugin->is_synchronised_with_external()) { // update user record from external DB
$user = update_user_record($username);
}
} else {
// Create account, we verified above that user creation is allowed.
$user = create_user_record($username, $password, $auth);
}
$authplugin->sync_roles($user);
foreach ($authsenabled as $hau) {
$hauth = get_auth_plugin($hau);
$hauth->user_authenticated_hook($user, $username, $password);
}
if (empty($user->id)) {
return false;
}
if (!empty($user->suspended)) {
// just in case some auth plugin suspended account
add_to_log(SITEID, 'login', 'error', 'index.php', $username);
error_log('[client '.getremoteaddr()."] $CFG->wwwroot Suspended Login: $username ".$_SERVER['HTTP_USER_AGENT']);
return false;
//.........这里部分代码省略.........
示例13: _create_account
/**
* 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;
}
示例14: autoregister_avatar_user
/**
* Auto-register a new user account for the current avatar.
* NOTE: this does NOT respect ANYTHING but the most basic Moodle accounts.
* Use at your own risk!
* @return string|bool The new password (plaintext) if successful, or false if not
* @access public
*/
function autoregister_avatar_user()
{
global $CFG;
// Make sure we have avatar data, and reset the user data
if (empty($this->avatar_data)) {
return false;
}
$this->user_data = null;
// Construct a basic username
$nameparts = explode(' ', $this->avatar_data->avname);
$baseusername = strip_tags(stripslashes(implode('', $nameparts)));
$username = $baseusername;
$conflict_moodle = record_exists('user', 'username', $username);
// If that didn't work, then try a few random variants (just a number added to the end of the name)
$MAX_RANDOM_TRIES = 3;
$rnd_try = 0;
while ($rnd_try < $MAX_RANDOM_TRIES && $conflict_moodle) {
// Pick a random 3 digit number
$rnd_num = mt_rand(100, 998);
if ($rnd_num >= 666) {
$rnd_num++;
}
// Some users may object to this number
// Construct a new username to try
$username = $baseusername . (string) $rnd_num;
// Check for conflicts
$conflict_moodle = record_exists('user', 'username', $username);
// Next attempt
$rnd_try++;
}
// Stop if we haven't found a unique name
if ($conflict_moodle) {
return false;
}
// Looks like we got an OK username
// Generate a random password
$plain_password = sloodle_random_web_password();
// Create the new user
$this->user_data = create_user_record($username, $plain_password);
if (!$this->user_data) {
$this->user_data = null;
return false;
}
// Get the complete user data again, so that we have the password this time
//$this->user_data = get_complete_user_data('id', $this->user_data->id); // this should not be necessary
$this->user_data = get_record('user', 'id', $this->user_data->id);
// this should be sufficient
// Attempt to use the first and last names of the avatar
$this->user_data->firstname = $nameparts[0];
if (isset($nameparts[1])) {
$this->user_data->lastname = $nameparts[1];
} else {
$this->user_data->lastname = $nameparts[0];
}
// Prevent emails from being sent to this user
$this->user_data->emailstop = 1;
// Attempt to update the database (we don't really care if this fails, since everything else will have worked)
update_record('user', $this->user_data);
// Now link the avatar to this account
$this->avatar_data->userid = $this->user_data->id;
update_record('sloodle_users', $this->avatar_data);
return $plain_password;
}
示例15: loginpage_hook
//.........这里部分代码省略.........
}
} else {
throw new moodle_exception('Empty Social UID', 'auth_lenauth');
}
} else {
/**
* addon @since 24.12.2014
* I forgot about clear $_COOKIE, thanks again for Yandex Tech Team guys!!!
*/
@setcookie($authprovider, null, time() - 3600);
throw new moodle_exception('Final request returns nothing', 'auth_lenauth');
}
$last_user_number = intval($this->_oauth_config->auth_lenauth_last_user_number);
$last_user_number = empty($last_user_number) ? 1 : $last_user_number + 1;
//$username = $this->_oauth_config->auth_lenauth_user_prefix . $last_user_number; //@todo
/**
* If user with email from webservice not exists, we will create an account
*/
if (empty($user_lenauth)) {
$username = $this->_oauth_config->auth_lenauth_user_prefix . $last_user_number;
//check for username exists in DB
$user_lenauth_check = $DB->get_record('user', array('username' => $username));
$i_check = 0;
while (!empty($user_lenauth_check)) {
$user_lenauth_check = $user_lenauth_check + 1;
$username = $this->_oauth_config->auth_lenauth_user_prefix . $last_user_number;
$user_lenauth_check = $DB->get_record('user', array('username' => $username));
$i_check++;
if ($i_check > 20) {
throw new moodle_exception('Something wrong with usernames of LenAuth users. Limit of 20 queries is out. Check last mdl_user table of Moodle', 'auth_lenauth');
}
}
// create user HERE
$user_lenauth = create_user_record($username, '', 'lenauth');
/**
* User exists...
*/
} else {
$username = $user_lenauth->username;
}
set_config('auth_lenauth_last_user_number', $last_user_number, 'auth/lenauth');
if (!empty($social_uid)) {
$user_social_uid_custom_field = new stdClass();
$user_social_uid_custom_field->userid = $user_lenauth->id;
$user_social_uid_custom_field->fieldid = $this->_field_id;
$user_social_uid_custom_field->data = $social_uid;
if (!$DB->record_exists('user_info_data', array('userid' => $user_lenauth->id, 'fieldid' => $this->_field_id))) {
$DB->insert_record('user_info_data', $user_social_uid_custom_field);
} else {
$record = $DB->get_record('user_info_data', array('userid' => $user_lenauth->id, 'fieldid' => $this->_field_id));
$user_social_uid_custom_field->id = $record->id;
$DB->update_record('user_info_data', $user_social_uid_custom_field);
}
}
//add_to_log( SITEID, 'auth_lenauth', '', '', $username . '/' . $user_email . '/' . $userid );
// complete Authenticate user
authenticate_user_login($username, null);
// fill $newuser object with response data from webservices
$newuser = new stdClass();
if (!empty($user_email)) {
$newuser->email = $user_email;
}
if (!empty($first_name)) {
$newuser->firstname = $first_name;
}
if (!empty($last_name)) {