KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.*;
39 import cowsultants.itracker.web.forms.*;
40 import cowsultants.itracker.web.util.*;
41
42
43 public class EditUserAction extends ITrackerAction {
44
45     public EditUserAction() {
46     }
47
48     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
49         ActionErrors errors = new ActionErrors();
50
51         if(! isLoggedIn(request, response)) {
52             return mapping.findForward("login");
53         }
54
55         if(! hasPermission(UserUtilities.PERMISSION_USER_ADMIN, request, response)) {
56             return mapping.findForward("unauthorized");
57         }
58
59         if(! isTokenValid(request)) {
60             Logger.logDebug("Invalid request token while editing component.");
61             return mapping.findForward("listusers");
62         }
63         resetToken(request);
64
65         UserForm userForm = (UserForm) form;
66         if(userForm == null) {
67             return mapping.findForward("listusers");
68         }
69
70         HttpSession session = request.getSession(true);
71
72         try {
73             InitialContext ic = new InitialContext();
74
75             Object JavaDoc uhRef = ic.lookup("java:comp/env/" + UserHandler.JNDI_NAME);
76             UserHandlerHome uhHome = (UserHandlerHome) PortableRemoteObject.narrow(uhRef, UserHandlerHome.class);
77             UserHandler uh = uhHome.create();
78
79             UserModel editUser = new UserModel();
80             editUser.setId(userForm.getId());
81             editUser.setLogin(userForm.getLogin());
82             editUser.setFirstName(userForm.getFirstName());
83             editUser.setLastName(userForm.getLastName());
84             editUser.setEmail(userForm.getEmail());
85             editUser.setSuperUser(userForm.isSuperUser());
86
87             try {
88                 if("create".equals(userForm.getAction())) {
89                     if(! uh.allowProfileCreation(editUser, null, UserUtilities.AUTH_TYPE_UNKNOWN, UserUtilities.REQ_SOURCE_WEB)) {
90                         errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.noprofilecreates"));
91                         saveErrors(request, errors);
92                         return mapping.findForward("error");
93                     }
94
95                     Logger.logDebug("Creating new userid.");
96                     editUser.setRegistrationType(UserUtilities.REGISTRATION_TYPE_ADMIN);
97                     if(uh.allowPasswordUpdates(editUser, null, UserUtilities.AUTH_TYPE_UNKNOWN, UserUtilities.REQ_SOURCE_WEB)) {
98                         editUser.setPassword(UserUtilities.encryptPassword(userForm.getPassword()));
99                     }
100                     editUser = uh.createUser(editUser);
101                 } else if ("update".equals(userForm.getAction())) {
102                     UserModel existingUser = uh.getUser(editUser.getId());
103                     if(existingUser != null) {
104                         boolean performUpdate = true;
105                         if(! uh.allowProfileUpdates(existingUser, null, UserUtilities.AUTH_TYPE_UNKNOWN, UserUtilities.REQ_SOURCE_WEB)) {
106                             editUser = existingUser;
107                             performUpdate = false;
108                         }
109                         if(uh.allowPasswordUpdates(existingUser, null, UserUtilities.AUTH_TYPE_UNKNOWN, UserUtilities.REQ_SOURCE_WEB)) {
110                             if(userForm.getPassword() != null && ! userForm.getPassword().equals("")) {
111                                 editUser.setPassword(UserUtilities.encryptPassword(userForm.getPassword()));
112                                 performUpdate = true;
113                             }
114                         }
115                         if(performUpdate) {
116                             editUser = uh.updateUser(editUser);
117                         }
118                     }
119                 } else {
120                     errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.invalidaction"));
121                 }
122             } catch(UserException ue) {
123                 ue.printStackTrace();
124                 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.existinglogin"));
125                 saveErrors(request, errors);
126                 saveToken(request);
127                 return mapping.getInputForward();
128             }
129
130             if(errors.isEmpty() && uh.allowPermissionUpdates(editUser, null, UserUtilities.AUTH_TYPE_UNKNOWN, UserUtilities.REQ_SOURCE_WEB)) {
131                 HashMap permissions = userForm.getPermissions();
132                 Vector permissionsVector = new Vector();
133                 for(Iterator iter = permissions.keySet().iterator(); iter.hasNext(); ) {
134                     String JavaDoc paramName = (String JavaDoc) iter.next();
135                     permissionsVector.addElement(new PermissionModel(new Integer JavaDoc(paramName.substring(paramName.lastIndexOf('j') + 1)),
136                                                                      Integer.parseInt(paramName.substring(4,paramName.lastIndexOf('P')))));
137                 }
138                 PermissionModel[] newPermissions = new PermissionModel[permissionsVector.size()];
139                 permissionsVector.copyInto(newPermissions);
140                 uh.setUserPermissions(editUser.getId(), newPermissions);
141             }
142
143             if(errors.isEmpty()) {
144                 if(SessionManager.getSessionStart(editUser.getLogin()) != null) {
145                     SessionManager.setSessionNeedsReset(editUser.getLogin());
146                 }
147
148                 Logger.logDebug("Forwarding to list users.");
149                 session.removeAttribute(Constants.EDIT_USER_KEY);
150                 return mapping.findForward("listusers");
151             }
152         } catch(Exception JavaDoc e) {
153             e.printStackTrace();
154             Logger.logError("Exception processing form data", e);
155             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.system"));
156         }
157
158         if(! errors.isEmpty()) {
159             saveErrors(request, errors);
160             saveToken(request);
161             return mapping.getInputForward();
162         }
163         session.removeAttribute(Constants.EDIT_USER_KEY);
164         return mapping.findForward("error");
165     }
166
167 }
168   
Popular Tags