Can Moodle users be Web Service users?

Can Moodle users be Web Service users?

by Jackie Smile -
Number of replies: 13

Hi,

as far as I understand from the External services security page: A Moodle user cannot be used to access certain external functions of a web service?

Could you please confirm that?

I would like to retrieve courses from Moodle through the web service. Is there a way to pull just the data the requesting user is allowed to view within Moodle as well?

Thank you

Jackie

Average of ratings: -
In reply to Jackie Smile

Re: Can Moodle users be Web Service users?

by Jérôme Mouneyrac -
Hi Jackie,
a user can access all the functions that have been added to a service: http://docs.moodle.org/22/en/How_to_create_and_enable_a_web_service#Adding_functions_to_the_service

Of course the user needs to be able to access the service itself.

I confirm that there is a function to get courses by web services. Have a look in Admin > Plugins > Web Services > API Documentation.

Moreover all implemented web service functions should match 100% of the user permissions in Moodle.

Cheers,
Jerome
In reply to Jérôme Mouneyrac

Re: Can Moodle users be Web Service users?

by Jackie Smile -

Thanks Jerome.

I think I was a bit confused when I wrote the other post because I forgot to state the way of authentication.

It is clear to me that if I use a token and specify the user for the webservice, that he is allowed to access the service as far as his permission allows.

What I would like to achieve, is a basic authentication so that each user can just retrieve his own courses for example, meaning that a teacher cannot look inside the courses of other teachers. As far as I understood about this basic authentication, I would need to create new users for use with the webservice. Is that correct? If yes, is there a way to have just one user account for use with moodle and the werbservice?

Thank you.

In reply to Jackie Smile

Re: Can Moodle users be Web Service users?

by Jérôme Mouneyrac -
Hi Jackie,
it seems to me you should use the token authentication where the token is the user (with all and only his/her capabilities).

1) sending the username/password, retrieve the user token from your app: http://docs.moodle.org/dev/Creating_a_web_service_client#How_to_get_a_user_token
2) call the web service function (for example core_course_get_courses) with this token. Web service functions match what the user can see/change in the web interface, and not more.

It's how My Moodle app works. You don't need to create any new users.


Average of ratings: Useful (1)
In reply to Jérôme Mouneyrac

Re: Can Moodle users be Web Service users?

by Jackie Smile -

Thank you very much. That helped me in so far that I can retrieve the token for my admin account. I see that this would be the right way to do but I can't work out the right way to set everything up for the teacher role.

I still find the whole users and role setting quite confusing. I added the permission of creating a token to the teacher role. However the teacher of a course still cannot create a token because he is missing this capability. Has it something to do with the context of the role? Could you please tell me what would be the right thing to do?

Regards

In reply to Jackie Smile

Re: Can Moodle users be Web Service users?

by Jérôme Mouneyrac -
Hi Jackie,
you need to give two capabilities to your teacher. One to create token, one to use the web service protocol (xmlrpc, rest...). In the administration you can edit the teacher system role, there you should find the capabilities to assign to this role.

Alternatively you can enable mobile web service in Admin > Plugins > Manage services. It should set the right caps to every users to use My Mobile app (and most likely other apps using xmlrpc protocol)

Cheers,
Jerome
In reply to Jérôme Mouneyrac

Re: Can Moodle users be Web Service users?

by Jackie Smile -

Ok, I tried both, but either I'm doing it wrong or something else doesn't work.

I added the two capabilities to the teacher role but then I still couldn't create a token with a teacher. I then changed the context of the teacher role to system which of course worked. I added the token for the teacher to my webservice client.php. However the teacher is then able to see every course. I removed the capabilities and the context setting and tried with your second suggestion but then I always get the error that I "cannot view the profile of this user"Also I don't have the path you mentioned above, just Site administration > Plugins > Web services > External services, where I could enable the mobile webservice.

I'm really lost with those user rights, roles and context even though it makes sense to me. I just can't incorporate what I need sad

Regards

jackie

In reply to Jackie Smile

Re: Can Moodle users be Web Service users?

by Jérôme Mouneyrac -
Hi Jackie,
your main issue is related to understanding roles and capabilities first. You need to read http://docs.moodle.org/22/en/Roles_and_permissions
Roles/Capabilities are the nerves of Moodle. They let you customize Moodle to be able to do about whatever you want. Because of this freedom, it is not quick to learn.

Cheers,
Jerome

PS: to get users, call get_enrolled_users/get_users_by_course function to retrieve participants. In Moodle you can view user profile in system context (mostly admin) and user profile in course context (often the teachers and participants).
In reply to Jérôme Mouneyrac

Re: Can Moodle users be Web Service users?

by Jackie Smile -

Hi Jerome,

I already read that. I also managed to locate my error. It was in the function that was checking the context.  I previously copied some code from the startattempt.php file in the quiz module. In there the context of the user was checked which in this case needed to be the course context. Now everything works as it should.

Thank you for your help and sorry for causing some confusion.

Best regards.
jackie

In reply to Jackie Smile

Re: Can Moodle users be Web Service users?

by Jérôme Mouneyrac -
No problem Jackie, happy that it works for you now smile
In reply to Jérôme Mouneyrac

Re: Can Moodle users be Web Service users?

by asha chaparala -
Hi sir,
i am working of moodle webservises.
i have one doubt,how can i know the user role for that site when he logged in.
for example:for admin(i need response as admin)
                            teacher(i need response as teacher)
                            student(i need response as student)
                            manager(i need response as manager)
i need this through webservises api call,is it possible to do like this.
please help me to fix this issue.



waiting for reply.............................
In reply to asha chaparala

Re: Can Moodle users be Web Service users?

by Malte Reißig -

hey asha, 

while "waiting for a reply" I can recommend you to go through http://docs.moodle.org/27/en/How_to_create_and_enable_a_web_service again.

As well have a look at the dialog on this page here, where jerome said:

Hi Jackie,

> it seems to me you should use the token authentication where the token is the user (with all and only his/her capabilities).

Here is what I think about that: To enable this kind of "token authentication" you need to set your "External service"-Configuration to "Required capability set". Then your moodle webservice knows and checks the capabilities and roles of each requesting user through the wstoken you send.

For more infos on working with roles and capabilities in moodle please check the link in one of jeromes answers above.

Hope this helps + Cheers!
In reply to Malte Reißig

Re: Can Moodle users be Web Service users?

by asha chaparala -

Hi Malte Reißig,

Thankyou very much for your reply.

I am able to create token for user,i completed this step and i created webservise and i added all the webservises functions i need,but here my problem is i want to hide some sections in admin login and in teacher login i want to show only some sections

Through webservises by using user token is any way to get user role for that site.

please help me to fix this issue.

In reply to Jackie Smile

Re: Can Moodle users be Web Service users?

by Pratik Sathaye -

I am also facing same issue like for role "Student" I have set "Create User" permission "Prohibit" but still using token I am able to create user from student credentials throught REST API.

And we can only select one capability for single external service. 

so token we get at login/token.php for service1, can we use it to call service2 ?

Can you help me with this issue ?