General developer forum

Autocreate webservice token upon plugin installaion

 
Picture of Darko Miletić
Re: Autocreate webservice token upon plugin installaion
Core developersParticularly helpful Moodlers

Yes. You should place your code in install.php. Take a look into structure of moodle plugin

https://docs.moodle.org/dev/Plugin_files#db.2Finstall.php


 
Average of ratings: Useful (1)
Picture of v k
Re: Autocreate webservice token upon plugin installaion
 

Thank you.

Is there a working example I can look at?

1. Create webservice with necessary functions
Do I use functions in $CFG->dirroot . '/webservice/lib.php'  for that?
Like creating new class $webservice = new webservice();
and then using $webservice->add_external_service(...); or some other way is expected?

2. Create a special user with relevant capabities
I stuck here. Do I use 'user/lib.php' functions? 
user_create_user(...); - this one or other way is expected?
And which function do I use to give a new user required capabilites..


Example would be great...

Thanks all!


 
Average of ratings: -
Picture of Darko Miletić
Re: Autocreate webservice token upon plugin installaion
Core developersParticularly helpful Moodlers

Read this document

https://docs.moodle.org/dev/Web_services_API

A web service is defined by adding services.php to your plugin (as outlined in the link above)

Example of that can be found here

https://github.com/moodle/moodle/blob/master/lib/db/services.php

Use this for creating user

$userdata = [
    'auth' => 'manual',
    'confirmed' => true,
    'username' => 'foo',
    'password' => 'foo',
    'firstname' => 'foo',
    'lastname' => 'faa',
    'email' => 'some@email.com'
];
$userid = user_create_user($userdata);


For assigning capabilities use API assign_capability.

https://github.com/moodle/moodle/blob/175b3708c92f3f19529ccab62dcc196c46ba6423/lib/accesslib.php#L1292



 
Average of ratings: -