英文:
How to create user in authentication plugin?
问题
我想为Moodle 3.10编写一个自定义身份验证插件
当用户尝试使用登录表单登录时,该插件将使用外部API检查凭据。只有当凭据匹配时,用户才能登录。如果帐户不存在,将创建该帐户。
我目前的进展如下:
class auth_plugin_externalapi extends auth_plugin_base {
user_login($username, $password){
global $DB;
$user = login_via_api($username, $password);
if(empty($user)){
return false;
}
if(!$DB->get_record('user', array('username' => $api_data->username))){
user_create_user($user);
}
return true;
}
}
这种方法失败,因为当通过lib/moodlelib.php
中的authenticate_user_login
方法调用user_login
时,用户会被创建两次。
在Moodle中,通过身份验证插件创建用户是否有特定的方法?还是我需要自定义lib/moodlelib.php
中的代码?
英文:
I want to write a customized authentication plugin for Moodle 3.10
When a user tries to login with loginform, the plugin will check credentials with an external API. Only if matches the user will be logged in. If account does not exists, it will be created.
This is how far I came:
class auth_plugin_externalapi extends auth_plugin_base {
user_login($username, $password){
global $DB;
$user = login_via_api($username, $password);
if(empty($user)){
return false;
}
if(!$DB->get_record('user', array('username' => $api_data->username)){
user_create_user($user);
}
return true;
}
}
This approach fails, because when user_login
is called via lib/moodlelib.php
in method authenticate_user_login
, then the user is created twice.
Is there a specific way to create user through authentication plugins in moodle? Or do I need to customize the code in lib/moodlelib.php
?
答案1
得分: 1
Moodle身份验证插件不应直接创建用户帐户,它们只应确定用户是否已成功进行身份验证以及用户的详细信息。
然后,Moodle核心代码会处理更新现有用户帐户的过程,使用提供的详细信息,或者使用这些详细信息创建一个新帐户(只要未设置'authpreventaccountcreation')。无论哪种方式,核心代码都会启动与识别的用户帐户的登录会话。
从您目前的位置继续的最简单方式可能是这样的:
class auth_plugin_externalapi extends auth_plugin_base {
private static $userinfo;
public function user_login($username, $password){
self::$userinfo = login_via_api($username, $password);
return (bool)self::$userinfo;
}
public function get_userinfo($username) {
if (!self::$userinfo) {
return false;
}
if (self::$userinfo->username !== $username) {
return false;
}
return (array)self::$userinfo;
}
}
希望这对您有所帮助。
英文:
Moodle authentication plugins should not be directly creating user accounts, they should just determine whether or not the user has authenticated successfully and the details of the user.
The Moodle core code then handles the process of either updating the existing user account, with the details provided, or creating a new account with those details (as long as 'authpreventaccountcreation' is not set). Either way, the core code then starts a login session with the identified user account.
The easiest way to proceed from where you are, would be something like this:
class auth_plugin_externalapi extends auth_plugin_base {
private static $userinfo;
public function user_login($username, $password){
self::$userinfo = login_via_api($username, $password);
return (bool)self::$userinfo;
}
public function get_userinfo($username) {
if (!self::$userinfo) {
return false;
}
if (self::$userinfo->username !== $username) {
return false;
}
return (array)self::$userinfo;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论