我正在尝试使用身份验证插件手动验证moodle中的用户,但我不了解整个过程工作所需的一切。所以,关于我在这里缺少的一些建议会非常有帮助!
我创建了插件,启用了它,并且它有效,但仅适用于某些情况,这就是我的问题所在。我的猜测是,在某些时候我需要实际调用一个函数来保存moodle数据库中的用户信息。但同样,我不确定它在哪里,或者它是如何工作的。所以...专家,请帮我一把。
以下是我在身份验证插件中更改的两个功能。 (auth.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;
}
答案 0 :(得分:2)
我已经做了一个auth插件。我认为你缺少的关键代码片段是这样的:
$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)
。您可以传递任何用户名,如果用户名不存在,则会创建一个空用户,准备填写。
我的插件运行得很好。因此,如果您需要某些内容或想要共享代码,以便我能够了解问题所在,欢迎您!