001: /* Copyright 2001 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.tools;
007:
008: import java.sql.Connection;
009: import java.sql.DatabaseMetaData;
010: import java.sql.ResultSet;
011: import java.sql.SQLException;
012: import java.sql.Statement;
013:
014: import org.jasig.portal.AuthorizationException;
015: import org.jasig.portal.IUserIdentityStore;
016: import org.jasig.portal.RDBMServices;
017: import org.jasig.portal.RDBMUserIdentityStore;
018: import org.jasig.portal.security.IPerson;
019: import org.jasig.portal.security.PersonFactory;
020: import org.springframework.dao.DataAccessException;
021:
022: /**
023: * Title: Delete Portal User
024: * Description: Deletes all traces of a portal user from the database
025: * Company:
026: * @author Atul Shenoy and Susan Bramhall (modify by Julien Marchal, University Nancy 2)
027: * @version $Revision: 35664 $
028: */
029:
030: public class DeleteUser {
031:
032: public static void main(String[] args) {
033: RDBMServices.setGetDatasourceFromJndi(false); /*don't try jndi when not in web app */
034:
035: if (args.length < 1) {
036: System.err.println("Usage \"DeleteUser <username>\"");
037: return;
038: }
039: int portalUID = -1;
040: IPerson per = PersonFactory.createPerson();
041: per.setAttribute(IPerson.USERNAME, args[0]);
042:
043: IUserIdentityStore rdbmuser = new RDBMUserIdentityStore();
044:
045: try {
046: portalUID = rdbmuser.getPortalUID(per, false);
047: System.out.println("DeleteUser.main(): Got portal UID for "
048: + args[0] + ": " + portalUID);
049: } catch (AuthorizationException e) {
050: System.err
051: .println("DeleteUser.main(): Attempting to get portal UID for "
052: + args[0]
053: + " - Authorization exception: "
054: + e.getMessage());
055: return;
056: }
057:
058: if (portalUID > -1) {
059: try {
060: rdbmuser.removePortalUID(portalUID);
061: System.out.println("DeleteUser.main(): Removed "
062: + portalUID + " from portal db.");
063: }
064:
065: catch (Exception e) {
066: System.err
067: .println("DeleteUser.main(): Attempting to delete user: "
068: + "error removing user from user identity store.");
069: e.printStackTrace();
070: return;
071: }
072: } else {
073: System.err.println("DeleteUser.removePortalUid(): "
074: + "Attempting to delete " + portalUID
075: + "; unable to find user.");
076: return;
077: }
078:
079: // Still left: bookmarks
080: // No current way to notify channels
081: // So directly access the database
082: // This is the wrong way to do this
083: // We need a way for channels to remove
084: // data for a user when the user is deleted
085:
086: try {
087: deleteBookmarks(portalUID);
088: } catch (SQLException e) {
089: System.err
090: .println("DeleteUser.main(): Error deleting bookmarks: "
091: + e.getMessage());
092: }
093: return;
094: }
095:
096: public static void deleteBookmarks(int uid) throws SQLException {
097: DatabaseMetaData metadata = null;
098: Connection con = null;
099: try {
100: con = RDBMServices.getConnection();
101:
102: Statement stmt = null;
103: try {
104: metadata = con.getMetaData();
105: String[] names = { "TABLE" };
106: ResultSet tableNames = null;
107: try {
108: tableNames = metadata.getTables(null, "%",
109: "UPC_BOOKMARKS", names);
110:
111: if (tableNames.next()) {
112: stmt = con.createStatement();
113: System.out
114: .println("Deleting bookmarks from UPC_BOOKMARKS");
115: String bookmarksSql = "DELETE FROM UPC_BOOKMARKS WHERE PORTAL_USER_ID="
116: + uid;
117: stmt.execute(bookmarksSql);
118: }
119: } finally {
120: try {
121: tableNames.close();
122: } catch (Exception e) {
123: }
124: }
125: } finally {
126: try {
127: stmt.close();
128: } catch (Exception e) {
129: }
130: }
131: } catch (DataAccessException dae) {
132: // we know this was thrown by RDBMServices.getConnection().
133: throw new SQLException("DeleteUser.deleteBookmarks(): "
134: + "Unable to get a database connection.");
135: } finally {
136: try {
137: RDBMServices.releaseConnection(con);
138: } catch (Exception e) {
139: }
140: }
141: return;
142: }
143: }
|