Opened 5 hours ago
Last modified 5 hours ago
#66720 new enhancement
Abilities API: Add the user write abilities (create, update, delete)
| Reported by: | jorgefilipecosta | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Abilities API | Version: | |
| Severity: | normal | Keywords: | has-patch has-unit-tests |
| Cc: | Focuses: |
Description
This ticket proposes three core abilities that write users, registered by Core in the user category next to core/users-query:
core/user-create("Create User") creates a user from a username and an email. Name, first name, last name, nickname, slug, description, URL, locale and roles are optional. The password is generated and never returned: the new user receives the email with the link to set one.core/user-update("Update User") changes the given attributes of one user. The attributes are the ones of create, except the username.core/user-delete("Delete User") deletes a user. On multisite it removes the user from the current site instead, and the account stays in the network.reassignis required: the ID of the user who takes over the content, orfalse.
The input and output schemas are the ones proposed in the user sections of CORE-SHAPE.md in https://github.com/WordPress/abilities/pull/22. Create and update return the same item as core/users-query, with the same fields and the same checks per field.
Permissions:
- Creating needs
create_users. - Updating needs a logged-in user and
edit_useron the user. Changing roles needspromote_useron the user. - On create and update, each role must be one the current user can assign.
- Deleting needs
delete_usersanddelete_useron the user. On multisite it needsremove_usersandremove_user.
Errors use user_* codes and carry an HTTP status in their data.
The abilities are added to the WP_Abilities_Users class, which registers core/users-query.
Related: #64657 (core/users-query), #66706 (content write abilities), #66719 (media abilities).
Follow-up to https://github.com/WordPress/wordpress-develop/pull/10775, with the shape from https://github.com/WordPress/abilities/pull/22.
This PR adds the
core/user-create,core/user-updateandcore/user-deleteabilities to theusercategory:core/user-createtakes{ data, fields }and returns the new user.data.usernameanddata.emailare required. The password is generated and never returned: the new user gets the email with the link to set one.core/user-updatetakes{ where: { id }, data, fields }, changes only the attributes indataand returns the user.core/user-deletetakes{ where: { id }, reassign }and returns{ outcome }:deleted, orremovedon multisite, where the user leaves the current site and the account stays in the network.reassignis required: the ID of the user who takes over the content, orfalse.dataacceptsemail,name,first_name,last_name,nickname,slug,description,url,localeandroles, plususernameon create.create_users, update a logged-in user and deletedelete_users(remove_userson multisite). Each call then checks the capabilities of the user for the target user and for the roles it sets.core/users-queryitem, with the samefieldsand the same checks per field.core/users-queryis registered as before: itsfieldsand item schemas moved to private helpers that the new abilities share. In its tests, the helper that registers it now unregisters the four user abilities, and the run route tests get the cases of the three new abilities.Creating a user:
{ "id": 2, "name": "Ana Silva", "link": "http://example.org/author/anasilva/", "slug": "anasilva", "avatar_urls": { "24": "https://secure.gravatar.com/avatar/86b9260a115ceac89a9738af23696ce3a5d70449de8d95ef28bd754bb80196b9?s=24&d=mm&r=g", "48": "https://secure.gravatar.com/avatar/86b9260a115ceac89a9738af23696ce3a5d70449de8d95ef28bd754bb80196b9?s=48&d=mm&r=g", "96": "https://secure.gravatar.com/avatar/86b9260a115ceac89a9738af23696ce3a5d70449de8d95ef28bd754bb80196b9?s=96&d=mm&r=g" } }Changing the role and the description, and asking for some fields:
{ "id": 2, "name": "Ana Silva", "description": "Writes the release notes.", "email": "ana@example.org", "roles": [ "editor" ] }Deleting the user and giving their content to user 1 (use the ID of a user of your site). With
reassign: falsethe content is deleted with the user:{ "outcome": "deleted" }Testing
Verify unit tests are passing:
Or run the examples above in the editor console as an administrator.
AI disclosure: the change and tests were drafted with AI assistance (Claude Code).