1 6 7 package org.contineo.actions.admin.user; 8 9 import java.util.Collection ; 10 import java.util.Iterator ; 11 12 import javax.servlet.http.HttpServletRequest ; 13 import javax.servlet.http.HttpServletResponse ; 14 import javax.servlet.http.HttpSession ; 15 16 import org.apache.log4j.Level; 17 import org.apache.log4j.Logger; 18 import org.apache.struts.action.Action; 19 import org.apache.struts.action.ActionError; 20 import org.apache.struts.action.ActionErrors; 21 import org.apache.struts.action.ActionForm; 22 import org.apache.struts.action.ActionForward; 23 import org.apache.struts.action.ActionMapping; 24 import org.apache.struts.action.ActionMessage; 25 import org.apache.struts.action.ActionMessages; 26 import org.contineo.admin.User; 27 import org.contineo.admin.UserDoc; 28 import org.contineo.admin.dao.MenuDAO; 29 import org.contineo.admin.dao.UserDAO; 30 import org.contineo.admin.dao.UserDocDAO; 31 import org.contineo.communication.EMail; 32 import org.contineo.communication.dao.EMailAccountDAO; 33 import org.contineo.communication.dao.EMailDAO; 34 import org.contineo.core.FileBean; 35 import org.contineo.core.LoggingManager; 36 import org.contineo.core.SessionManagement; 37 import org.contineo.core.config.SettingConfigurator; 38 import org.contineo.documan.History; 39 import org.contineo.documan.dao.HistoryDAO; 40 41 42 46 public class DeleteUserAction extends Action { 47 48 52 private Logger logger; 53 54 55 56 public DeleteUserAction() { 57 logger = LoggingManager.getLogger(this.getClass()); 58 } 59 60 public ActionForward execute(ActionMapping mapping, 61 ActionForm form, HttpServletRequest request, 62 HttpServletResponse response) { 63 ActionForward actionForward = new ActionForward(); 64 ActionErrors errors = new ActionErrors(); 65 ActionMessages messages = new ActionMessages(); 66 HttpSession session = request.getSession(); 67 if (SessionManagement.isValid(session)) { 68 try { 69 MenuDAO mdao = new MenuDAO(); 70 String uname = (String )session.getAttribute("authuser"); 71 String username = request.getParameter("username"); 72 UserDAO dao = new UserDAO(); 73 74 if (mdao.isReadEnable(10, uname)) { 76 77 User toBeDeletedUser = dao.findByPrimaryKey(username); 79 boolean isAdmin = false; 80 if (toBeDeletedUser != null) { 81 toBeDeletedUser.initGroups(); 82 String [] userGroups = toBeDeletedUser.getGroups(); 83 if (userGroups != null) { 84 for (int i = 0; i < userGroups.length; i++) { 85 if (userGroups[i].equals("admin")) { 86 isAdmin = true; 87 break; 88 } 89 } 90 } 91 } 92 93 int adminsFound = 0; 96 if (isAdmin) { 97 Collection allUsers = dao.findAll(); 98 Iterator userIter = allUsers.iterator(); while (userIter.hasNext()) { 100 User currUser = (User) userIter.next(); 101 currUser.initGroups(); String [] groups = currUser.getGroups(); 103 if (groups != null) { 104 for (int i = 0; i < groups.length; i++) { 105 if (groups[i].equals("admin")) { 106 adminsFound++; 107 break; } 109 } 110 } 111 if (adminsFound > 2) 114 break; 115 } 116 } 117 118 if (!isAdmin || (isAdmin && adminsFound > 1)) { 120 EMailDAO emailDao = new EMailDAO(); 122 Collection coll = emailDao.findByUserName(username); 123 Iterator iter = coll.iterator(); 124 while (iter.hasNext()) { 125 EMail email = (EMail) iter.next(); 126 emailDao.delete(email.getMessageId()); 127 } 128 EMailAccountDAO accountDao = new EMailAccountDAO(); 129 accountDao.deleteByUsername(username); 130 131 UserDocDAO userDocDao = new UserDocDAO(); 133 Collection userDocColl = userDocDao.findByUserName(username); 134 Iterator userDocIter = userDocColl.iterator(); 135 while (userDocIter.hasNext()) { 136 UserDoc userDoc = (UserDoc) userDocIter.next(); 137 userDocDao.delete(username, userDoc.getMenuId()); 138 } 139 140 HistoryDAO historyDAO = new HistoryDAO(); 142 Collection historyColl = historyDAO.findByUsername(username); 143 Iterator historyIter = historyColl.iterator(); 144 while (historyIter.hasNext()) { 145 History history = (History) historyIter.next(); 146 historyDAO.delete(history.getHistoryId()); 147 } 148 149 boolean deleted = dao.delete(username); 150 if (!deleted) { 151 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("errors.action.deleteuser")); 152 } else { 153 messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("msg.action.deleteuser")); 154 SettingConfigurator conf = new SettingConfigurator(); 155 String userdir = conf.getValue("userdir") + "/" + username; 156 FileBean.deleteDir(userdir); 157 } 158 } else if (isAdmin && adminsFound < 2) { 159 messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("msg.action.deleteuser.admingroup")); 160 } 161 actionForward = mapping.findForward("showuser"); 162 } else 163 actionForward = mapping.findForward("noaccess"); 164 } catch (Exception e) { 165 if (logger.isEnabledFor(Level.ERROR)) 166 logger.error(e.getMessage()); 167 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("errors.action.deleteuser")); 168 actionForward = mapping.findForward("error"); 169 } 170 saveErrors(request, errors); 171 saveMessages(request, messages); 172 } else 173 actionForward = mapping.findForward("invalid"); 174 return actionForward; 175 } 176 } | Popular Tags |