通过网络服务在 Moodle 中创建用户 php
create user in moodle through web services php
我正在尝试使用 PHP 通过 Moodle 中的 Web 服务创建用户,并且我正在使用 ws 函数:core_user_create_users
。但它给了我一个错误。
{"exception":"webservice_access_exception","errorcode":"accessexception","message":"Access control exception"}
我正在使用 moodle ver3.4。
如果web服务功能有问题,能不能告诉我如何给当前用户的token增加能力?如果还有其他问题请告诉我。
谢谢,
我会就如何做到这一点给出一个完整的答案。首先,我们将从为 Web 服务创建一个角色开始(这不是必需的,但我喜欢这样做以保持管理员实例的清洁)。
但首先,有一个快速解决您的问题的方法。将授权用户添加到您网站的管理员列表中,您可以使用生成的令牌自由调用任何功能。
一、为网络服务创建新角色
您可以转到 Site administration > Users > Permissions > Define roles
来做到这一点。您会发现一个用于添加新角色的按钮 Add a new role
。单击该按钮后,您将获得一个表单,您可以在其中使用定义的(或默认的)角色或原型来定义您的新角色。您还可以使用从另一个 Moodle 实例导出的文件来创建您的角色。无论如何,我们将选择 Continue
来定义我们的自定义角色。现在,我们可以填写必要的信息,如(简称、自定义全名、自定义描述)。在上下文部分,选择 System
从系统的任何地方分配这个角色。
现在一直向下滚动到功能。在 Filter
搜索框中搜索您的能力(在我们的例子中是创建用户)。当您创建 Web 服务并向其添加功能时,您可以看到所需的功能。每个功能都需要具有特定能力的用户才能执行。
您可以通过转到 Site administration > Plugins > Web services > External services
并创建一个新的外部服务并添加您的功能,从管理仪表板中找到功能功能,您将在您的服务 Required capabilities
列中看到功能列表 table.只需在函数下拉列表中键入 core_user_create_users
。单击 Add functions
按钮,现在您可以看到 Required capabilities
。在这种情况下,它只是 moodle/user:create
。因此,我们可以通过选中我们想要的每个功能旁边的 allow
复选框来在我们的角色中使用它。
完成角色配置后,单击 Create this role
。
二.将用户分配给新角色
现在我们可以将我们的用户分配给新角色。
- 转到
Site administrator > Users > Permissions > Assign system roles
。
- 选择我们的新角色。
- 将用户添加到现有用户列表。
三。将用户添加到授权用户
仅当您在创建外部服务时选中 Only authorized users
选项时才需要这样做。
- 转到
Site administrator > Plugins > Web services > External services
。
- 在您的 Web 服务上单击
Authorized users
link。
- 将用户添加到
Authorized users
的列表中。
检查表单下的警告。如果您的用户不具备某些能力,Moodle 会在表单下发出警告信号。您可以通过将缺失的功能添加到我们之前创建的角色来解决这个问题。
四.创建令牌
现在我们可以转到令牌管理器并为我们的用户为我们的外部 Web 服务生成一个新令牌。
BS: 不要忘记先启用网络服务。启用 REST 协议或您愿意使用的任何协议。
更新:如何调用函数?
根据此回答的评论中要求的详细信息,我将提供一个示例来说明如何调用这些 Web 服务函数(在此示例中 create_users
)。
在此示例中,我们将使用 Moodle 核心团队提供的 cURL class 来简化我们的操作。您可以在他们的 Web 服务客户端示例 here 的存储库中找到这个 class。我将使用此存储库中 PHP-REST
文件夹中的 curl.php
文件。此存储库中的所有 PHP 文件夹都有相同的 curl.php
文件。 client.php
文件是唯一的区别。
$token = 'replace it with your web service token';
$domainname = 'http://your_moodle_domain.ltd';
$functionname = 'core_user_create_users';
$restformat = 'json';
$user = array(
"username" => "username of your new user", // must be unique.
"password" => "password must respect moodle policies",
"firstname" => "first name",
"lastname" => "last name",
"email" => "your_user@email.com",
"auth" => 'authentication method can be email, manual, registred ..',
"customfields" => array ( // If you have custom fields in your system.
array(
"type" => "birthdate",
"value" => strtotime("01/01/1990")
),
array(
"type" => "something_else",
"value" => "0"
)
)
);
$users = array($user); // must be wrapped in an array because it's plural.
$param = array("users" => $users); // the paramater to send
$serverurl = $domainname . '/webservice/rest/server.php'. '?wstoken=' .
$token . '&wsfunction='.$functionname;
require_once('curl.php'); // You can put it in the top.
$curl = new curl;
$restformat = ($restformat == 'json')?'&moodlewsrestformat=' .
$restformat:'';
$resp = $curl->post($serverurl . $restformat, $param);
我正在尝试使用 PHP 通过 Moodle 中的 Web 服务创建用户,并且我正在使用 ws 函数:core_user_create_users
。但它给了我一个错误。
{"exception":"webservice_access_exception","errorcode":"accessexception","message":"Access control exception"}
我正在使用 moodle ver3.4。
如果web服务功能有问题,能不能告诉我如何给当前用户的token增加能力?如果还有其他问题请告诉我。
谢谢,
我会就如何做到这一点给出一个完整的答案。首先,我们将从为 Web 服务创建一个角色开始(这不是必需的,但我喜欢这样做以保持管理员实例的清洁)。 但首先,有一个快速解决您的问题的方法。将授权用户添加到您网站的管理员列表中,您可以使用生成的令牌自由调用任何功能。
一、为网络服务创建新角色
您可以转到 Site administration > Users > Permissions > Define roles
来做到这一点。您会发现一个用于添加新角色的按钮 Add a new role
。单击该按钮后,您将获得一个表单,您可以在其中使用定义的(或默认的)角色或原型来定义您的新角色。您还可以使用从另一个 Moodle 实例导出的文件来创建您的角色。无论如何,我们将选择 Continue
来定义我们的自定义角色。现在,我们可以填写必要的信息,如(简称、自定义全名、自定义描述)。在上下文部分,选择 System
从系统的任何地方分配这个角色。
现在一直向下滚动到功能。在 Filter
搜索框中搜索您的能力(在我们的例子中是创建用户)。当您创建 Web 服务并向其添加功能时,您可以看到所需的功能。每个功能都需要具有特定能力的用户才能执行。
您可以通过转到 Site administration > Plugins > Web services > External services
并创建一个新的外部服务并添加您的功能,从管理仪表板中找到功能功能,您将在您的服务 Required capabilities
列中看到功能列表 table.只需在函数下拉列表中键入 core_user_create_users
。单击 Add functions
按钮,现在您可以看到 Required capabilities
。在这种情况下,它只是 moodle/user:create
。因此,我们可以通过选中我们想要的每个功能旁边的 allow
复选框来在我们的角色中使用它。
完成角色配置后,单击 Create this role
。
二.将用户分配给新角色
现在我们可以将我们的用户分配给新角色。
- 转到
Site administrator > Users > Permissions > Assign system roles
。 - 选择我们的新角色。
- 将用户添加到现有用户列表。
三。将用户添加到授权用户
仅当您在创建外部服务时选中 Only authorized users
选项时才需要这样做。
- 转到
Site administrator > Plugins > Web services > External services
。 - 在您的 Web 服务上单击
Authorized users
link。 - 将用户添加到
Authorized users
的列表中。
检查表单下的警告。如果您的用户不具备某些能力,Moodle 会在表单下发出警告信号。您可以通过将缺失的功能添加到我们之前创建的角色来解决这个问题。
四.创建令牌
现在我们可以转到令牌管理器并为我们的用户为我们的外部 Web 服务生成一个新令牌。
BS: 不要忘记先启用网络服务。启用 REST 协议或您愿意使用的任何协议。
更新:如何调用函数?
根据此回答的评论中要求的详细信息,我将提供一个示例来说明如何调用这些 Web 服务函数(在此示例中 create_users
)。
在此示例中,我们将使用 Moodle 核心团队提供的 cURL class 来简化我们的操作。您可以在他们的 Web 服务客户端示例 here 的存储库中找到这个 class。我将使用此存储库中 PHP-REST
文件夹中的 curl.php
文件。此存储库中的所有 PHP 文件夹都有相同的 curl.php
文件。 client.php
文件是唯一的区别。
$token = 'replace it with your web service token';
$domainname = 'http://your_moodle_domain.ltd';
$functionname = 'core_user_create_users';
$restformat = 'json';
$user = array(
"username" => "username of your new user", // must be unique.
"password" => "password must respect moodle policies",
"firstname" => "first name",
"lastname" => "last name",
"email" => "your_user@email.com",
"auth" => 'authentication method can be email, manual, registred ..',
"customfields" => array ( // If you have custom fields in your system.
array(
"type" => "birthdate",
"value" => strtotime("01/01/1990")
),
array(
"type" => "something_else",
"value" => "0"
)
)
);
$users = array($user); // must be wrapped in an array because it's plural.
$param = array("users" => $users); // the paramater to send
$serverurl = $domainname . '/webservice/rest/server.php'. '?wstoken=' .
$token . '&wsfunction='.$functionname;
require_once('curl.php'); // You can put it in the top.
$curl = new curl;
$restformat = ($restformat == 'json')?'&moodlewsrestformat=' .
$restformat:'';
$resp = $curl->post($serverurl . $restformat, $param);