Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

43251 add a check for editable roles #6808

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/wp-admin/includes/ms.php
Original file line number Diff line number Diff line change
Expand Up @@ -1172,3 +1172,24 @@ function get_site_screen_help_sidebar_content() {
'<p>' . __( '<a href="https://developer.wordpress.org/advanced-administration/multisite/admin/#network-admin-sites-screen">Documentation on Site Management</a>' ) . '</p>' .
'<p>' . __( '<a href="https://wordpress.org/support/forum/multisite/">Support forums</a>' ) . '</p>';
}

Comment on lines 1174 to +1175
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removes an additional line break.

Suggested change
}
}


/**
* Makes sure the passed $role is part of editable_roles
*
* @since 6.7.0
*
* @param string $role - name of the role
* @return void
*/

function ensure_editable_role( $role ) {
Comment on lines +1177 to +1186
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main change is to rename the function to use the wp_ prefix

Suggested change
/**
* Makes sure the passed $role is part of editable_roles
*
* @since 6.7.0
*
* @param string $role - name of the role
* @return void
*/
function ensure_editable_role( $role ) {
/**
* Stop execution if the role can not be assigned by the current user.
*
* @since 6.7.0
*
* @param string $role Role the user is attempting to assign.
*/
function wp_ensure_editable_role( $role ) {

$roles = get_editable_roles();
if ( ! isset( $roles[ $role ] ) ) {
wp_die(
'<h1>' . __( 'You need a higher level of permission.' ) . '</h1>' .
'<p>' . __( 'Sorry, you are not allowed to assign users to this role.' ) . '</p>',
403
);
Comment on lines +1189 to +1193
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the filter prevents users from adding the role, the level of permission heading is misleading.

I've modified the message to match the one for single sites. WordPress aims to reuse strings where possible to avoid the need for similar strings to be translated.

Suggested change
wp_die(
'<h1>' . __( 'You need a higher level of permission.' ) . '</h1>' .
'<p>' . __( 'Sorry, you are not allowed to assign users to this role.' ) . '</p>',
403
);
wp_die( __( 'Sorry, you are not allowed to give users that role.' ), 403 );

}
}
6 changes: 6 additions & 0 deletions src/wp-admin/user-new.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@
$redirect = add_query_arg( array( 'update' => 'addexisting' ), 'user-new.php' );
} else {
if ( isset( $_POST['noconfirmation'] ) && current_user_can( 'manage_network_users' ) ) {

ensure_editable_role( $_REQUEST['role'] );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ensure_editable_role( $_REQUEST['role'] );
wp_ensure_editable_role( $_REQUEST['role'] );


$result = add_existing_user_to_blog(
array(
'user_id' => $user_id,
Expand Down Expand Up @@ -218,6 +221,9 @@
add_filter( 'wpmu_signup_user_notification', '__return_false' ); // Disable confirmation email.
add_filter( 'wpmu_welcome_user_notification', '__return_false' ); // Disable welcome email.
}

ensure_editable_role( $_REQUEST['role'] );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ensure_editable_role( $_REQUEST['role'] );
wp_ensure_editable_role( $_REQUEST['role'] );


wpmu_signup_user(
$new_user_login,
$new_user_email,
Expand Down
44 changes: 44 additions & 0 deletions tests/phpunit/tests/multisite/wpmuValidateUserSignup.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,50 @@ public function test_signup_nonce_check_invalid() {

$this->assertContains( 'invalid_nonce', $valid['errors']->get_error_codes() );
}

public function test_ensure_editable_role() {
$exception = null;
try {
ensure_editable_role( 'editor' );
} catch ( WPDieException $e ) {
$exception = $e;
}
$this->assertNull( $exception );

$exception = null;
try {
ensure_editable_role( 'non-existant' );
} catch ( WPDieException $e ) {
$exception = $e;
}
$this->assertNotNull( $exception );
$this->assertStringContainsString( 'Sorry, you are not allowed to assign users to this role.', $exception->getMessage() );

$exception = null;
try {
add_filter(
'editable_roles',
function ( $roles ) {
unset( $roles['administrator'] );
return $roles;
}
);

ensure_editable_role( 'administrator' );

remove_filter(
'editable_roles',
function ( $roles ) {
unset( $roles['administrator'] );
return $roles;
}
);
} catch ( WPDieException $e ) {
$exception = $e;
}
$this->assertNotNull( $exception );
$this->assertStringContainsString( 'Sorry, you are not allowed to assign users to this role.', $exception->getMessage() );
}
Comment on lines +224 to +266
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a few things happening here:

  • I've added docblocks and the ticket number to the tests so they can be run as their own group
  • I've split the tests up in to the three cases: editable role, unknown role, excluded role
  • I've used $this->expectException( 'WPDieException' ); to test for the wp_die() call to remove the need for try...catch
  • I've used the name change suggested above.
  • The WordPress test suite resets filters between tests so the remove_filter() isn't needed.
Suggested change
public function test_ensure_editable_role() {
$exception = null;
try {
ensure_editable_role( 'editor' );
} catch ( WPDieException $e ) {
$exception = $e;
}
$this->assertNull( $exception );
$exception = null;
try {
ensure_editable_role( 'non-existant' );
} catch ( WPDieException $e ) {
$exception = $e;
}
$this->assertNotNull( $exception );
$this->assertStringContainsString( 'Sorry, you are not allowed to assign users to this role.', $exception->getMessage() );
$exception = null;
try {
add_filter(
'editable_roles',
function ( $roles ) {
unset( $roles['administrator'] );
return $roles;
}
);
ensure_editable_role( 'administrator' );
remove_filter(
'editable_roles',
function ( $roles ) {
unset( $roles['administrator'] );
return $roles;
}
);
} catch ( WPDieException $e ) {
$exception = $e;
}
$this->assertNotNull( $exception );
$this->assertStringContainsString( 'Sorry, you are not allowed to assign users to this role.', $exception->getMessage() );
}
/**
* Ensure that wp_ensure_editable_role does not throw an exception when the role is editable.
*
* @ticket 43251
*
* @covers ::wp_ensure_editable_role
*/
public function test_wp_ensure_editable_role_allows_editable_roles() {
$role = get_role( 'editor' );
$this->assertInstanceOf( 'WP_Role', $role, 'The editor role should exist.' );
$this->assertNull( wp_ensure_editable_role( 'editor' ), 'The editor role should be editable.' );
}
/**
* Ensure that wp_ensure_editable_role throws an exception for non-existent roles.
*
* @ticket 43251
*
* @covers ::wp_ensure_editable_role
*/
public function test_wp_ensure_editable_role_does_not_allow_non_existent_role() {
$this->expectException( 'WPDieException' );
$role = get_role( 'non-existent-role' );
$this->assertNotInstanceOf( 'WP_Role', $role, 'The non-existent-role role should not exist.' );
wp_ensure_editable_role( 'non-existent-role' );
}
/**
* Ensure that wp_ensure_editable_role throws an exception for roles that are not editable.
*
* @ticket 43251
*
* @covers ::wp_ensure_editable_role
*/
public function test_wp_ensure_editable_role_does_not_allow_uneditable_roles() {
add_filter(
'editable_roles',
function ( $roles ) {
unset( $roles['editor'] );
return $roles;
}
);
$this->expectException( 'WPDieException' );
$role = get_role( 'editor' );
$this->assertInstanceOf( 'WP_Role', $role, 'The editor role should exist.' );
wp_ensure_editable_role( 'editor' );
}

}

endif;
Loading