我正在尝试使用身份验证插件在 moodle 中手动对用户进行身份验证,但我不了解整个过程工作所需的一切。因此,关于我在这里缺少的一些建议将非常有帮助!
我创建了插件,启用了它,它起作用了,但仅适用于某些情况,这就是我的问题所在。我的猜测是,在某些时候,我需要实际调用一个将用户信息保存在 moodle 数据库中的函数。但同样,我不确定它在哪里,或者它是如何工作的。所以。。。专家,在这里帮我一把。
这是我在身份验证插件上更改的两个功能。(身份验证.php)
function loginpage_hook() {
global $CFG, $DB, $user, $frm, $errormsg;
$IsAuthenticated = false;
if(isset($_COOKIE["AUTHENTICATION_KEY"])){
$json = file_get_contents("WWW.WEBSERVICEURL.COM",true);
//getting the file content
$decode = json_decode($json, true);
//getting the file content as array
if($decode["AuthFlag"]){
$ucUser = $decode["Username"];
$user = $DB->get_record('user', array('username'=>$ucUser, 'mnethostid'=>$CFG->mnet_localhost_id));
$frm->username = $ucUser;
$IsAuthenticated = true;
}
}
if(!$IsAuthenticated && empty($frm->username)){
$errormsg = ".";
}
}
/**
* Returns true if the username and password work or don't exist and false
* if the user exists and the password is wrong.
*
* @param string $username The username
* @param string $password The password
* @return bool Authentication success or failure.
*/
function user_login ($username, $password) {
global $CFG, $DB, $user;
if(!$user){ return false; }
if(isset($_COOKIE["AUTHENTICATION_KEY"])){
$json = file_get_contents("WWW.WEBSERVICEURL.COM", true);
//getting the file content
$decode = json_decode($json, true);
//getting the file content as array
if($decode["AuthFlag"]){
$ucUser = $decode["Username"];
if($user->username = $ucUser){ return true; }
}
}
return false;
}
我已经做了一个身份验证插件。我认为您缺少的代码的关键片段将是这样的:
$user = authenticate_user_login($username, null);
$newuser = new stdClass();
$newuser->id = $user->id;
$newuser->email = $mail;
$newuser->username = $mail;
$newuser->country = 'AR';
$newuser->city = 'capital federal';
$newuser->firstname = $nombre;
$newuser->lastname = $apellido;
$DB->update_record('user', $newuser);
$user = authenticate_user_login($mail, null);
complete_user_login($user);
那里的关键功能是authenticate_user_login($username, null)
。您可以传递任何用户名,如果用户名不存在,它将创建一个空用户,准备填写。
我的插件工作得很好。因此,如果您需要某些东西或想要共享您的代码,以便我了解问题所在,欢迎您!