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