Creating user with API Moodle

Creating user with API Moodle

de Владимир Филиппов -
Número de respuestas: 7

Good afternoon! Understanding how to create a user in moodle using api. In particular, through webservice and core_user_create_users.

I found an example on the Internet, slightly modified for myself:

<?php

require_once('../config.php');

require_once($CFG->libdir.'/setup.php');

// Load the MoodleRest class

require_once($CFG->dirroot.'/user/classes/MoodleRest.php');

// Put your webservice url

$webservervice_url = 'https://mysite/webservice/rest/server.php';

// Put a token that have permission to execute core_user_get_users and core_user_create_users

$token = 'mytoken'; //here is my real token in the file

// Instantiate a MoodleRest object

$MoodleRest = new MoodleRest($webservervice_url, $token);

// Query user

$username = 'vvvfilippov';

$query_user['criteria'][0] = array('key' => 'username', 'value' => $username);

$result_query = $MoodleRest->request('core_user_get_users', $query_user);

// Test if an exception was returned when querying the user data

if (!empty($result_query['exception'])) {

     print_r(array('Error querying user', $result_query));

     die();

}

// Check if the user was returned. If not, create him

if (empty($result_query['users'][0]['id'])) {

     $people['users'][0] = array(

         'username' => $username,

         'password' => '123456',

         'firstname' => 'Vladimir',

         'lastname' => "Filippovv",

         'email' => $username . '@email.fake'

     );

     $result_insert = $MoodleRest->request('core_user_create_users', $people, MoodleRest::METHOD_POST);

// Test if an exception was returned when creating the user

     if (!empty($result_insert['exception'])) {

         print_r(array('Error creating user', $result_insert));

         die();

     }

     print_r(array('Created user', $result_insert));

} else {

     print_r(array('Returned user', $result_query));

}


The response on the page is: Array ( [0] => Error querying user [1] => Array ( [exception] => webservice_access_exception [errorcode] => accessexception [message] => Access Control Exception ) )

Can you please tell me what I wrote wrong in the code? I created the token on the page /admin/webservice/tokens.php?ftoken&page=1 Is this correct or should I take it somewhere else?

Promedio de valoraciones: -
En respuesta a Владимир Филиппов

Re: Creating user with API Moodle

de Krishna Mohan Prasad -
Hi Владимир Филиппов,

From the given details I can predict few things, 

1. The access token you are using might be belonging to a user who may not have enough permission to call this webservice, permissions required to call this web-services are moodle/user:viewdetails, moodle/user:viewhiddendetails, moodle/course:useremail, moodle/user:update.

2. Rest webservice is not available to use by this user role, you may need to check rest protocol is permitted to this user's role.


I would request to turn on debugging mode in developer settings, which will provide more information about the error. 

If you can attach your MoodleRest.php, that would help understand how you are internally calling the web-services.


Thanks.

En respuesta a Krishna Mohan Prasad

Re: Creating user with API Moodle

de Владимир Филиппов -
I turned on error output, I was shown a message in the debuginfo section:

[debuginfo] => Access to the function core_user_create_users() is not allowed.
There could be multiple reasons for this:
1. The service linked to the user token does not contain the function.
2. The service is user-restricted and the user is not listed.
3. The service is IP-restricted and the user IP is not listed.
4. The service is time-restricted and the time has expired.
5. The token is time-restricted and the time has expired.
6. The service requires a specific capability which the user does not have.
7. The function is called with username/password (no user token is sent)
and none of the services has the function to allow the user.
These settings can be found in Administration > Site administration
> Server > Web services > External services and Manage tokens.
)
En respuesta a Владимир Филиппов

Re: Creating user with API Moodle

de Владимир Филиппов -
I solved this problem, but another one appeared: [message] => Invalid parameter value detected. [debuginfo] => criteria => Invalid parameter value detected.: Missing required key in single structure: value
En respuesta a Владимир Филиппов

Re: Creating user with API Moodle

de Krishna Mohan Prasad -
Imagen de Plugin developers
From the documentation you cab check the parameters should be sent like this:
REST (POST parameters)

criteria[0][key]= string
criteria[0][value]= string

Try this, it should work
En respuesta a Krishna Mohan Prasad

Re: Creating user with API Moodle

de Владимир Филиппов -
Understood with the help of examples in the file /user/tests/externallib_test.php

User data is obtained through:
$searchparams = array(
array('key' => 'email', 'value' => 'myemail@mypochta.ru')
);
$result = core_user_external::get_users($searchparams);

Create:
$user = array(
'username' => loginmy,
'password' => 'pswd123',
'firstname' => 'Vvladimir',
'lastname' => 'Filippovv',
'middlename' => 'Vladimirovich',
'email' => 'loginmy@email.fake'
);
$result_insert = core_user_external::create_users(array($user));