diff --git a/libraries/joomla/table/user.php b/libraries/joomla/table/user.php index 4f9c97b3ee22d..28e913742d1f3 100644 --- a/libraries/joomla/table/user.php +++ b/libraries/joomla/table/user.php @@ -321,24 +321,55 @@ public function store($updateNulls = false) // Store the group data if the user data was saved. if (is_array($this->groups) && count($this->groups)) { - // Delete the old user group maps. - $query->delete($this->_db->quoteName('#__user_usergroup_map')) - ->where($this->_db->quoteName('user_id') . ' = ' . (int) $this->id); + // Grab all usergroup entries for the user + $query -> clear() + -> select('*') + -> from($this->_db->quoteName('#__user_usergroup_map')) + -> where($this->_db->quoteName('user_id') . ' = ' . (int) $this->id); + $this->_db->setQuery($query); - $this->_db->execute(); + $result = $this->_db->loadObjectList(); - // Set the new user group maps. - $query->clear() - ->insert($this->_db->quoteName('#__user_usergroup_map')) - ->columns(array($this->_db->quoteName('user_id'), $this->_db->quoteName('group_id'))); + // Loop through them and check if database contains something $this->groups does not + if (count($result)) + { + foreach ($result as $map) + { + if (array_key_exists($map->group_id, $this->groups)) + { + // It already exists, no action required + unset($this->groups[$map->group_id]); + } + else + { + // It should be removed + $query -> clear() + -> delete($this->_db->quoteName('#__user_usergroup_map')) + -> where($this->_db->quoteName('user_id') . ' = ' . (int) $this->id) + -> where($this->_db->quoteName('group_id') . ' = ' . (int) $map->group_id); + + $this->_db->setQuery($query); + $this->_db->execute(); + } + } + } - // Have to break this up into individual queries for cross-database support. - foreach ($this->groups as $group) + // If there is anything left in this->groups it needs to be inserted + if (count($this->groups)) { - $query->clear('values') - ->values($this->id . ', ' . $group); - $this->_db->setQuery($query); - $this->_db->execute(); + // Set the new user group maps. + $query->clear() + ->insert($this->_db->quoteName('#__user_usergroup_map')) + ->columns(array($this->_db->quoteName('user_id'), $this->_db->quoteName('group_id'))); + + // Have to break this up into individual queries for cross-database support. + foreach ($this->groups as $group) + { + $query->clear('values') + ->values($this->id . ', ' . $group); + $this->_db->setQuery($query); + $this->_db->execute(); + } } }