From f704fc808a9fae551d22312701ed30a9ea4c3e9d Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Mon, 12 Dec 2016 21:42:42 +0000 Subject: [PATCH] Multisite: Handle capability check for removing oneself via `map_meta_cap()`. Site administrators should not be able to remove themselves from a site. This moves the enforcement of this rule from `wp-admin/users.php` to `remove_user_from_blog()` via the `remove_user` capability, which furthermore allows us to get rid of two additional clauses and their `is_super_admin()` checks in `wp-admin/users.php`. A unit test for the new behavior has been added. Fixes #39063. See #37616. Built from https://develop.svn.wordpress.org/trunk@39588 git-svn-id: http://core.svn.wordpress.org/trunk@39528 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/users.php | 9 +-------- wp-includes/capabilities.php | 7 ++++++- wp-includes/version.php | 2 +- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/wp-admin/users.php b/wp-admin/users.php index 214c06d8d3..95d9f768bb 100644 --- a/wp-admin/users.php +++ b/wp-admin/users.php @@ -321,10 +321,6 @@ case 'doremove': $update = 'remove'; foreach ( $userids as $id ) { $id = (int) $id; - if ( $id == $current_user->ID && !is_super_admin() ) { - $update = 'err_admin_remove'; - continue; - } if ( !current_user_can('remove_user', $id) ) { $update = 'err_admin_remove'; continue; @@ -377,10 +373,7 @@ case 'remove': foreach ( $userids as $id ) { $id = (int) $id; $user = get_userdata( $id ); - if ( $id == $current_user->ID && !is_super_admin() ) { - /* translators: 1: user id, 2: user login */ - echo "
  • " . sprintf(__('ID #%1$s: %2$s The current user will not be removed.'), $id, $user->user_login) . "
  • \n"; - } elseif ( !current_user_can('remove_user', $id) ) { + if ( ! current_user_can( 'remove_user', $id ) ) { /* translators: 1: user id, 2: user login */ echo "
  • " . sprintf(__('ID #%1$s: %2$s Sorry, you are not allowed to remove this user.'), $id, $user->user_login) . "
  • \n"; } else { diff --git a/wp-includes/capabilities.php b/wp-includes/capabilities.php index 308c7063c8..78cde1c2ee 100644 --- a/wp-includes/capabilities.php +++ b/wp-includes/capabilities.php @@ -32,7 +32,12 @@ function map_meta_cap( $cap, $user_id ) { switch ( $cap ) { case 'remove_user': - $caps[] = 'remove_users'; + // In multisite the user must be a super admin to remove themselves. + if ( isset( $args[0] ) && $user_id == $args[0] && ! is_super_admin( $user_id ) ) { + $caps[] = 'do_not_allow'; + } else { + $caps[] = 'remove_users'; + } break; case 'promote_user': case 'add_users': diff --git a/wp-includes/version.php b/wp-includes/version.php index 99ebccd19e..8aee5002f9 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.8-alpha-39586'; +$wp_version = '4.8-alpha-39588'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.