Accessing Moodle Username

Accessing Moodle Username

Marc Bibaud -
回帖数:3

Given that I currently only have 'Moodle Administrator' access to my schools system, is there a way I can pass the 'current username' to an external php script.

I've tested on my own local system and from within Moodle, I can refer to $USER->username  Is there a way that that can be securely passed to a call of an external script?

If the above is not possible, please could someone advise how one might deal with the following requirement (such that I can ask the 'Site FTP Administrator' to do it on our behalf:

I need to sign on to an external system using the Moodle logon in place of their own as they are satisfied that Moodle has authenticated the user.  I can call my script: http://........php?username=nnnnnn where 'nnnnn' is the Moodle username but at the moment I'm not even sure how to do that and additionally this would be very insecure as it could just be pasted into any browser.  I fgured that if instead of reading a query string, my script read a session variable (of the username) only available to an authenticated Moodle user then it would be reasonably secure.

 Any pointers in the right direction very much appreciated.

Thanks

AM

回复Marc Bibaud

Re: Accessing Moodle Username

Hubert Chathi -
Here are a few possibilities:
  • If your script is on the same domain as the Moodle site, it can read Moodle's session cookie, and then access the session data.
  • You could make your external PHP script use Moodle Networks (but it may be more effort than it's worth)
  • You could do something similar to the way Moodle Networks does single sign-on: when a user wants to access the external script, have Moodle create a token (a random number) and store it in the database, and then call your external script using http://....php?token=[random number]. Your script will then need to ask Moodle who the user is corresponding to that token (or if your script can access the database directly, it can do that as well)
  • Make Moodle into an OpenID identity provider, and make your external PHP use OpenID for authentication.
The first option is probably the easiest, if it is possible. Otherwise, the third option is probably the next easiest, but still a bit of work. The second and fourth options are probably the most work.
回复Hubert Chathi

Re: Accessing Moodle Username

Dallas Ray Smetter -
Hubert,

I would like to try your first suggestion...

How would I do it?

"read Moodle's session cookie, and then access the session data."