Skip to content

Commit

Permalink
MDL-56065 user: Fix update_users Web Service
Browse files Browse the repository at this point in the history
Users won’t be updated if:
- They are admins and the user updating is not
- They are the guest user
- They are mnet users
- They are deleted users
  • Loading branch information
jleyva authored and Mr. Jenkins (CiBoT) committed Nov 10, 2016
1 parent 179a26f commit c3bb491
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
14 changes: 13 additions & 1 deletion user/externallib.php
Expand Up @@ -415,7 +415,7 @@ public static function update_users_parameters() {
* @since Moodle 2.2
*/
public static function update_users($users) {
global $CFG, $DB;
global $CFG, $DB, $USER;
require_once($CFG->dirroot."/user/lib.php");
require_once($CFG->dirroot."/user/profile/lib.php"); // Required for customfields related function.

Expand All @@ -429,6 +429,18 @@ public static function update_users($users) {
$transaction = $DB->start_delegated_transaction();

foreach ($params['users'] as $user) {
// First check the user exists.
if (!$existinguser = core_user::get_user($user['id'])) {
continue;
}
// Check if we are trying to update an admin.
if ($existinguser->id != $USER->id and is_siteadmin($existinguser) and !is_siteadmin($USER)) {
continue;
}
// Other checks (deleted, remote or guest users).
if ($existinguser->deleted or is_mnet_remote_user($existinguser) or isguestuser($existinguser->id)) {
continue;
}
user_update_user($user, true, false);
// Update user custom fields.
if (!empty($user['customfields'])) {
Expand Down
20 changes: 19 additions & 1 deletion user/tests/externallib_test.php
Expand Up @@ -588,8 +588,26 @@ public function test_update_users() {
$context = context_system::instance();
$roleid = $this->assignUserCapability('moodle/user:update', $context->id);

// Check we can't update deleted users, guest users, site admin.
$user2 = $user3 = $user4 = $user1;
$user2['id'] = $CFG->siteguest;

$siteadmins = explode(',', $CFG->siteadmins);
$user3['id'] = array_shift($siteadmins);

$userdeleted = self::getDataGenerator()->create_user();
$user4['id'] = $userdeleted->id;
user_delete_user($userdeleted);

// Call the external function.
core_user_external::update_users(array($user1));
core_user_external::update_users(array($user1, $user2, $user3, $user4));

$dbuser2 = $DB->get_record('user', array('id' => $user2['id']));
$this->assertNotEquals($dbuser2->username, $user2['username']);
$dbuser3 = $DB->get_record('user', array('id' => $user3['id']));
$this->assertNotEquals($dbuser3->username, $user3['username']);
$dbuser4 = $DB->get_record('user', array('id' => $user4['id']));
$this->assertNotEquals($dbuser4->username, $user4['username']);

$dbuser = $DB->get_record('user', array('id' => $user1['id']));
$this->assertEquals($dbuser->username, $user1['username']);
Expand Down

0 comments on commit c3bb491

Please sign in to comment.