Repository navigation
Abilities API: Add the core/user-create, core/user-update and core/user-delete abilities - #16074
jorgefilipecosta wants to merge 4 commits into
Conversation
…re/user-delete` abilities. Add three abilities to the `user` category: - `core/user-create` creates a user from `data` and returns it. The password is generated and never returned: the new user receives the email with the link to set one. - `core/user-update` updates the given attributes of the user matched by `where` and returns it. - `core/user-delete` deletes the user matched by `where`, or on multisite removes the user from the current site. `reassign` is required: the ID of the user who takes over the content, or false. The attributes that can be written are the email, name, first name, last name, nickname, slug, description, URL, locale and roles, plus the username on create. Each call checks the capabilities of the current user for the target user and for the roles it sets. The `fields` and item schemas of `core/users-query` move to private helpers that the new abilities share. That ability is registered as before.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
Replace the `@ticket` placeholder in the tests of `core/user-create`, `core/user-update` and `core/user-delete` with 66720. See #66720.
| return array( | ||
| 'type' => 'object', | ||
| 'additionalProperties' => false, | ||
| 'required' => array( 'where', 'reassign' ), |
There was a problem hiding this comment.
I spent a good amount of time analyzing how the REST API handles deleting and removing users. It led me to one suggestion: could permanent be a required input for this ability?
Users have no Trash, so on a single site this delete cannot be undone. REST returns 501 there when a user is deleted without force: true. This ability deletes the account at once. A required permanent would do the same job as force, and the caller would have to state if the action is final.
The field could be required in both site modes, and the accepted value could follow what the site can do:
permanent: true |
permanent: false |
|
|---|---|---|
| Single site | Deletes the account. The result is deleted |
Refused with user_trash_not_supported, because users have no Trash |
| Multisite | Refused with a 501 error, because the account cannot be deleted here | Removes the user from the site. The result is removed |
The input then has the same shape in both site modes, and a caller who asks for a final delete never gets a removal. REST refuses in the same two cases. It returns 501 without force on a single site, and 501 for every user delete on multisite, also for a network admin.
This also matches the other delete abilities. core/content-delete and core/media-delete use permanent: true for a final delete. They return content_trash_not_supported (#16028) and media_trash_not_supported (#16138) when an item cannot go to the Trash.
The same field would fit core/term-delete in #16102, and the design in WordPress/abilities#22 would need the update too.
|
|
||
| if ( array_key_exists( 'slug', $data ) ) { | ||
| /** This filter is documented in wp-includes/user.php */ | ||
| $slug = (string) apply_filters( 'pre_user_nicename', sanitize_title( sanitize_user( wp_slash( $data['slug'] ), true ) ) ); |
There was a problem hiding this comment.
REST and this ability clean a slug in a different order, so the same input can give a different saved slug.
- REST runs
sanitize_title()first, and then passes the result towp_insert_user(). - The ability passes the slug straight to
wp_insert_user(), which runs strictsanitize_user()beforesanitize_title().
The order matters for a character such as /. sanitize_title() turns it into a hyphen, but strict sanitize_user() removes it. I tested this with Team/Desk: REST saves team-desk, and core/user-update saves teamdesk. I also confirmed that core/content-query then finds the author's posts only with teamdesk, so a client that expects the REST spelling can miss them.
Could we run sanitize_title() on the slug first, as REST does, and cover it with a test?
| 'roles' => array( | ||
| 'type' => 'array', | ||
| 'uniqueItems' => true, | ||
| 'minItems' => 1, |
There was a problem hiding this comment.
REST and the Users screen can put a user into "no role". All roles are removed, the account and the content stay, and the user can no longer use the dashboard. It is a common way to block a user without deleting the account. core/user-update cannot do it, because roles: [] is refused here and again in check_roles().
Could an empty list be allowed on update, for other users? Leaving roles out would still keep the roles. I tried set_user_roles() with an empty list, and it already clears the roles and keeps the membership.
One case should stay refused: clearing your own roles. Today the ability already stops an administrator from giving themselves a role that cannot edit users. That check runs inside the loop over the new roles, so it would not run for an empty list. The Users screen refuses this case with "Sorry, you cannot remove your own role."
| 'type' => array( 'integer', 'boolean' ), | ||
| 'minimum' => 1, | ||
| 'description' => __( 'The ID of the user who takes over the content of the deleted user, or false to reassign nothing. True is not accepted.' ), |
There was a problem hiding this comment.
Could reassign take a user ID or null, in place of a user ID or false?
'type' => array( 'integer', 'null' ),
'minimum' => 1,- The schema becomes exact. In my tests,
nulland a valid user ID pass, andfalse,true,0and an empty string are refused by the schema. Todaytruepasses the schema and is refused later by the code. - It matches the design.
reassignis the only union in the design document that is not "a type ornull".
I saw the note that null is refused on purpose. The field would stay required, so a missing value is still refused. The code would need array_key_exists() to tell a missing value from null.
One more thing about the description. "Reassign nothing" sounds harmless, but on a single site the user's content is then deleted or moved to the Trash. The ability description explains this well. Could this field repeat the main point?
| if ( $slug_user && ( ! $target || $slug_user->ID !== $target->ID ) ) { | ||
| return $this->invalid_attribute_error( 'data[slug]', __( 'Invalid slug.' ) ); |
There was a problem hiding this comment.
Low priority. This works correctly, but the error could be more informative.
A slug that another user already has and a slug that is empty after cleaning, such as !!!, both return user_invalid_attribute with "Invalid slug.". The caller cannot tell "pick another slug" from "fix the format". Taken usernames and emails already return user_exists. Could a taken slug do the same?
The strict rule itself looks right to me. When no slug is sent, core still picks a free one, such as desk-2.
| 'required' => array( 'outcome' ), | ||
| 'properties' => array( | ||
| 'outcome' => array( | ||
| 'type' => 'string', |
There was a problem hiding this comment.
The output schema for core/user-delete accepts any string for outcome. The ability returns deleted for a single-site account deletion and removed for a multisite membership removal. Could we list both values in an enum, here and in the design? Then clients can check the response.
…er write abilities. `core/user-create` and `core/user-update` passed the slug to the save as it was sent, so a character such as a slash was removed instead of becoming a hyphen. The slug is now cleaned with `sanitize_title()` first, and the checks and the save both start from that cleaned value: `Team/Desk` is saved as `team-desk`. A slug that another user already has now returns `user_exists`, as a taken username or email does, so a caller can tell it from a slug that is empty after cleaning. The description of the `reassign` input of `core/user-delete` now says what happens to the content when no user takes it over. See #66720.
| $url, | ||
| array( | ||
| 'type' => 'string', | ||
| 'minLength' => 1, |
There was a problem hiding this comment.
Set data.url to javascript:alert(1) in a profile update. The error says the value must be at least 1 character long. The value has characters. WordPress refuses its URL scheme, so nothing is left after sanitizing.
Refusing it is good. REST accepts the same value and saves an empty URL. Could we use the existing Invalid URL. message, so the caller knows what to fix?
| * address sent is refused, rather than stored changed. | ||
| */ | ||
| /** This filter is documented in wp-includes/user.php */ | ||
| if ( wp_unslash( apply_filters( 'pre_user_email', wp_slash( $data['email'] ) ) ) !== $data['email'] ) { |
There was a problem hiding this comment.
Very low priority. Everything is saved correctly.
Updating another user's email through the ability runs pre_user_email twice, once in this check and once in the save. REST and wp_update_user() run it once. I counted the calls in a test. A plugin that logs inside this filter would record one change twice.
I think the second call is fine, because the check needs to know what the save would store. Could the comment above also say that the filter runs again in the save? The slug and URL checks call their filters in the same way.
…ite abilities. `core/user-create` and `core/user-update` refuse a URL that has nothing left after sanitizing, such as one with a scheme that is not allowed. The error said the value must be at least 1 character long, although a value was sent. It now has the existing "Invalid URL." message, so the caller knows what to fix. The comments of the attribute checks now say that the `pre_user_email`, `pre_user_display_name`, `pre_user_nicename` and `pre_user_url` filters run in the check and again in the save. See #66720.
Follow-up to #10775, with the shape from WordPress/abilities#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" }Trac ticket: https://core.trac.wordpress.org/ticket/66720
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).