KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > website > actions > UserNewAction


1
2 package org.roller.presentation.website.actions;
3
4 import org.apache.commons.logging.Log;
5 import org.apache.commons.logging.LogFactory;
6 import org.apache.struts.action.ActionError;
7 import org.apache.struts.action.ActionErrors;
8 import org.apache.struts.action.ActionForm;
9 import org.apache.struts.action.ActionForward;
10 import org.apache.struts.action.ActionMapping;
11 import org.apache.struts.action.ActionMessage;
12 import org.apache.struts.action.ActionMessages;
13 import org.roller.RollerException;
14 import org.roller.model.UserManager;
15 import org.roller.pojos.UserData;
16 import org.roller.presentation.MainPageAction;
17 import org.roller.presentation.RollerContext;
18 import org.roller.presentation.RollerRequest;
19 import org.roller.presentation.pagecache.PageCacheFilter;
20 import org.roller.presentation.website.formbeans.UserFormEx;
21 import org.roller.util.StringUtils;
22
23 import java.io.IOException JavaDoc;
24 import java.util.HashMap JavaDoc;
25
26 import javax.servlet.ServletContext JavaDoc;
27 import javax.servlet.ServletException JavaDoc;
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletResponse JavaDoc;
30
31 import org.roller.config.RollerRuntimeConfig;
32
33 /////////////////////////////////////////////////////////////////////////////
34
/**
35  * Actions for creating a new user.
36  *
37  * @struts.action name="userFormEx" path="/user"
38  * scope="session" parameter="method"
39  *
40  * @struts.action-forward name="registerUser.page" path="/website/UserNew.jsp"
41  * @struts.action-forward name="welcome.page" path="/website/welcome.jsp"
42  */

