Skip to content

Commit 76b6c9a

Browse files
reolver level field filtering
1 parent 69a38fe commit 76b6c9a

1 file changed

Lines changed: 65 additions & 33 deletions

File tree

src/wp-includes/abilities/class-wp-users-abilities.php

Lines changed: 65 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ private static function get_user_input_schema(): array {
8888
'type' => 'string',
8989
'description' => __( 'User login name.' ),
9090
),
91-
'email' => array(
92-
'type' => 'string',
93-
'format' => 'email',
94-
'description' => __( 'User email address.' ),
95-
),
91+
'email' => array(
92+
'type' => 'string',
93+
'format' => 'email',
94+
'description' => __( 'User email address.' ),
95+
),
9696
'include_capabilities' => array(
9797
'type' => 'boolean',
9898
'description' => __( 'Whether to include the user capabilities in the response.' ),
@@ -103,6 +103,30 @@ private static function get_user_input_schema(): array {
103103
);
104104
}
105105

106+
/**
107+
* Determines whether sensitive fields can be returned for a user.
108+
*
109+
* @since 7.0.0
110+
*
111+
* @param WP_User $user The target user.
112+
* @return bool Whether sensitive fields can be returned.
113+
*/
114+
private static function can_view_sensitive_user_fields( WP_User $user ): bool {
115+
return get_current_user_id() === $user->ID || current_user_can( 'edit_user', $user->ID );
116+
}
117+
118+
/**
119+
* Determines whether roles can be returned for a user.
120+
*
121+
* @since 7.0.0
122+
*
123+
* @param WP_User $user The target user.
124+
* @return bool Whether roles can be returned.
125+
*/
126+
private static function can_view_user_roles( WP_User $user ): bool {
127+
return current_user_can( 'list_users' ) || current_user_can( 'edit_user', $user->ID );
128+
}
129+
106130
/**
107131
* Gets the output schema for the get-user ability.
108132
*
@@ -137,12 +161,12 @@ private static function get_user_output_schema(): array {
137161
'id',
138162
'username',
139163
),
140-
'properties' => array(
141-
'avatar_urls' => $avatar_urls_schema,
142-
'capabilities' => array(
143-
'type' => 'object',
144-
'description' => __( 'All capabilities assigned to the user. Only included if include_capabilities is true.' ),
145-
),
164+
'properties' => array(
165+
'avatar_urls' => $avatar_urls_schema,
166+
'capabilities' => array(
167+
'type' => 'object',
168+
'description' => __( 'All capabilities assigned to the user. Only included if include_capabilities is true and the current user can view them.' ),
169+
),
146170
'description' => array(
147171
'type' => 'string',
148172
'description' => __( 'Description of the user.' ),
@@ -187,13 +211,13 @@ private static function get_user_output_schema(): array {
187211
'format' => 'date-time',
188212
'description' => __( 'Registration date for the user in ISO 8601 format.' ),
189213
),
190-
'roles' => array(
191-
'type' => 'array',
192-
'description' => __( 'Roles assigned to the user.' ),
193-
'items' => array(
194-
'type' => 'string',
214+
'roles' => array(
215+
'type' => 'array',
216+
'description' => __( 'Roles assigned to the user when the current user can view them.' ),
217+
'items' => array(
218+
'type' => 'string',
219+
),
195220
),
196-
),
197221
'slug' => array(
198222
'type' => 'string',
199223
'description' => __( 'An alphanumeric identifier for the user.' ),
@@ -284,25 +308,33 @@ public static function execute_get_user( array $input = array() ) {
284308
return new WP_Error( 'user_not_found', __( 'User not found.' ), array( 'status' => 404 ) );
285309
}
286310

311+
$can_view_sensitive_user_fields = self::can_view_sensitive_user_fields( $user );
312+
287313
$result = array(
288-
'id' => $user->ID,
289-
'username' => $user->user_login,
290-
'email' => $user->user_email,
291-
'display_name' => $user->display_name,
292-
'first_name' => $user->first_name,
293-
'last_name' => $user->last_name,
294-
'nickname' => $user->nickname,
295-
'description' => $user->description,
296-
'url' => $user->user_url,
297-
'link' => get_author_posts_url( $user->ID, $user->user_nicename ),
298-
'slug' => $user->user_nicename,
299-
'registered_date' => gmdate( 'c', strtotime( $user->user_registered ) ),
300-
'roles' => array_values( $user->roles ),
301-
'locale' => get_user_locale( $user ),
302-
'avatar_urls' => rest_get_avatar_urls( $user ),
314+
'id' => $user->ID,
315+
'display_name' => $user->display_name,
316+
'description' => $user->description,
317+
'url' => $user->user_url,
318+
'link' => get_author_posts_url( $user->ID, $user->user_nicename ),
319+
'slug' => $user->user_nicename,
320+
'avatar_urls' => rest_get_avatar_urls( $user ),
303321
);
304322

305-
if ( $include_capabilities ) {
323+
if ( $can_view_sensitive_user_fields ) {
324+
$result['username'] = $user->user_login;
325+
$result['email'] = $user->user_email;
326+
$result['first_name'] = $user->first_name;
327+
$result['last_name'] = $user->last_name;
328+
$result['nickname'] = $user->nickname;
329+
$result['registered_date'] = gmdate( 'c', strtotime( $user->user_registered ) );
330+
$result['locale'] = get_user_locale( $user );
331+
}
332+
333+
if ( self::can_view_user_roles( $user ) ) {
334+
$result['roles'] = array_values( $user->roles );
335+
}
336+
337+
if ( $include_capabilities && $can_view_sensitive_user_fields ) {
306338
$result['capabilities'] = (object) $user->allcaps;
307339
}
308340

0 commit comments

Comments
 (0)