Commit d6555bcf authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gaston

Removal of user account now removes also user's roster.


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@321 b35dd754-fafc-0310-a699-88a17e54d16e
parent e00ba786
......@@ -23,10 +23,8 @@ import org.jivesoftware.messenger.*;
import org.jivesoftware.messenger.auth.Permissions;
import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.messenger.spi.PresenceImpl;
import org.jivesoftware.messenger.user.User;
import org.jivesoftware.messenger.user.UserAlreadyExistsException;
import org.jivesoftware.messenger.user.UserManager;
import org.jivesoftware.messenger.user.UserNotFoundException;
import org.jivesoftware.messenger.user.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
......@@ -70,6 +68,11 @@ import org.dom4j.QName;
public class IQRegisterHandler extends IQHandler implements ServerFeaturesProvider {
private static MetaDataFragment probeResult;
public UserManager userManager;
public RosterManager rosterManager;
public PresenceUpdateHandler presenceHandler;
private IQHandlerInfo info;
// TODO: this value needs to be shared across all instances but not across the entire jvm...
private static boolean enabled;
......@@ -206,6 +209,9 @@ public class IQRegisterHandler extends IQHandler implements ServerFeaturesProvid
presenceHandler.process(presence);
// Delete the user
userManager.deleteUser(userManager.getUser(session.getUsername()));
// Delete the roster of the user
rosterManager.deleteRoster(session.getAddress());
reply = packet.createResult();
session.getConnection().deliver(reply);
// Close the user's connection
......@@ -355,12 +361,10 @@ public class IQRegisterHandler extends IQHandler implements ServerFeaturesProvid
delegates.remove(serviceName);
}
public UserManager userManager;
public PresenceUpdateHandler presenceHandler;
protected TrackInfo getTrackInfo() {
TrackInfo trackInfo = super.getTrackInfo();
trackInfo.getTrackerClasses().put(UserManager.class, "userManager");
trackInfo.getTrackerClasses().put(RosterManager.class, "rosterManager");
trackInfo.getTrackerClasses().put(PresenceUpdateHandler.class, "presenceHandler");
return trackInfo;
}
......
......@@ -67,17 +67,12 @@ public interface RosterItemProvider {
void deleteItem(String username, long rosterItemID) throws UnsupportedOperationException;
/**
* <p>Delete all roster items associated with the given user (optional operation).</p>
* <p/>
* <p>This method is used by Messenger during user account removal. It will NOT be called
* if UserAccountProvider.deleteUser() throws UnsupportedOperationException.</p>
* <p/>
* <p>If you don't want roster items deleted through messenger, throw UnsupportedOperationException.</p>
* Returns an iterator on the usernames whose roster includes the specified JID.
*
* @param username the username of the user/chatbot that owns the roster items
* @throws UnsupportedOperationException If the provider does not support the operation (this is an optional operation)
* @param jid the jid that the rosters should include.
* @return an iterator on the usernames whose roster includes the specified JID.
*/
void deleteItems(String username) throws UnsupportedOperationException;
Iterator<String> getUsernames(String jid);
/**
* <p>Obtain a count of the number of roster items available for the given user.</p>
......
......@@ -11,6 +11,8 @@
package org.jivesoftware.messenger.user;
import org.jivesoftware.messenger.XMPPAddress;
/**
* <p>A simple service that allows components to retrieve a roster based solely on the ID of the owner.</p>
* <p/>
......@@ -31,4 +33,12 @@ public interface RosterManager {
* @throws UserNotFoundException If the ID does not correspond to a known entity on the server
*/
CachedRoster getRoster(String username) throws UserNotFoundException;
/**
* Removes the entire roster of a given user. This is necessary when a user account is being
* deleted from the server.
*
* @param user the user to remove his roster.
*/
void deleteRoster(XMPPAddress user);
}
......@@ -16,7 +16,6 @@ import org.jivesoftware.database.SequenceManager;
import org.jivesoftware.util.JiveConstants;
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
import org.jivesoftware.util.LongList;
import org.jivesoftware.messenger.XMPPAddress;
import org.jivesoftware.messenger.user.*;
......@@ -26,6 +25,8 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.List;
/**
* <p>Implements the roster item provider against the jiveRoster table
......@@ -189,34 +190,21 @@ public class DbRosterItemProvider implements RosterItemProvider {
}
}
private static final String LOAD_ROSTER_IDS =
"DELETE from jiveRosterGroups WHERE username=?";
private static final String LOAD_USERNAMES =
"SELECT DISTINCT username from jiveRoster WHERE jid=?";
private static final String DELETE_ROSTER =
"DELETE FROM jiveRoster WHERE username=?";
public void deleteItems(String username) throws UnsupportedOperationException {
public Iterator<String> getUsernames(String jid) {
List<String> answer = new ArrayList<String>();
Connection con = null;
PreparedStatement pstmt = null;
try {
LongList list = new LongList();
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_ROSTER_IDS);
pstmt.setString(1, username);
pstmt = con.prepareStatement(LOAD_USERNAMES);
pstmt.setString(1, jid);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
list.add(rs.getLong(1));
answer.add(rs.getString(1));
}
for (int i = 0; i < list.size(); i++) {
pstmt = con.prepareStatement(DELETE_ROSTER_ITEM_GROUPS);
pstmt.setLong(1, list.get(i));
pstmt.executeUpdate();
}
pstmt = con.prepareStatement(DELETE_ROSTER);
pstmt.setString(1, username);
pstmt.executeUpdate();
}
catch (SQLException e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
......@@ -227,6 +215,7 @@ public class DbRosterItemProvider implements RosterItemProvider {
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e); }
}
return answer.iterator();
}
private static final String COUNT_ROSTER_ITEMS =
......
......@@ -14,9 +14,11 @@ package org.jivesoftware.messenger.user.spi;
import org.jivesoftware.util.Cache;
import org.jivesoftware.util.CacheManager;
import org.jivesoftware.messenger.container.BasicModule;
import org.jivesoftware.messenger.user.CachedRoster;
import org.jivesoftware.messenger.user.RosterManager;
import org.jivesoftware.messenger.user.UserNotFoundException;
import org.jivesoftware.messenger.user.*;
import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.messenger.XMPPAddress;
import java.util.Iterator;
public class RosterManagerImpl extends BasicModule implements RosterManager {
......@@ -44,4 +46,44 @@ public class RosterManagerImpl extends BasicModule implements RosterManager {
}
return roster;
}
public void deleteRoster(XMPPAddress user) {
try {
String username = user.getNamePrep();
// Get the roster of the deleted user
Roster roster = (Roster)CacheManager.getCache("username2roster").get(username);
if (roster == null) {
// Not in cache so load a new one:
roster = new CachedRosterImpl(username);
}
// Remove each roster item from the user's roster
Iterator<RosterItem> items = roster.getRosterItems();
while (items.hasNext()) {
roster.deleteRosterItem(items.next().getJid());
}
// Remove the cached roster from memory
CacheManager.getCache("username2roster").remove(username);
// Get the rosters that have a reference to the deleted user
RosterItemProvider rosteItemProvider = UserProviderFactory.getRosterItemProvider();
Iterator<String> usernames = rosteItemProvider.getUsernames(user.toBareStringPrep());
while (usernames.hasNext()) {
username = usernames.next();
// Get the roster that has a reference to the deleted user
roster = (Roster)CacheManager.getCache("username2roster").get(username);
if (roster == null) {
// Not in cache so load a new one:
roster = new CachedRosterImpl(username);
}
// Remove the deleted user reference from this roster
roster.deleteRosterItem(user);
}
}
catch (UnsupportedOperationException e) {
// Do nothing
}
catch (UnauthorizedException e) {
// Do nothing
}
}
}
\ No newline at end of file
......@@ -20,6 +20,7 @@ import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.messenger.user.User;
import org.jivesoftware.messenger.user.UserManager;
import org.jivesoftware.messenger.user.UserNotFoundException;
import org.jivesoftware.messenger.user.RosterManager;
import org.jivesoftware.messenger.XMPPServer;
import org.jivesoftware.messenger.PrivateStore;
import org.jivesoftware.messenger.PresenceManager;
......@@ -96,14 +97,15 @@ public class WebManager extends WebBean {
}
public UserManager getUserManager() {
// The user object of the logged-in user
UserManager userManager = (UserManager)getServiceLookup().lookup(UserManager.class);
return userManager;
return (UserManager)getServiceLookup().lookup(UserManager.class);
}
public RosterManager getRosterManager() {
return (RosterManager)getServiceLookup().lookup(RosterManager.class);
}
public PrivateStore getPrivateStore() {
final PrivateStore privateStore = (PrivateStore)getServiceLookup().lookup(PrivateStore.class);
return privateStore;
return (PrivateStore)getServiceLookup().lookup(PrivateStore.class);
}
public PresenceManager getPresenceManager() {
......
......@@ -8,7 +8,8 @@
<%@ page import="org.jivesoftware.util.*,
org.jivesoftware.messenger.user.*,
org.jivesoftware.admin.*"
org.jivesoftware.admin.*,
org.jivesoftware.messenger.XMPPAddress"
%>
<jsp:useBean id="webManager" class="org.jivesoftware.util.WebManager" />
<% webManager.init(request, response, session, application, out ); %>
......@@ -30,7 +31,11 @@
// Handle a user delete:
if (delete) {
// Delete the user
webManager.getUserManager().deleteUser(user);
// Delete the user's roster
XMPPAddress userAddress = new XMPPAddress(username, webManager.getServerInfo().getName(), null);
webManager.getRosterManager().deleteRoster(userAddress);
// Deleted your own user account, force login
if (username.equals(webManager.getAuthToken().getUsername())){
session.removeAttribute("jive.admin.authToken");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment