[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

Context

  • Plone 5.2 with email login enabled
  • Python3

Problem

Suppose we create a user through REST API:

curl -i -X POST http://nohost/plone/@users -H 'Accept: application/json' -H 'Content-Type: application/json' --data-raw '{"description": "Professor of Linguistics", "email": "noam.chomsky@example.com", "fullname": "Noam Avram Chomsky", "home_page": "web.mit.edu/chomsky", "location": "Cambridge, MA", "password": "colorlessgreenideas", "roles": ["Contributor"]}' --user admin:secret

Note the missing field “username”:

it is not allowed when email login is enabled .

Suppose we now want to add this user to the ploneteam group by updating this group through REST API. If we use his email as user identifier in "users":

curl -i -X PATCH http://nohost/plone/@groups/ploneteam -H 'Accept: application/json' -H 'Content-Type: application/json' --data-raw '{"email": "ploneteam2@plone.org", "users": {"noam.chomsky@example.com": true}}' --user admin:secret

fails (silently: "204 No Content response") to add this user to the ploneteam group.

By contrast, if we (manually) look at the information of this user, we'll see his user_id is noam-avram-chomsky and using in the previous command this user identifier instead of his email:

curl -i -X PATCH http://nohost/plone/@groups/ploneteam -H 'Accept: application/json' -H 'Content-Type: application/json' --data-raw '{"email": "ploneteam2@plone.org", "users": {"noam-avram-chomsky": true}}' --user admin:secret

successfully updates the group by adding him to the ploneteam group.

The problem is, this cannot be done programmatically.

Am I missing something?

The CREATE returns the userid in the response.
Use that id to send a PATCH to the @groups endpoint.

How to do that if, instead of curl, a Python script is run?

Assuming you're using requests:

response = requests.post(...)
user_id = response.json()["id"]
... do PATCH here

Works like a charm: many thanks!

Plone Foundation Code of Conduct