KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > web > actions > EditPreferencesAction


1 /*
2  * This software was designed and created by Jason Carroll.
3  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4  * The author can be reached at jcarroll@cowsultants.com
5  * ITracker website: http://www.cowsultants.com
6  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it only under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  */

18
19 package cowsultants.itracker.web.actions;
20
21 import java.io.*;
22 import java.rmi.*;
23 import java.util.*;
24 import javax.ejb.*;
25 import javax.rmi.*;
26 import javax.naming.*;
27 import javax.servlet.*;
28 import javax.servlet.http.*;
29
30 import org.apache.commons.beanutils.*;
31 import org.apache.struts.action.*;
32 import org.apache.struts.upload.*;
33 import org.apache.struts.util.*;
34
35 import cowsultants.itracker.ejb.client.exceptions.*;
36 import cowsultants.itracker.ejb.client.interfaces.*;
37 import cowsultants.itracker.ejb.client.models.*;
38 import cowsultants.itracker.ejb.client.resources.*;
39 import cowsultants.itracker.ejb.client.util.*;
40 import cowsultants.itracker.web.forms.*;
41 import cowsultants.itracker.web.util.*;
42
43
44 /**
45   * This class performas an update of the user's profile information based on their input.
46   * Only the users core profile information, password, and preferences are updated, no permissions
47   * can be updated from here. Also each type of information is only updated, if it is allowed
48   * by the current systems plugable authentication.
49   */

50 public class EditPreferencesAction extends ITrackerAction {
51
52     public EditPreferencesAction() {
53     }
54
55     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
56         Logger.logDebug("Starting pref mod");
57
58         ActionErrors errors = new ActionErrors();
59
60         if(! isLoggedIn(request, response)) {
61             return mapping.findForward("login");
62         }
63         if(! isTokenValid(request)) {
64             Logger.logDebug("Invalid request token while editing user preferences.");
65             return mapping.findForward("index");
66         }
67         resetToken(request);
68
69         UserModel user = null;
70         try {
71             InitialContext ic = new InitialContext();
72
73             Object JavaDoc uhRef = ic.lookup("java:comp/env/" + UserHandler.JNDI_NAME);
74             UserHandlerHome uhHome = (UserHandlerHome) PortableRemoteObject.narrow(uhRef, UserHandlerHome.class);
75             UserHandler uh = uhHome.create();
76
77             HttpSession session = request.getSession();
78             user = (UserModel) session.getAttribute(Constants.USER_KEY);
79             if(user == null) {
80                 return mapping.findForward("login");
81             }
82
83             UserModel existingUser = uh.getUser(user.getId());
84             if(existingUser == null || user.getId().intValue() != existingUser.getId().intValue()) {
85                 Logger.logDebug("Unauthorized edit preferences request from " + user.getLogin() + "(" + user.getId() + ") for " + existingUser.getLogin() + "(" + existingUser.getId() + ")");
86                 return mapping.findForward("unauthorized");
87             }
88             UserForm userForm = (UserForm) form;
89
90             errors = form.validate(mapping, request);
91
92             if(errors.isEmpty()) {
93                 if(uh.allowPasswordUpdates(existingUser, null, UserUtilities.AUTH_TYPE_UNKNOWN, UserUtilities.REQ_SOURCE_WEB)) {
94                     if(userForm.getPassword() != null && userForm.getPassword().trim().length() > 1) {
95                         if(userForm.getCurrPassword() == null || "".equals(userForm.getCurrPassword())) {
96                             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.missingpassword"));
97                         } else {
98                             try {
99                                 UserModel passwordCheck = uh.checkLogin(user.getLogin(), userForm.getCurrPassword(), AuthenticationConstants.AUTH_TYPE_PASSWORD_PLAIN, AuthenticationConstants.REQ_SOURCE_WEB);
100                                 if(passwordCheck == null) {
101                                     throw new AuthenticatorException(AuthenticatorException.INVALID_DATA);
102                                 }
103                                 existingUser.setPassword(UserUtilities.encryptPassword(userForm.getPassword().trim()));
104                             } catch(AuthenticatorException ae) {
105                                 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.wrongpassword"));
106                             }
107                         }
108                     }
109                 }
110
111                 if(uh.allowProfileUpdates(existingUser, null, UserUtilities.AUTH_TYPE_UNKNOWN, UserUtilities.REQ_SOURCE_WEB)) {
112                     existingUser.setFirstName(userForm.getFirstName());
113                     existingUser.setLastName(userForm.getLastName());
114                     existingUser.setEmail(userForm.getEmail());
115                 }
116             }
117
118             if(errors.isEmpty()) {
119                 Logger.logDebug("Passed required checks. Updating user info for " + user.getLogin());
120                 user = uh.updateUser(existingUser);
121
122                 UserPreferencesModel userPrefs = new UserPreferencesModel();
123                 if(uh.allowPreferenceUpdates(existingUser, null, UserUtilities.AUTH_TYPE_UNKNOWN, UserUtilities.REQ_SOURCE_WEB)) {
124                     userPrefs.setUserId(existingUser.getId());
125                     userPrefs.setUserLogin(existingUser.getLogin());
126
127                     userPrefs.setUserLocale(userForm.getUserLocale());
128                     userPrefs.setSaveLogin(("true".equals(userForm.getSaveLogin()) ? true : false));
129                     try {
130                         userPrefs.setNumItemsOnIndex(Integer.parseInt(userForm.getNumItemsOnIndex()));
131                     } catch(NumberFormatException JavaDoc nfe) {
132                         userPrefs.setNumItemsOnIndex(-1);
133                     }
134                     try {
135                         userPrefs.setNumItemsOnIssueList(Integer.parseInt(userForm.getNumItemsOnIssueList()));
136                     } catch(NumberFormatException JavaDoc nfe) {
137                         userPrefs.setNumItemsOnIssueList(-1);
138                     }
139                     userPrefs.setShowClosedOnIssueList(("true".equals(userForm.getShowClosedOnIssueList()) ? true : false));
140                     userPrefs.setSortColumnOnIssueList(userForm.getSortColumnOnIssueList());
141
142                     int hiddenSections = 0;
143                     Integer JavaDoc[] hiddenSectionsArray = userForm.getHiddenIndexSections();
144                     if(hiddenSectionsArray != null) {
145                         for(int i = 0; i < hiddenSectionsArray.length; i++) {
146                             hiddenSections += hiddenSectionsArray[i].intValue();
147                         }
148                     }
149                     userPrefs.setHiddenIndexSections(hiddenSections);
150
151                     userPrefs.setRememberLastSearch(("true".equals(userForm.getRememberLastSearch()) ? true : false));
152
153                     userPrefs = uh.updateUserPreferences(userPrefs);
154                 }
155
156                 session.setAttribute(Constants.USER_KEY, existingUser);
157                 session.setAttribute(Constants.PREFERENCES_KEY, userPrefs);
158                 session.setAttribute(Constants.LOCALE_KEY, ITrackerResources.getLocale(userPrefs.getUserLocale()));
159                 session.removeAttribute(Constants.EDIT_USER_KEY);
160                 session.removeAttribute(Constants.EDIT_USER_PREFS_KEY);
161             }
162         } catch(Exception JavaDoc e) {
163             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.save"));
164         }
165
166         if(! errors.isEmpty()) {
167             saveErrors(request, errors);
168             saveToken(request);
169             return mapping.getInputForward();
170         }
171
172         return mapping.findForward("index");
173     }
174 }
175   
Popular Tags