KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > core > struts > actions > UserNewAction


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18
19 package org.apache.roller.ui.core.struts.actions;
20
21 import java.io.IOException JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Locale JavaDoc;
24 import java.util.TimeZone 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.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.apache.struts.action.ActionError;
34 import org.apache.struts.action.ActionErrors;
35 import org.apache.struts.action.ActionForm;
36 import org.apache.struts.action.ActionForward;
37 import org.apache.struts.action.ActionMapping;
38 import org.apache.struts.action.ActionMessage;
39 import org.apache.struts.action.ActionMessages;
40 import org.apache.roller.RollerException;
41 import org.apache.roller.config.RollerConfig;
42 import org.apache.roller.config.RollerRuntimeConfig;
43 import org.apache.roller.model.RollerFactory;
44 import org.apache.roller.model.UserManager;
45 import org.apache.roller.pojos.UserData;
46 import org.apache.roller.ui.core.BasePageModel;
47 import org.apache.roller.ui.core.RollerContext;
48 import org.apache.roller.ui.core.RollerRequest;
49 import org.apache.roller.ui.core.security.CustomUserRegistry;
50 import org.apache.roller.ui.authoring.struts.formbeans.UserFormEx;
51 import org.apache.commons.lang.StringUtils;
52
53
54 /**
55  * Actions for creating a new user.
56  *
57  * @struts.action name="userFormEx" path="/roller-ui/user"
58  * scope="session" parameter="method"
59  *
60  * @struts.action-forward name="registerUser.page" path=".UserNew"
61  * @struts.action-forward name="welcome.page" path=".welcome"
62  */

