Reputation: 43
I am trying to create a user through web services in Moodle by using PHP and I am using ws function: core_user_create_users
. but it gives me an error.
{"exception":"webservice_access_exception","errorcode":"accessexception","message":"Access control exception"}
i am using moodle ver3.4.
Can you tell me how to add capabilities to the current token with respect to the user if its problem of web service function? if its other problem please tell me.
Thanks,
Upvotes: 2
Views: 10264
Reputation: 390
I will give a full answer on how to do this. First, we will start by creating a role for web services (it's not necessary, but I love to do it this way to keep the instance clean for administrators). But first, there is a quick solution to your problem. Add the authorized user to the list of administrators of your website and you are free to call any function with the generated token.
You can do that by going to Site administration > Users > Permissions > Define roles
. You will find a button to add a new role Add a new role
. After you click on that button, you will get a form where you can use defined (or default) roles or archetype to define your new role. You can also create your role using an exported file from another Moodle instance. Anyway, we will choose Continue
to define our custom role. Now, we can fill the necessary information like (short name, custom full name, custom description).In the context part, choose System
to assign this role from everywhere in the system.
Now scroll down all the way to the capabilities. Search in the Filter
search box your capabilities (in our case creating users). You can see the needed capabilities when you create the web service and add the function to it. Each function needs a user with specific capabilities to execute.
You can find function capabilities either from admin dashboard by going to Site administration > Plugins > Web services > External services
and create a new external service and add your function and you will see the list of capabilities int he Required capabilities
column of your service table. Just type core_user_create_users
in the functions drop-down list. Click on Add functions
button and now you can see the Required capabilities
. In this case, it's only moodle/user:create
. So we can use this in our role by checking allow
checkbox next to each capability we want.
After you finish your role configuration, click on Create this role
.
Now we can assign our user to the new role.
Site administrator > Users > Permissions > Assign system roles
.This is only required if you checked the Only authorized users
option when you created your external service.
Site administrator > Plugins > Web services > External services
.Authorized users
link on your web service.Authorized users
.Check the warning under the form. If your user doesn't have some capabilities, Moodle will signal that as a warning under the form. You can fix that by adding the missing capabilities to the role that we created earlier.
Now we can go to the token manager and generate a new token to our user for our external web service.
BS: Don't forget to enable web services first. Enable REST protocol or whatever protocol you are willing to use.
Following the details requested in the comments of this answer, I will provide an example on how to call these web services functions (in this example create_users
).
In this example, we will use cURL class provided by the core team of Moodle to make things easier for us. You can find this class in the repository of their web service client example here. I will be using the curl.php
file in the PHP-REST
folder in this repository. All the PHP folder in this repositories have the same curl.php
file. The client.php
file is the only difference.
$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" => "[email protected]",
"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);
Upvotes: 8