43 public class UserNewAction extends UserBaseAction
44 {
45     private static Log mLogger =
46         LogFactory.getFactory().getInstance(UserNewAction.class);
47
48     //------------------------------------------------------------------------
49
/** Process GET of new user page (allows admin to create a user) */
50     public ActionForward createUser(
51         ActionMapping mapping,
52         ActionForm actionForm,
53         HttpServletRequest JavaDoc request,
54         HttpServletResponse JavaDoc response)
55         throws IOException JavaDoc, ServletException JavaDoc
56     {
57         UserFormEx userForm = (UserFormEx)actionForm;
58         userForm.setAdminCreated(true);
59         return registerUser(mapping, actionForm, request, response);
60     }
61     
62     //------------------------------------------------------------------------
63
/** Process GET of user registration page (allows users to register themselves. */
64     public ActionForward registerUser(
65         ActionMapping mapping,
66         ActionForm actionForm,
67         HttpServletRequest JavaDoc request,
68         HttpServletResponse JavaDoc response)
69         throws IOException JavaDoc, ServletException JavaDoc
70     {
71         ActionForward forward = mapping.findForward("registerUser.page");
72         ActionErrors errors = new ActionErrors();
73         RollerRequest rreq = RollerRequest.getRollerRequest(request);
74         try
75         {
76             UserFormEx userForm = (UserFormEx)actionForm;
77             loadRequestObjects(request, rreq, null, userForm);
78             userForm.setLocale(request.getLocale().toString());
79                 
80             // User must set new password twice
81
userForm.setPasswordText(null);
82             userForm.setPasswordConfirm(null);
83         }
84         catch (Exception JavaDoc e)
85         {
86             errors.add(ActionErrors.GLOBAL_ERROR,
87                 new ActionError("error.editing.user", e.toString()));
88             mLogger.error("ERROR in newUser", e);
89         }
90         return forward;
91     }
92
93     //-----------------------------------------------------------------------
94
/** Process POST of new user information. */
95     public ActionForward add(
96         ActionMapping mapping,
97         ActionForm actionForm,
98         HttpServletRequest JavaDoc request,
99         HttpServletResponse JavaDoc response)
100         throws IOException JavaDoc, ServletException JavaDoc
101     {
102         UserFormEx form = (UserFormEx)actionForm;
103         RollerRequest rreq = RollerRequest.getRollerRequest(request);
104         ServletContext JavaDoc ctx = rreq.getServletContext();
105         RollerContext rollerContext = RollerContext.getRollerContext(ctx);
106
107         boolean reg_allowed =
108                 RollerRuntimeConfig.getBooleanProperty("users.registration.enabled");
109
110         if ( !reg_allowed && !request.isUserInRole("admin"))
111         {
112             throw new ServletException JavaDoc("New users disabled!");
113         }
114
115         ActionMessages msgs = new ActionMessages();
116         ActionMessages errors = validate(form, new ActionErrors());
117         if (!errors.isEmpty())
118         {
119             saveErrors(request, errors);
120         }
121         else try
122         {
123             // Add new user
124
UserManager mgr = rreq.getRoller().getUserManager();
125             
126             // Need system user to add new user
127
rreq.getRoller().setUser(UserData.SYSTEM_USER);
128
129             UserData ud = new UserData();
130             form.copyTo(ud, request.getLocale()); // doesn't copy password
131
ud.setId(null);
132             ud.setDateCreated(new java.util.Date JavaDoc());
133
134             // If user set both password and passwordConfirm then reset password
135
if ( !StringUtils.isEmpty(form.getPasswordText())
136                  && !StringUtils.isEmpty(form.getPasswordConfirm()))
137             {
138                ud.resetPassword(rreq.getRoller(),
139                   form.getPasswordText(), form.getPasswordConfirm());
140             }
141             
142             String JavaDoc theme = form.getTheme();
143             HashMap JavaDoc pages = rollerContext.readThemeMacros(theme);
144             mgr.addUser( ud, pages, theme, form.getLocale(), form.getTimezone() );
145             rreq.getRoller().commit();
146
147             // Flush cache so user will immediately appear on index page
148
PageCacheFilter.removeFromCache( request, ud );
149             MainPageAction.flushMainPageCache();
150
151             if (form.getAdminCreated())
152             {
153                 // User created for admin, so return to new user page with empty form
154
msgs.add(ActionMessages.GLOBAL_MESSAGE,
155                     new ActionMessage("newUser.created"));
156                 saveMessages(request, msgs);
157                 form.reset(mapping, request);
158                 return createUser(mapping, actionForm, request, response);
159             }
160             else
161             {
162                 // User registered, so go to welcome page
163
String JavaDoc weblogURL = rollerContext.getAbsoluteContextUrl(request)
164                                  + "/page/"+ud.getUserName();
165                 request.setAttribute("weblogURL",weblogURL);
166                 String JavaDoc rssURL = rollerContext.getAbsoluteContextUrl(request)
167                               + "/rss/"+ud.getUserName();
168                 request.setAttribute("rssURL",rssURL);
169                 request.setAttribute("contextURL",
170                                  rollerContext.getAbsoluteContextUrl(request));
171                 return mapping.findForward("welcome.page");
172             }
173         }
174         catch (RollerException e)
175         {
176             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(e.getMessage()));
177             saveErrors(request,errors);
178             mLogger.error("ERROR in addUser", e);
179             form.setUserName("");
180         }
181
182         if (form.getAdminCreated())
183         {
184             return mapping.findForward("createUser");
185         }
186         else
187         {
188             // Error occured, send user back to new user form
189
return mapping.findForward("registerUser");
190         }
191     }
192
193     //------------------------------------------------------------------------
194
/** Validate user form. TODO: replace with Struts validation. */
195     protected ActionMessages validate( UserFormEx form, ActionMessages errors ) {
196         super.validate(form, errors);
197         if ( StringUtils.isEmpty(form.getPasswordText())
198              && StringUtils.isEmpty(form.getPasswordConfirm()))
199         {
200             errors.add( ActionErrors.GLOBAL_ERROR,
201                new ActionError("error.add.user.missingPassword"));
202         }
203         return errors;
204     }
205 }
Popular Tags