Skip to content

Commit 6054027

Browse files
Role/Capability: Ensure that logged-out users cannot edit themselves.
Follow-up to [3846], [6697], [14189], [21152]. Props dd32, peterwilsoncc, johnbillion, mukesh27, swissspidy, SergeyBiryukov. Fixes #63684. git-svn-id: https://develop.svn.wordpress.org/trunk@60491 602fd350-edb4-49c9-b593-d223f7449a82
1 parent f0a3c68 commit 6054027

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

src/wp-includes/capabilities.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ function map_meta_cap( $cap, $user_id, ...$args ) {
6060
break;
6161
case 'edit_user':
6262
case 'edit_users':
63+
// Non-existent users can't edit users, not even themselves.
64+
if ( $user_id < 1 ) {
65+
$caps[] = 'do_not_allow';
66+
break;
67+
}
68+
6369
// Allow user to edit themselves.
6470
if ( 'edit_user' === $cap && isset( $args[0] ) && $user_id === (int) $args[0] ) {
6571
break;

tests/phpunit/tests/user/capabilities.php

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,13 +1830,49 @@ public function test_multisite_administrator_can_not_edit_users() {
18301830
$this->assertFalse( current_user_can( 'edit_user', $other_user->ID ) );
18311831
}
18321832

1833-
public function test_user_can_edit_self() {
1834-
foreach ( self::$users as $role => $user ) {
1835-
wp_set_current_user( $user->ID );
1836-
$this->assertTrue( current_user_can( 'edit_user', $user->ID ), "User with role {$role} should have the capability to edit their own profile" );
1833+
/**
1834+
* Test if a user can edit their own profile based on their role.
1835+
*
1836+
* @ticket 63684
1837+
*
1838+
* @dataProvider data_user_can_edit_self
1839+
*
1840+
* @param string $role The role of the user.
1841+
* @param bool $can_edit_self Whether the user can edit their own profile.
1842+
*/
1843+
public function test_user_can_edit_self( $role, $can_edit_self = true ) {
1844+
$user = self::$users[ $role ];
1845+
wp_set_current_user( $user->ID );
1846+
1847+
if ( $can_edit_self ) {
1848+
$this->assertTrue(
1849+
current_user_can( 'edit_user', $user->ID ),
1850+
"User with role '{$role}' should have the capability to edit their own profile"
1851+
);
1852+
} else {
1853+
$this->assertFalse(
1854+
current_user_can( 'edit_user', $user->ID ),
1855+
"User with role '{$role}' should not have the capability to edit their own profile"
1856+
);
18371857
}
18381858
}
18391859

1860+
/**
1861+
* Data provider for test_user_can_edit_self.
1862+
*
1863+
* @return array[] Data provider.
1864+
*/
1865+
public static function data_user_can_edit_self() {
1866+
return array(
1867+
'anonymous' => array( 'anonymous', false ),
1868+
'administrator' => array( 'administrator', true ),
1869+
'editor' => array( 'editor', true ),
1870+
'author' => array( 'author', true ),
1871+
'contributor' => array( 'contributor', true ),
1872+
'subscriber' => array( 'subscriber', true ),
1873+
);
1874+
}
1875+
18401876
public function test_only_admins_and_super_admins_can_remove_users() {
18411877
if ( is_multisite() ) {
18421878
$this->assertTrue( user_can( self::$super_admin->ID, 'remove_user', self::$users['subscriber']->ID ) );

0 commit comments

Comments
 (0)