View Javadoc

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 org.itracker.web.actions.user;
20  
21  import java.io.IOException;
22  
23  import javax.servlet.ServletException;
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.apache.log4j.Logger;
28  import org.apache.struts.action.ActionForm;
29  import org.apache.struts.action.ActionForward;
30  import org.apache.struts.action.ActionMapping;
31  import org.apache.struts.action.ActionMessage;
32  import org.apache.struts.action.ActionMessages;
33  import org.itracker.model.Notification;
34  import org.itracker.model.User;
35  import org.itracker.model.Notification.Role;
36  import org.itracker.model.Notification.Type;
37  import org.itracker.model.UserPreferences;
38  import org.itracker.services.ConfigurationService;
39  import org.itracker.services.ITrackerServices;
40  import org.itracker.services.UserService;
41  import org.itracker.services.exceptions.UserException;
42  import org.itracker.services.util.AuthenticationConstants;
43  import org.itracker.services.util.UserUtilities;
44  import org.itracker.web.actions.base.ItrackerBaseAction;
45  import org.itracker.web.forms.UserForm;
46  import org.itracker.web.util.LoginUtilities;
47  import org.itracker.web.util.RequestHelper;
48  
49  public class SelfRegisterAction extends ItrackerBaseAction {
50  	private static final Logger log = Logger
51  			.getLogger(SelfRegisterAction.class);
52  
53  
54  
55  	public ActionForward execute(ActionMapping mapping, ActionForm form,
56  			HttpServletRequest request, HttpServletResponse response)
57  			throws ServletException, IOException {
58  
59  		ActionMessages errors = new ActionMessages();
60  
61  		resetToken(request);
62  
63  		try {
64  //			itracker.web.selfreg.title
65  
66  //			String pageTitleKey = "itracker.web.selfreg.title";
67  //			String pageTitleArg = "";
68  //			request.setAttribute("pageTitleKey", pageTitleKey);
69  //			request.setAttribute("pageTitleArg", pageTitleArg);
70  //			
71  			ConfigurationService configurationService = getITrackerServices()
72  					.getConfigurationService();
73  
74  			boolean allowSelfRegister = configurationService
75  					.getBooleanProperty("allow_self_register", false);
76  
77  			if (!allowSelfRegister) {
78  				errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
79  						"itracker.web.error.notenabled"));
80  			} else {
81  				UserForm regForm = (UserForm) form;
82  
83  				User user = new User(regForm.getLogin(), UserUtilities
84  						.encryptPassword(regForm.getPassword()), regForm
85  						.getFirstName(), regForm.getLastName(), regForm
86  						.getEmail(), UserUtilities.REGISTRATION_TYPE_SELF,
87  						false);
88  
89  				if (!user.hasRequiredData()) {
90  					errors.add(ActionMessages.GLOBAL_MESSAGE,
91  							new ActionMessage(
92  									"itracker.web.error.missingfields"));
93  				} else {
94                      UserService userService = getITrackerServices().getUserService();
95  
96  
97  					try {
98  						if (userService
99  								.allowRegistration(
100 										user,
101 										regForm.getPassword(),
102 										AuthenticationConstants.AUTH_TYPE_PASSWORD_PLAIN,
103 										AuthenticationConstants.REQ_SOURCE_WEB)) {
104 							user = userService.createUser(user);
105 
106                             // TODO: remove this hack, this should be handled central, there are other
107                             // instances of this hack
108                             UserPreferences userPrefs = user.getPreferences();
109                             if (userPrefs == null) {
110                                 userPrefs = new UserPreferences();
111                                 user.setPreferences(userPrefs);
112                                 userPrefs.setUser(user);
113                             }
114                             user.getPreferences().setUserLocale( String.valueOf(LoginUtilities.getCurrentLocale(request)) );
115 
116 
117 
118 							Notification notification = new Notification();
119 							notification.setUser(user);
120 							notification.setRole(Role.ANY);
121 							getITrackerServices().getNotificationService()
122 									.sendNotification(notification,
123 											Type.SELF_REGISTER,
124 											getBaseURL(request));
125 						} else {
126 							errors
127 									.add(
128 											ActionMessages.GLOBAL_MESSAGE,
129 											new ActionMessage(
130 													"itracker.web.error.register.unauthorized"));
131 						}
132 					} catch (UserException ue) {
133 						errors.add(ActionMessages.GLOBAL_MESSAGE,
134 								new ActionMessage(
135 										"itracker.web.error.existinglogin"));
136 					}
137 				}
138 			}
139 		} catch (Exception e) {
140 			log.info("Error during self registration.  " + e.getMessage());
141 			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
142 					"itracker.web.error.register.system"));
143 		}
144 
145 		if (!errors.isEmpty()) {
146 			saveErrors(request, errors);
147 			saveToken(request);
148 			return mapping.getInputForward();
149 		}
150 
151 		return mapping.findForward("login");
152 	}
153 
154 }