Skip to content

Commit cad2f8c

Browse files
committed
Users: Use editable_roles filter for multisite sub-sites.
Adds a check of the `editable_roles` filter when adding users to a multisite sub-site to ensure the role is permitted to be used on the network. If the role is blocked by the filter, attempting to add the role will trigger a `wp_die()` similar to attempting to add a user with the role on a single site install. Props eartboard, hareesh-pillai, ideag, sukhendu2002, spacedmonkey, thomaswm. Fixes #43251. git-svn-id: https://develop.svn.wordpress.org/trunk@59901 602fd350-edb4-49c9-b593-d223f7449a82
1 parent e30ab16 commit cad2f8c

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

src/wp-admin/includes/ms.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,3 +1172,17 @@ function get_site_screen_help_sidebar_content() {
11721172
'<p>' . __( '<a href="https://developer.wordpress.org/advanced-administration/multisite/admin/#network-admin-sites-screen">Documentation on Site Management</a>' ) . '</p>' .
11731173
'<p>' . __( '<a href="https://wordpress.org/support/forum/multisite/">Support forums</a>' ) . '</p>';
11741174
}
1175+
1176+
/**
1177+
* Stop execution if the role can not be assigned by the current user.
1178+
*
1179+
* @since 6.8.0
1180+
*
1181+
* @param string $role Role the user is attempting to assign.
1182+
*/
1183+
function wp_ensure_editable_role( $role ) {
1184+
$roles = get_editable_roles();
1185+
if ( ! isset( $roles[ $role ] ) ) {
1186+
wp_die( __( 'Sorry, you are not allowed to give users that role.' ), 403 );
1187+
}
1188+
}

src/wp-admin/user-new.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@
6969
$redirect = add_query_arg( array( 'update' => 'addexisting' ), 'user-new.php' );
7070
} else {
7171
if ( isset( $_POST['noconfirmation'] ) && current_user_can( 'manage_network_users' ) ) {
72+
73+
wp_ensure_editable_role( $_REQUEST['role'] );
74+
7275
$result = add_existing_user_to_blog(
7376
array(
7477
'user_id' => $user_id,
@@ -225,6 +228,8 @@
225228
add_filter( 'wpmu_welcome_user_notification', '__return_false' ); // Disable welcome email.
226229
}
227230

231+
wp_ensure_editable_role( $_REQUEST['role'] );
232+
228233
wpmu_signup_user(
229234
$new_user_login,
230235
$new_user_email,

tests/phpunit/tests/multisite/wpmuValidateUserSignup.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,54 @@ public function test_signup_nonce_check_invalid() {
220220

221221
$this->assertContains( 'invalid_nonce', $valid['errors']->get_error_codes() );
222222
}
223+
224+
/**
225+
* Ensure that wp_ensure_editable_role does not throw an exception when the role is editable.
226+
*
227+
* @ticket 43251
228+
*
229+
* @covers ::wp_ensure_editable_role
230+
*/
231+
public function test_wp_ensure_editable_role_allows_editable_roles() {
232+
$role = get_role( 'editor' );
233+
$this->assertInstanceOf( 'WP_Role', $role, 'The editor role should exist.' );
234+
$this->assertNull( wp_ensure_editable_role( 'editor' ), 'The editor role should be editable.' );
235+
}
236+
237+
/**
238+
* Ensure that wp_ensure_editable_role throws an exception for non-existent roles.
239+
*
240+
* @ticket 43251
241+
*
242+
* @covers ::wp_ensure_editable_role
243+
*/
244+
public function test_wp_ensure_editable_role_does_not_allow_non_existent_role() {
245+
$this->expectException( 'WPDieException' );
246+
$role = get_role( 'non-existent-role' );
247+
$this->assertNotInstanceOf( 'WP_Role', $role, 'The non-existent-role role should not exist.' );
248+
wp_ensure_editable_role( 'non-existent-role' );
249+
}
250+
251+
/**
252+
* Ensure that wp_ensure_editable_role throws an exception for roles that are not editable.
253+
*
254+
* @ticket 43251
255+
*
256+
* @covers ::wp_ensure_editable_role
257+
*/
258+
public function test_wp_ensure_editable_role_does_not_allow_uneditable_roles() {
259+
add_filter(
260+
'editable_roles',
261+
function ( $roles ) {
262+
unset( $roles['editor'] );
263+
return $roles;
264+
}
265+
);
266+
$this->expectException( 'WPDieException' );
267+
$role = get_role( 'editor' );
268+
$this->assertInstanceOf( 'WP_Role', $role, 'The editor role should exist.' );
269+
wp_ensure_editable_role( 'editor' );
270+
}
223271
}
224272

225273
endif;

0 commit comments

Comments
 (0)