1 43 package net.jforum.view.admin; 44 45 import java.util.ArrayList ; 46 import java.util.Iterator ; 47 import java.util.List ; 48 49 import net.jforum.SessionFacade; 50 import net.jforum.dao.DataAccessDriver; 51 import net.jforum.dao.GroupDAO; 52 import net.jforum.dao.UserDAO; 53 import net.jforum.entities.Group; 54 import net.jforum.entities.User; 55 import net.jforum.repository.SecurityRepository; 56 import net.jforum.util.I18n; 57 import net.jforum.util.TreeGroup; 58 import net.jforum.util.preferences.ConfigKeys; 59 import net.jforum.util.preferences.SystemGlobals; 60 import net.jforum.util.preferences.TemplateKeys; 61 import net.jforum.view.forum.common.UserCommon; 62 import net.jforum.view.forum.common.ViewCommon; 63 64 68 public class UserAction extends AdminCommand 69 { 70 public void list() throws Exception 72 { 73 int start = this.preparePagination(DataAccessDriver.getInstance().newUserDAO().getTotalUsers()); 74 int usersPerPage = SystemGlobals.getIntValue(ConfigKeys.USERS_PER_PAGE); 75 76 this.context.put("users", DataAccessDriver.getInstance().newUserDAO().selectAll(start ,usersPerPage)); 77 this.commonData(); 78 } 79 80 private int preparePagination(int totalUsers) 81 { 82 int start = ViewCommon.getStartPage(); 83 int usersPerPage = SystemGlobals.getIntValue(ConfigKeys.USERS_PER_PAGE); 84 85 ViewCommon.contextToPagination(start, totalUsers, usersPerPage); 86 87 return start; 88 } 89 90 private void commonData() throws Exception 91 { 92 this.context.put("selectedList", new ArrayList ()); 93 this.context.put("groups", new TreeGroup().getNodes()); 94 this.setTemplateName(TemplateKeys.USER_ADMIN_COMMON); 95 this.context.put("searchAction", "list"); 96 this.context.put("searchId", new Integer (-1)); 97 } 98 99 public void groupSearch() throws Exception 100 { 101 final int groupId = this.request.getIntParameter("group_id"); 102 if (groupId == 0) { 103 this.list(); 104 return; 105 } 106 107 UserDAO um = DataAccessDriver.getInstance().newUserDAO(); 108 109 int start = this.preparePagination(um.getTotalUsersByGroup(groupId)); 110 int usersPerPage = SystemGlobals.getIntValue(ConfigKeys.USERS_PER_PAGE); 111 112 this.commonData(); 113 114 List l = new ArrayList (); 115 l.add(new Integer (groupId)); 116 117 this.context.put("selectedList", l); 118 this.context.put("searchAction", "groupSearch"); 119 this.context.put("users", um.selectAllByGroup(groupId, start, usersPerPage)); 120 this.context.put("searchId", new Integer (groupId)); 121 } 122 123 public void search() throws Exception 124 { 125 List users = new ArrayList (); 126 String search = this.request.getParameter("username"); 127 String group = this.request.getParameter("group"); 128 129 if (search != null && !"".equals(search)) { 130 users = DataAccessDriver.getInstance().newUserDAO().findByName(search, false); 131 132 this.commonData(); 133 134 this.context.put("users", users); 135 this.context.put("search", search); 136 this.context.put("start", new Integer (1)); 137 } 138 else if (!"0".equals(group)) { 139 this.groupSearch(); 140 return; 141 } 142 else { 143 this.list(); 144 return; 145 } 146 } 147 148 public void edit() throws Exception 149 { 150 int userId = this.request.getIntParameter("id"); 151 UserDAO um = DataAccessDriver.getInstance().newUserDAO(); 152 User u = um.selectById(userId); 153 154 this.context.put("u", u); 155 this.context.put("action", "editSave"); 156 this.setTemplateName(TemplateKeys.USER_ADMIN_EDIT); 157 this.context.put("admin", true); 158 } 159 160 public void editSave() throws Exception 161 { 162 int userId = this.request.getIntParameter("user_id"); 163 UserCommon.saveUser(userId); 164 165 this.list(); 166 } 167 168 public void delete() throws Exception 170 { 171 String ids[] = this.request.getParameterValues("user_id"); 172 UserDAO um = DataAccessDriver.getInstance().newUserDAO(); 173 174 if (ids != null) { 175 for (int i = 0; i < ids.length; i++) { 176 177 int user = Integer.parseInt(ids[i]); 178 179 if (um.isDeleted(user)){ 180 um.undelete(user); 181 } 182 else { 183 String sessionId = SessionFacade.isUserInSession(user); 184 185 if (sessionId != null) { 186 SessionFacade.remove(sessionId); 187 } 188 189 um.delete(user); 190 } 191 } 192 } 193 194 this.list(); 195 } 196 197 public void groups() throws Exception 199 { 200 int userId = this.request.getIntParameter("id"); 201 202 UserDAO um = DataAccessDriver.getInstance().newUserDAO(); 203 User u = um.selectById(userId); 204 205 List selectedList = new ArrayList (); 206 for (Iterator iter = u.getGroupsList().iterator(); iter.hasNext(); ) { 207 selectedList.add(new Integer (((Group)iter.next()).getId())); 208 } 209 210 this.context.put("selectedList", selectedList); 211 this.context.put("groups", new TreeGroup().getNodes()); 212 this.context.put("user", u); 213 this.context.put("userId", new Integer (userId)); 214 this.setTemplateName(TemplateKeys.USER_ADMIN_GROUPS); 215 this.context.put("groupFor", I18n.getMessage("User.GroupsFor", new String [] { u.getUsername() })); 216 } 217 218 public void groupsSave() throws Exception 220 { 221 int userId = this.request.getIntParameter("user_id"); 222 223 UserDAO um = DataAccessDriver.getInstance().newUserDAO(); 224 GroupDAO gm = DataAccessDriver.getInstance().newGroupDAO(); 225 226 List allGroupsList = gm.selectAll(); 228 int[] allGroups = new int[allGroupsList.size()]; 229 230 int counter = 0; 231 for (Iterator iter = allGroupsList.iterator(); iter.hasNext(); counter++) { 232 Group g = (Group)iter.next(); 233 234 allGroups[counter] = g.getId(); 235 } 236 237 um.removeFromGroup(userId, allGroups); 238 239 String [] selectedGroups = this.request.getParameterValues("groups"); 241 242 if(selectedGroups == null) { 243 selectedGroups = new String [0]; 244 } 245 246 int[] newGroups = new int[selectedGroups.length]; 247 248 for (int i = 0; i < selectedGroups.length; i++) { 249 newGroups[i] = Integer.parseInt(selectedGroups[i]); 250 } 251 252 um.addToGroup(userId, newGroups); 253 SecurityRepository.remove(userId); 254 255 this.list(); 256 } 257 } 258 | Popular Tags |