Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Find out User ID for a User based on the Moodle API #1

Open
phish108 opened this issue Feb 22, 2021 · 4 comments
Open

Find out User ID for a User based on the Moodle API #1

phish108 opened this issue Feb 22, 2021 · 4 comments

Comments

@phish108
Copy link
Member

phish108 commented Feb 22, 2021

Many API Calls depend on the user id of the active user. The user id is not easy to find in Moodle. Is there an API call to get the user ID for the active user?

@phish108
Copy link
Member Author

phish108 commented Feb 22, 2021

If one possesses the USERTOKEN, a known vector for the token owner should be also known. The best thing to use is the email address. The email address is normally a standard attribute with moodle systems. In rare cases it is possible no email is stored for the user. In these cases the user should use the users full name, which is shown in Moodle's User Menu. This may yield multiple matches for common names.

The user information is obtained by searching the user using the function core_user_get_users_by_field.

The API Call for getting the user id for a known vector such as the user's email is:

/webservice/rest/server.php?wstoken=<USERTOKEN>&wsfunction=core_user_get_users_by_field&moodlewsrestformat=json&field=email&values[0]=user@example.com

Call structure

Base URL: /webservice/rest/server.php?moodlewsrestformat=json&wstoken=${ USERTOKEN }

Attributes:

  • wsfunction MUST BE core_user_get_users_by_field
  • field the profile field to look up (ideally email)
  • values[0] the exact lookup value. Without wildcards.

Result structure

The result is an array of user records or an empty array if no matches are found.

The search is successful if the any user is found. It MAY yield records for other users that match the lookup value.

The following attributes are only present if the USERTOKEN matches the selected user:

  • preferences
  • auth
  • theme
  • mailformat
  • lang
  • confirmed
  • username

@phish108
Copy link
Member Author

phish108 commented Jul 6, 2021

Achtung: bei der Verwendung mit curl muss die URL in Einfache Anführungszeichen gesetzt werden!

@phish108
Copy link
Member Author

phish108 commented Jul 6, 2021

Ab moodle 4 ist es nicht mehr möglich alle Nutzer zu finden. So kann über die API für einen regulären Nutzer nicht mehr herausgefunden werden, ob ein anderer Nutzer existiert, solange es keine Peerverbindung (z.B. über einen Kurs) gibt.

@phish108
Copy link
Member Author

phish108 commented Jul 6, 2021

A much smarter variant for bootstrapping is core_webservice_get_site_info. It is parameterless and also reports the available functions for the user. This function only leaves out the user's email address, but this is OK, because Moodle uses the email only internally.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant