001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * This file creation date: Apr 19, 2003 / 9:13:16 PM
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.view.admin;
044:
045: import java.util.ArrayList;
046: import java.util.Iterator;
047: import java.util.List;
048:
049: import net.jforum.SessionFacade;
050: import net.jforum.dao.DataAccessDriver;
051: import net.jforum.dao.GroupDAO;
052: import net.jforum.dao.UserDAO;
053: import net.jforum.entities.Group;
054: import net.jforum.entities.User;
055: import net.jforum.repository.SecurityRepository;
056: import net.jforum.util.I18n;
057: import net.jforum.util.TreeGroup;
058: import net.jforum.util.preferences.ConfigKeys;
059: import net.jforum.util.preferences.SystemGlobals;
060: import net.jforum.util.preferences.TemplateKeys;
061: import net.jforum.view.forum.common.UserCommon;
062: import net.jforum.view.forum.common.ViewCommon;
063:
064: /**
065: * @author Rafael Steil
066: * @version $Id: UserAction.java,v 1.35 2007/09/21 17:29:30 rafaelsteil Exp $
067: */
068: public class UserAction extends AdminCommand {
069: public void list() {
070: int start = this .preparePagination(DataAccessDriver
071: .getInstance().newUserDAO().getTotalUsers());
072: int usersPerPage = SystemGlobals
073: .getIntValue(ConfigKeys.USERS_PER_PAGE);
074:
075: this .context.put("users", DataAccessDriver.getInstance()
076: .newUserDAO().selectAll(start, usersPerPage));
077: this .commonData();
078: }
079:
080: public void pendingActivations() {
081: UserDAO dao = DataAccessDriver.getInstance().newUserDAO();
082: List users = dao.pendingActivations();
083:
084: this
085: .setTemplateName(TemplateKeys.USER_ADMIN_PENDING_ACTIVATIONS);
086: this .context.put("users", users);
087: }
088:
089: public void activateAccount() {
090: String[] ids = this .request.getParameterValues("user_id");
091:
092: if (ids != null) {
093: UserDAO dao = DataAccessDriver.getInstance().newUserDAO();
094:
095: for (int i = 0; i < ids.length; i++) {
096: int userId = Integer.parseInt(ids[i]);
097: dao.writeUserActive(userId);
098: }
099: }
100:
101: this .pendingActivations();
102: }
103:
104: private int preparePagination(int totalUsers) {
105: int start = ViewCommon.getStartPage();
106: int usersPerPage = SystemGlobals
107: .getIntValue(ConfigKeys.USERS_PER_PAGE);
108:
109: ViewCommon.contextToPagination(start, totalUsers, usersPerPage);
110:
111: return start;
112: }
113:
114: private void commonData() {
115: this .context.put("selectedList", new ArrayList());
116: this .context.put("groups", new TreeGroup().getNodes());
117: this .setTemplateName(TemplateKeys.USER_ADMIN_COMMON);
118: this .context.put("searchAction", "list");
119: this .context.put("searchId", new Integer(-1));
120: }
121:
122: public void groupSearch() {
123: final int groupId = this .request.getIntParameter("group_id");
124: if (groupId == 0) {
125: this .list();
126: return;
127: }
128:
129: UserDAO um = DataAccessDriver.getInstance().newUserDAO();
130:
131: int start = this .preparePagination(um
132: .getTotalUsersByGroup(groupId));
133: int usersPerPage = SystemGlobals
134: .getIntValue(ConfigKeys.USERS_PER_PAGE);
135:
136: this .commonData();
137:
138: List l = new ArrayList();
139: l.add(new Integer(groupId));
140:
141: this .context.put("selectedList", l);
142: this .context.put("searchAction", "groupSearch");
143: this .context.put("users", um.selectAllByGroup(groupId, start,
144: usersPerPage));
145: this .context.put("searchId", new Integer(groupId));
146: }
147:
148: public void search() {
149: String search = this .request.getParameter("username");
150: String group = this .request.getParameter("group");
151:
152: if (search != null && !"".equals(search)) {
153: List users;
154: users = DataAccessDriver.getInstance().newUserDAO()
155: .findByName(search, false);
156:
157: this .commonData();
158:
159: this .context.put("users", users);
160: this .context.put("search", search);
161: this .context.put("start", new Integer(1));
162: } else if (!"0".equals(group)) {
163: this .groupSearch();
164: } else {
165: this .list();
166: }
167: }
168:
169: public void edit() {
170: int userId = this .request.getIntParameter("id");
171: UserDAO um = DataAccessDriver.getInstance().newUserDAO();
172: User u = um.selectById(userId);
173:
174: this .setTemplateName(TemplateKeys.USER_ADMIN_EDIT);
175: this .context.put("u", u);
176: this .context.put("action", "editSave");
177: this .context.put("specialRanks", DataAccessDriver.getInstance()
178: .newRankingDAO().selectSpecials());
179: this .context.put("avatarAllowExternalUrl", SystemGlobals
180: .getBoolValue(ConfigKeys.AVATAR_ALLOW_EXTERNAL_URL));
181: this .context.put("admin", true);
182: }
183:
184: public void editSave() {
185: int userId = this .request.getIntParameter("user_id");
186: UserCommon.saveUser(userId);
187:
188: this .list();
189: }
190:
191: // Delete
192: public void delete() {
193: String ids[] = this .request.getParameterValues("user_id");
194: UserDAO um = DataAccessDriver.getInstance().newUserDAO();
195:
196: if (ids != null) {
197: for (int i = 0; i < ids.length; i++) {
198:
199: int user = Integer.parseInt(ids[i]);
200:
201: if (um.isDeleted(user)) {
202: um.undelete(user);
203: } else {
204: String sessionId = SessionFacade
205: .isUserInSession(user);
206:
207: if (sessionId != null) {
208: SessionFacade.remove(sessionId);
209: }
210:
211: um.delete(user);
212: }
213: }
214: }
215:
216: this .list();
217: }
218:
219: // Groups
220: public void groups() {
221: int userId = this .request.getIntParameter("id");
222:
223: UserDAO um = DataAccessDriver.getInstance().newUserDAO();
224: User u = um.selectById(userId);
225:
226: List selectedList = new ArrayList();
227: for (Iterator iter = u.getGroupsList().iterator(); iter
228: .hasNext();) {
229: selectedList
230: .add(new Integer(((Group) iter.next()).getId()));
231: }
232:
233: this .context.put("selectedList", selectedList);
234: this .context.put("groups", new TreeGroup().getNodes());
235: this .context.put("user", u);
236: this .context.put("userId", new Integer(userId));
237: this .setTemplateName(TemplateKeys.USER_ADMIN_GROUPS);
238: this .context.put("groupFor", I18n.getMessage("User.GroupsFor",
239: new String[] { u.getUsername() }));
240: }
241:
242: // Groups Save
243: public void groupsSave() {
244: int userId = this .request.getIntParameter("user_id");
245:
246: UserDAO um = DataAccessDriver.getInstance().newUserDAO();
247: GroupDAO gm = DataAccessDriver.getInstance().newGroupDAO();
248:
249: // Remove the old groups
250: List allGroupsList = gm.selectAll();
251: int[] allGroups = new int[allGroupsList.size()];
252:
253: int counter = 0;
254: for (Iterator iter = allGroupsList.iterator(); iter.hasNext(); counter++) {
255: Group g = (Group) iter.next();
256:
257: allGroups[counter] = g.getId();
258: }
259:
260: um.removeFromGroup(userId, allGroups);
261:
262: // Associate the user to the selected groups
263: String[] selectedGroups = this .request
264: .getParameterValues("groups");
265:
266: if (selectedGroups == null) {
267: selectedGroups = new String[0];
268: }
269:
270: int[] newGroups = new int[selectedGroups.length];
271:
272: for (int i = 0; i < selectedGroups.length; i++) {
273: newGroups[i] = Integer.parseInt(selectedGroups[i]);
274: }
275:
276: um.addToGroup(userId, newGroups);
277: SecurityRepository.remove(userId);
278:
279: this.list();
280: }
281: }
|