KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > engines > users > NewUserRegistration_Engine


1 package org.jahia.engines.users;
2
3 import org.jahia.engines.JahiaEngine;
4 import org.jahia.engines.EngineToolBox;
5 import org.jahia.params.ParamBean;
6 import org.jahia.exceptions.JahiaException;
7 import org.jahia.data.JahiaData;
8 import org.jahia.exceptions.JahiaSessionExpirationException;
9 import java.util.HashMap JavaDoc;
10 import javax.servlet.http.HttpSession JavaDoc;
11 import org.jahia.registries.ServicesRegistry;
12 import java.util.Map JavaDoc;
13 import java.util.Set JavaDoc;
14 import java.util.Enumeration JavaDoc;
15 import java.util.Properties JavaDoc;
16 import org.jahia.services.usermanager.JahiaUser;
17 import org.jahia.engines.EngineMessages;
18 import org.jahia.engines.EngineMessage;
19 import org.jahia.services.usermanager.JahiaGroup;
20 import org.jahia.services.usermanager.JahiaGroupManagerService;
21 import org.jahia.services.usermanager.JahiaUserManagerService;
22
23 public class NewUserRegistration_Engine implements JahiaEngine {
24
25     private static final String JavaDoc EDIT_JSP = "newuserregistration.jsp";
26     private static final String JavaDoc SUCCESS_JSP = "userregistrationok.jsp";
27     private static final String JavaDoc CLOSE_JSP = "close";
28     private static NewUserRegistration_Engine instance = null;
29     public static final String JavaDoc ENGINE_NAME = "newuserregistration";
30     private EngineToolBox toolBox;
31
32     /** logging */
33     private static final org.apache.log4j.Logger logger =
34             org.apache.log4j.Logger.getLogger (NewUserRegistration_Engine.class);
35
36     /**
37      * Private constructor for singleton pattern
38      */

39     private NewUserRegistration_Engine () {
40         logger.debug ("***** Starting " + NewUserRegistration_Engine.class.getName () +
41                 " engine *****");
42         toolBox = EngineToolBox.getInstance ();
43     }
44
45     /**
46      * Returns the singleton instance
47      *
48      * @return NewUserRegistration_Engine the singleton object instance.
49      */

50     public static synchronized NewUserRegistration_Engine getInstance () {
51         if (instance == null) {
52             instance = new NewUserRegistration_Engine ();
53         }
54         return instance;
55     }
56
57     /**
58      * Check if we have the rights to view this engine
59      *
60      * @param jParams ParamBean object
61      *
62      * @return boolean if we are allowed to render this engine, false otherwise
63      */

64     public boolean authoriseRender (ParamBean jParams) {
65         return jParams.getUser().getUsername().equals(JahiaUserManagerService.GUEST_USERNAME);
66     }
67
68     /**
69      * Renders a link to this engine.
70      *
71      * @param jParams ParamBean object to be used to generate URL.
72      * @param theObj the target object on which we want to process this engine
73      *
74      * @return a String containing an URL to this engine
75      *
76      * @throws JahiaException
77      */

78     public String JavaDoc renderLink (ParamBean jParams, Object JavaDoc theObj)
79             throws JahiaException {
80         String JavaDoc rightParams = (String JavaDoc) theObj;
81         String JavaDoc params = EMPTY_STRING;
82         params += "?mode=display&screen=edit";
83         // params += rightParams;
84
return jParams.composeEngineUrl (ENGINE_NAME, params);
85     }
86
87     /**
88      * needsJahiaData
89      *
90      * @param jParams the current ParamBean
91      *
92      * @return true if the engine requires a JahiaData object to be constructed before
93      * dispatching to it.
94      */

95     public boolean needsJahiaData (ParamBean jParams) {
96         return true;
97     }
98
99     /**
100      * handles the engine actions
101      *
102      * @param jParams a ParamBean object
103      * @param jData a JahiaData object (not mandatory)
104      *
105      * @throws JahiaException if there is an error processing input parameters
106      * @throws JahiaSessionExpirationException
107      * if the session has expired while processing input actions
108      */

109     public void handleActions (ParamBean jParams, JahiaData jData)
110             throws JahiaException,
111             JahiaSessionExpirationException {
112         // initalizes the hashmap
113
HashMap JavaDoc engineMap = initEngineMap (jParams);
114
115         processLastScreen (jParams, engineMap);
116         processCurrentScreen (jParams, engineMap);
117
118         // displays the screen
119
toolBox.displayScreen (jParams, engineMap);
120
121     }
122
123     /**
124      * Retrieve the engine name.
125      *
126      * @return the engine name.
127      */

128     public final String JavaDoc getName () {
129         return ENGINE_NAME;
130     }
131
132     /**
133      * processes the last screen sent by the user
134      *
135      * @param jParams a ParamBean object
136      * @param engineMap the engineMap object containing the current engine state
137      *
138      * @throws JahiaException if there is an error processing input parameters
139      */

140     public void processLastScreen (ParamBean jParams, HashMap JavaDoc engineMap)
141             throws JahiaException {
142         // gets engineMap values
143
String JavaDoc theScreen = (String JavaDoc) engineMap.get ("screen");
144         if (theScreen == null) {
145             throw new JahiaException ("EditUserPreferences_Engine.processLastScreen",
146                     "Error in parameters",
147                     JahiaException.PARAMETER_ERROR,
148                     JahiaException.CRITICAL_SEVERITY);
149         }
150         if (theScreen.equals ("edit")) {
151         } else if (theScreen.equals ("save")) {
152             boolean allValuesValid = true;
153
154             // first let's retrieve all the values from the form.
155
String JavaDoc userName = jParams.getRequest().getParameter("newUser_username");
156             String JavaDoc password1 = jParams.getRequest().getParameter("newUser_password1");
157             String JavaDoc password2 = jParams.getRequest().getParameter("newUser_password2");
158             String JavaDoc[] groupList = jParams.getRequest().getParameterValues("newUser_groupList");
159             Properties JavaDoc userProperties = new Properties JavaDoc();
160             Enumeration JavaDoc paramNames = jParams.getRequest().getParameterNames();
161             while (paramNames.hasMoreElements()) {
162                 String JavaDoc curParamName = (String JavaDoc) paramNames.nextElement();
163                 String JavaDoc[] curParamValues = jParams.getRequest().getParameterValues(curParamName);
164                 if (curParamName.startsWith("newUserProp_")) {
165                     // found a user property, let's store it.
166
String JavaDoc propName = curParamName.substring("newUserProp_".length());
167                     StringBuffer JavaDoc propValue = new StringBuffer JavaDoc();
168                     for (int i=0; i < curParamValues.length; i++) {
169                         propValue.append(curParamValues[i]);
170                         if (i < (curParamValues.length -1)) {
171                             propValue.append(";");
172                         }
173                     }
174                     userProperties.setProperty(propName, propValue.toString());
175                 }
176                 jParams.getRequest().setAttribute(curParamName, curParamValues);
177             }
178
179             EngineMessages resultMessages = new EngineMessages();
180             if ((userName == null) || "".equals(userName)) {
181                 allValuesValid = false;
182                 EngineMessage errorMessage = new EngineMessage(
183                     "org.jahia.engines.users.newuserregistration.missingUserName");
184                 resultMessages.add("newUserRegistration", errorMessage);
185             }
186             if ((password1 == null) || "".equals(password1)) {
187                 allValuesValid = false;
188                 EngineMessage errorMessage = new EngineMessage(
189                     "org.jahia.engines.users.newuserregistration.missingPassword1");
190                 resultMessages.add("newUserRegistration", errorMessage);
191             }
192             if ((password2 == null) || "".equals(password2)) {
193                 allValuesValid = false;
194                 EngineMessage errorMessage = new EngineMessage(
195                     "org.jahia.engines.users.newuserregistration.missingPassword2");
196                 resultMessages.add("newUserRegistration", errorMessage);
197             }
198             if (allValuesValid) {
199                 // now let's check password integrity.
200
if (!password1.equals(password2)) {
201                     allValuesValid = false;
202                     EngineMessage errorMessage = new EngineMessage(
203                         "org.jahia.engines.users.newuserregistration.passwordsDontMatch");
204                     resultMessages.add("newUserRegistration", errorMessage);
205                 }
206             }
207             if (allValuesValid) {
208                 if (password1.length() < 6) {
209                     allValuesValid = false;
210                     EngineMessage errorMessage = new EngineMessage("org.jahia.engines.users.newuserregistration.passwordTooShort");
211                     resultMessages.add("newUserRegistration", errorMessage);
212                 }
213             }
214
215             if (allValuesValid) {
216                 // now let's check if username already exists.
217
JahiaUser existingUser = ServicesRegistry.getInstance().getJahiaUserManagerService().lookupUser(jParams.getSiteID(), userName);
218                 if (existingUser != null) {
219                     allValuesValid = false;
220                     EngineMessage errorMessage = new EngineMessage(
221                         "org.jahia.engines.users.newuserregistration.userNameAlreadyExists");
222                     resultMessages.add("newUserRegistration", errorMessage);
223                 }
224             }
225
226             if (allValuesValid) {
227                 // now let's check that the group list doesn't contain
228
// invalid groups
229
if (groupList != null) {
230                     for (int i = 0; i < groupList.length; i++) {
231                         String JavaDoc curGroupName = groupList[i];
232                         if (JahiaGroupManagerService.ADMINISTRATORS_GROUPNAME.
233                             equals(curGroupName) ||
234                             JahiaGroupManagerService.GUEST_GROUPNAME.equals(
235                             curGroupName) ||
236                             JahiaGroupManagerService.USERS_GROUPNAME.equals(
237                             curGroupName)) {
238                             allValuesValid = false;
239                             EngineMessage errorMessage = new EngineMessage(
240                                 "org.jahia.engines.users.newuserregistration.unauthorizedGroup",
241                                 curGroupName);
242                             resultMessages.add("newUserRegistration",
243                                                errorMessage);
244                         }
245                     }
246                 }
247             }
248
249             if (allValuesValid) {
250                 JahiaUser newUser = ServicesRegistry.getInstance().
251                                     getJahiaUserManagerService().
252                                     createUser(userName,
253                                                password1,
254                                                userName + ":" +
255                                                Integer.toString(jParams.
256                     getSiteID()),
257                                                jParams.getSiteID(),
258                                                userProperties);
259                 if (newUser != null) {
260                     ServicesRegistry.getInstance().
261                         getJahiaSiteUserManagerService().addMember(jParams.
262                         getSiteID(), newUser);
263
264                     // make user part of selected groups.
265
if ((groupList != null) && (groupList.length > 0)) {
266                         for (int i = 0; i < groupList.length; i++) {
267                             JahiaGroup curGroup = ServicesRegistry.getInstance().
268                                                   getJahiaGroupManagerService().
269                                                   lookupGroup(
270                                 jParams.getSiteID(), groupList[i]);
271                             if (curGroup != null) {
272                                 curGroup.addMember(newUser);
273                             }
274                         }
275                     }
276                 } else {
277                     allValuesValid = false;
278                     EngineMessage errorMessage = new EngineMessage(
279                         "org.jahia.engines.users.newuserregistration.errorWhileCreatingUser");
280                     resultMessages.add("newUserRegistration", errorMessage);
281                 }
282             }
283             if (!allValuesValid) {
284                 // let's stay on the edit screen.
285
engineMap.put("screen", "edit");
286                 resultMessages.saveMessages(jParams.getRequest());
287             }
288         }
289     }
290
291     /**
292      * prepares the screen requested by the user
293      *
294      * @param jParams a ParamBean object
295      * @param engineMap the engineMap object containing the current engine state
296      *
297      * @throws JahiaException if there is an error processing input parameters
298      */

299     public void processCurrentScreen (ParamBean jParams, HashMap JavaDoc engineMap)
300             throws JahiaException {
301         String JavaDoc theScreen = (String JavaDoc) engineMap.get ("screen");
302
303         // let's prepare the group list
304

305         Map JavaDoc groupMap = ServicesRegistry.getInstance().getJahiaSiteGroupManagerService().getGroups(jParams.getSiteID());
306         groupMap.remove(JahiaGroupManagerService.ADMINISTRATORS_GROUPNAME);
307         groupMap.remove(JahiaGroupManagerService.GUEST_GROUPNAME);
308         groupMap.remove(JahiaGroupManagerService.USERS_GROUPNAME);
309         Set JavaDoc groupNameSet = groupMap.keySet();
310         engineMap.put("groupList", groupNameSet);
311
312         String JavaDoc targetJSP = EDIT_JSP;
313         if ("save".equals(theScreen)) {
314             targetJSP = SUCCESS_JSP;
315         }
316
317         String JavaDoc jspSiteMapFileName = jParams.getPage ().getPageTemplate ().getSourcePath ();
318         jspSiteMapFileName = jspSiteMapFileName.substring (0,
319                 jspSiteMapFileName.lastIndexOf ("/") + 1) +
320                 targetJSP;
321         engineMap.put (ENGINE_OUTPUT_FILE_PARAM, jspSiteMapFileName);
322
323         jParams.getRequest ().setAttribute ("jahia_session_engineMap", engineMap);
324
325     }
326
327     /**
328      * inits the engine map
329      *
330      * @param jParams a ParamBean object (with request and response)
331      *
332      * @return a HashMap object containing all the basic values needed by an engine
333      *
334      * @throws JahiaException if there is an error building the current engineMap
335      * @throws JahiaSessionExpirationException
336      * if the session has expired while processing input actions
337      */

338     private HashMap JavaDoc initEngineMap (ParamBean jParams)
339             throws JahiaException,
340             JahiaSessionExpirationException {
341         String JavaDoc theScreen = jParams.getRequest ().getParameter ("screen");
342
343         // gets session values
344
//HttpSession theSession = jParams.getRequest().getSession (true);
345
HttpSession JavaDoc theSession = jParams.getSession ();
346
347         HashMap JavaDoc engineMap = (HashMap JavaDoc) theSession.getAttribute (
348                 "jahia_session_engineMap");
349
350         if (engineMap == null) {
351             theScreen = "edit";
352             // init engine map
353
engineMap = new HashMap JavaDoc ();
354         }
355         engineMap.put (RENDER_TYPE_PARAM, new Integer JavaDoc (JahiaEngine.RENDERTYPE_FORWARD));
356         engineMap.put (ENGINE_NAME_PARAM, ENGINE_NAME);
357         engineMap.put (ENGINE_URL_PARAM, jParams.composeEngineUrl (ENGINE_NAME));
358         theSession.setAttribute ("jahia_session_engineMap", engineMap);
359
360         if (theScreen == null) {
361             theScreen = "edit";
362         }
363
364         // sets screen
365
engineMap.put ("screen", theScreen);
366         if (theScreen.equals ("cancel")) {
367             engineMap.put ("jspSource", CLOSE_JSP);
368         } else if (theScreen.equals ("save")) {
369             engineMap.put ("jspSource", CLOSE_JSP);
370         } else {
371             engineMap.put ("jspSource", EDIT_JSP);
372         }
373
374         // sets engineMap for JSPs
375
jParams.getRequest ().setAttribute ("engineTitle", "New User Registration");
376         jParams.getRequest ().setAttribute ("org.jahia.engines.EngineHashMap",
377                 engineMap);
378
379         return engineMap;
380     }
381 }
382
Popular Tags