1

我正在尝试使用身份验证插件手动对 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;
}
4

1 回答 1

2

我最近制作了一个身份验证插件。我认为您缺少的关键代码片段将是这样的:

$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)。你可以传递任何用户名,如果用户名不存在,它会创建一个空用户,准备被填充。

我的插件工作得很好。因此,如果您需要某些东西或者您想分享您的代码以便我了解问题所在,欢迎您!

于 2012-06-27T15:08:11.943 回答