Getting a user id through REST API: vicious circle, or how to get this user id just from his email?

Context

  • Plone 5.2 with email login enabled
  • Python3

Problem

(Note that the current question is close to [RESOLVED] Updating a group by adding a user through REST API: doesn't work with his email as user_id even if email login is enabled

Reading a User through Plone RESTful API says:

To retrieve all details for a particular user, send a GET request to the /@users endpoint and append the user id to the URL:

requests.get('http://nohost/plone/@users/noam',
headers={ 'Accept': 'application/json', }, auth=('admin', 'secret'))

What the server will respond is:

HTTP/1.1 200 OK
Content-Type: application/json

{
  "@id": "http://localhost:55001/plone/@users/noam", 
  "description": "Professor of Linguistics", 
  "email": "noam.chomsky@example.com", 
  "fullname": "Noam Avram Chomsky", 
  "home_page": "web.mit.edu/chomsky", 
  "id": "noam", 
  "location": "Cambridge, MA", 
  "portrait": null, 
  "roles": [
    "Member"
  ], 
  "username": "noam"
}

(Edit: Sorry, I wrongly identified username and user id id. Corrected below.)

and, among others, we see the username the user id "noam" in the id field. But it is a kind of a vicious circle, since this user id was needed (we had to append it to the URL) to get this user id.

This remark comes from what I'd like to do: to get through REST API the username user id id of a user, just knowing his email. Do you know how to proceed?

1 Like

https://plonerestapi.readthedocs.io/en/latest/users.html#list-users supports "some basic filtering" Try and see if you can search using the email address

I already seen this feature but AFAICS this filtering only applies to usernames:

The server will respond with a list the filtered users in the portal with username starts with the query.

Edit: I confirm that filtering on (part of) the email address gives an empty list if it is not contained in any of the usernames.

and https://plonerestapi.readthedocs.io/en/latest/principals.html#search-principals ?

1 Like

Good catch, thanks!

IMHO, still quite strange that "To retrieve all details for a particular user", one needs to "send a GET request to the /@users endpoint and append the user id to the URL" : we are bitten if we precisely are interested in this user id detail. It would be nice if we could retrieve all details for a particular user from the other fields, at least the email one which uniquely identifies a user and is the key one in case of email login enabled.

@espenmn, this tip could be useful.

Plone Foundation Code of Conduct