-
-
Save timhunt/51987ad386faca61fe013904c242e9b4 to your computer and use it in GitHub Desktop.
| <?php | |
| require_once(__DIR__ . '/config.php'); | |
| require_once($CFG->dirroot . '/webservice/lib.php'); | |
| $systemcontext = context_system::instance(); | |
| // Enable web services and REST protocol. | |
| set_config('enablewebservices', true); | |
| set_config('webserviceprotocols', 'rest'); | |
| // Create a web service user. | |
| $webserviceuser = $this->datagenerator->create_user([ | |
| 'username' => 'ws-externalquiz-user', 'firstname' => 'Externalquiz grades', | |
| 'lastname' => 'User']); | |
| // Create a web service role. | |
| $wsroleid = create_role('WS Role for Externalquiz grades', 'ws-externalquiz-role', ''); | |
| set_role_contextlevels($wsroleid, [CONTEXT_SYSTEM]); | |
| assign_capability('webservice/rest:use', CAP_ALLOW, $wsroleid, $systemcontext->id, true); | |
| assign_capability('mod/externalquiz:receivegrades', CAP_ALLOW, $wsroleid, $systemcontext->id, true); | |
| // Give the user the role. | |
| role_assign($wsroleid, $webserviceuser->id, $systemcontext->id); | |
| // Enable the externalquiz webservice. | |
| $webservicemanager = new webservice(); | |
| $service = $webservicemanager->get_external_service_by_shortname('mod_externalquiz_receive_grades'); | |
| $service->enabled = true; | |
| $webservicemanager->update_external_service($service); | |
| // Authorise the user to use the service. | |
| $webservicemanager->add_ws_authorised_user((object) ['externalserviceid' => $service->id, | |
| 'userid' => $webserviceuser->id]); | |
| // Create a token for the user. | |
| $token = external_generate_token(EXTERNAL_TOKEN_PERMANENT, $service, $webserviceuser->id, $systemcontext); |
Yes, that is better code.
Hi, sorry, any have the endpoints for use the API of Moodle ?
Hi, sorry, any have the endpoints for use the API of Moodle ?
Do you have any answer?
Hi, sorry, any have the endpoints for use the API of Moodle ?
Do you have any answer?
Is this what you are looking for?
https://docs.moodle.org/dev/Web_service_API_functions
According to documentation the webservice user should use the "webservice" auth method:
https://docs.moodle.org/500/en/Web_services_authentication
So the create_user function needs the additional parameter 'auth' => 'webservice'!
The purpose is to avoid setting 'policyagreed' => 1 for the webservice user:
https://github.com/moodle/moodle/blob/MOODLE_500_STABLE/lib/moodlelib.php#L2465
Fun fact: the "webservice" auth method can be used without activating it.
Very nice!
I know this is just to demonstrate, but just for interest sake, if there are already other protocols enabled,
will disable them. That is why I do: