Make WordPress Core

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. reassign is required: the ID of the user who takes over the content, or false.

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_user on the user. Changing roles needs promote_user on the user.
  • On create and update, each role must be one the current user can assign.
  • Deleting needs delete_users and delete_user on the user. On multisite it needs remove_users and remove_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).

Change History (1)

This ticket was mentioned in ​PR #16074 on ​WordPress/wordpress-develop by ​@jorgefilipecosta.


5 hours ago
#1

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-update and core/user-delete abilities to the user category:

  • core/user-create takes { data, fields } and returns the new user. data.username and data.email are required. The password is generated and never returned: the new user gets the email with the link to set one.
  • core/user-update takes { where: { id }, data, fields }, changes only the attributes in data and returns the user.
  • core/user-delete takes { where: { id }, reassign } and returns { outcome }: deleted, or removed on multisite, where the user leaves the current site and the account stays in the network. reassign is required: the ID of the user who takes over the content, or false.
  • data accepts email, name, first_name, last_name, nickname, slug, description, url, locale and roles, plus username on create.
  • Create requires create_users, update a logged-in user and delete delete_users (remove_users on multisite). Each call then checks the capabilities of the user for the target user and for the roles it sets.
  • The output is the core/users-query item, with the same fields and the same checks per field.
  • core/users-query is registered as before: its fields and 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:

const user = await wp.apiFetch( {
        path: '/wp-abilities/v1/abilities/core/user-create/run',
        method: 'POST',
        data: {
                input: {
                        data: { username: 'anasilva', email: 'ana@example.org', name: 'Ana Silva', roles: [ 'author' ] },
                },
        },
} );
{
	"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:

await wp.apiFetch( {
        path: '/wp-abilities/v1/abilities/core/user-update/run',
        method: 'POST',
        data: {
                input: {
                        where: { id: user.id },
                        data: { roles: [ 'editor' ], description: 'Writes the release notes.' },
                        fields: [ 'name', 'email', 'roles', 'description' ],
                },
        },
} );
{ "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: false the content is deleted with the user:

await wp.apiFetch( {
        path: '/wp-abilities/v1/abilities/core/user-delete/run',
        method: 'POST',
        data: { input: { where: { id: user.id }, reassign: 1 } },
} );
{ "outcome": "deleted" }

Testing

Verify unit tests are passing:

npm run test:php -- --group 66720

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).

Note: See TracTickets for help on using tickets.