63 public class UserNewAction extends UserBaseAction {
64     
65     private static Log mLogger = LogFactory.getLog(UserNewAction.class);
66     
67     
68     /** Process GET of new user page (allows admin to create a user) */
69     public ActionForward createUser(
70             ActionMapping mapping,
71             ActionForm actionForm,
72             HttpServletRequest JavaDoc request,
73             HttpServletResponse JavaDoc response)
74             throws IOException JavaDoc, ServletException JavaDoc {
75         UserFormEx userForm = (UserFormEx)actionForm;
76         userForm.setAdminCreated(true);
77         return registerUser(mapping, actionForm, request, response);
78     }
79     
80     
81     public ActionForward cancel(
82             ActionMapping mapping,
83             ActionForm actionForm,
84             HttpServletRequest JavaDoc request,
85             HttpServletResponse JavaDoc response)
86             throws Exception JavaDoc {
87         return mapping.findForward("main");
88     }
89     
90     
91     /** Process GET of user registration page (allows users to register themselves). */
92     public ActionForward registerUser(
93             ActionMapping mapping,
94             ActionForm actionForm,
95             HttpServletRequest JavaDoc request,
96             HttpServletResponse JavaDoc response)
97             throws IOException JavaDoc, ServletException JavaDoc {
98         ActionForward forward = mapping.findForward("registerUser.page");
99         ActionErrors errors = new ActionErrors();
100         RollerRequest rreq = RollerRequest.getRollerRequest(request);
101         try {
102             UserFormEx userForm = (UserFormEx)actionForm;
103             
104             userForm.setLocale(Locale.getDefault().toString());
105             userForm.setTimeZone(TimeZone.getDefault().getID());
106             userForm.setDataFromSSO(false);
107             
108             // Let's see if there's any user-authentication available from Acegi
109
// and retrieve custom user data to pre-populate form.
110
boolean usingSSO = RollerConfig.getBooleanProperty("users.sso.enabled");
111             if(usingSSO) {
112               UserData fromSSO = CustomUserRegistry.getUserDetailsFromAuthentication();
113               if(fromSSO != null) {
114                 userForm.copyFrom(fromSSO, request.getLocale());
115                 userForm.setDataFromSSO(true);
116               }
117             }
118             
119             userForm.setPasswordText(null);
120             userForm.setPasswordConfirm(null);
121             request.setAttribute("model", new BasePageModel(
122                     "newUser.addNewUser", request, response, mapping));
123         } catch (Exception JavaDoc e) {
124             errors.add(ActionErrors.GLOBAL_ERROR,
125                     new ActionError("error.editing.user", e.toString()));
126             mLogger.error("ERROR in newUser", e);
127         }
128         return forward;
129     }
130     
131     
132     /** Process POST of new user information. */
133     public ActionForward add(
134             ActionMapping mapping,
135             ActionForm actionForm,
136             HttpServletRequest JavaDoc request,
137             HttpServletResponse JavaDoc response)
138             throws IOException JavaDoc, ServletException JavaDoc {
139         UserFormEx form = (UserFormEx)actionForm;
140         RollerRequest rreq = RollerRequest.getRollerRequest(request);
141         ServletContext JavaDoc ctx = rreq.getServletContext();
142         RollerContext rollerContext = RollerContext.getRollerContext();
143         
144         boolean reg_allowed =
145                 RollerRuntimeConfig.getBooleanProperty("users.registration.enabled");
146         
147         if ( !reg_allowed && !request.isUserInRole("admin")) {
148             throw new ServletException JavaDoc("New users disabled!");
149         }
150         
151         ActionMessages msgs = new ActionMessages();
152         ActionMessages errors = validate(form, new ActionErrors());
153         if (!errors.isEmpty()) {
154             saveErrors(request, errors);
155         } else try {
156             // Add new user
157
UserManager mgr = RollerFactory.getRoller().getUserManager();
158             
159             UserData ud = new UserData();
160             form.copyTo(ud, request.getLocale()); // doesn't copy password
161
ud.setId(null);
162             ud.setDateCreated(new java.util.Date JavaDoc());
163             ud.setEnabled(Boolean.TRUE);
164             
165             // If user set both password and passwordConfirm then reset password
166
if ( !StringUtils.isEmpty(form.getPasswordText())
167             && !StringUtils.isEmpty(form.getPasswordConfirm())) {
168                 ud.resetPassword(RollerFactory.getRoller(),
169                         form.getPasswordText(), form.getPasswordConfirm());
170             }
171             
172             // save new user
173
mgr.addUser(ud);
174             RollerFactory.getRoller().flush();
175             
176             if (form.getAdminCreated()) {
177                 // User created for admin, so return to new user page with empty form
178
msgs.add(ActionMessages.GLOBAL_MESSAGE,
179                         new ActionMessage("newUser.created"));
180                 saveMessages(request, msgs);
181                 form.reset(mapping, request);
182                 return createUser(mapping, actionForm, request, response);
183             } else {
184                 // User registered, so go to welcome page
185
request.setAttribute("contextURL",
186                         RollerRuntimeConfig.getAbsoluteContextURL());
187                 return mapping.findForward("welcome.page");
188             }
189         } catch (RollerException e) {
190             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(e.getMessage()));
191             saveErrors(request,errors);
192             mLogger.error("ERROR in addUser", e);
193             form.setUserName("");
194         }
195         
196         if (form.getAdminCreated()) {
197             return mapping.findForward("createUser");
198         } else {
199             // Error occured, send user back to new user form
200
return mapping.findForward("registerUser");
201         }
202     }
203     
204     
205     /** Validate user form. TODO: replace with Struts validation. */
206     protected ActionMessages validate( UserFormEx form, ActionMessages errors ) {
207       
208         // if usingSSO, we don't want to error on empty password/username from HTML form.
209
form.setDataFromSSO(false);
210         boolean usingSSO = RollerConfig.getBooleanProperty("users.sso.enabled");
211         if(usingSSO) {
212           boolean storePassword = RollerConfig.getBooleanProperty("users.sso.passwords.saveInRollerDb");
213           UserData fromSSO = CustomUserRegistry.getUserDetailsFromAuthentication();
214           if(fromSSO != null) {
215             String JavaDoc password = RollerConfig.getProperty("users.sso.passwords.defaultValue", "<unknown>");
216             if(storePassword) {
217               password = fromSSO.getPassword();
218             }
219             form.setPasswordText(password);
220             form.setPasswordConfirm(password);
221             form.setUserName(fromSSO.getUserName());
222             form.setDataFromSSO(true);
223           }
224         }
225       
226         super.validate(form, errors);
227         if ( StringUtils.isEmpty(form.getPasswordText())
228         && StringUtils.isEmpty(form.getPasswordConfirm())) {
229             errors.add( ActionErrors.GLOBAL_ERROR,
230                     new ActionError("error.add.user.missingPassword"));
231         }
232         return errors;
233     }
234     
235 }
Popular Tags