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 ; 10 import javax.servlet.http.HttpSession ; 11 import org.jahia.registries.ServicesRegistry; 12 import java.util.Map ; 13 import java.util.Set ; 14 import java.util.Enumeration ; 15 import java.util.Properties ; 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 EDIT_JSP = "newuserregistration.jsp"; 26 private static final String SUCCESS_JSP = "userregistrationok.jsp"; 27 private static final String CLOSE_JSP = "close"; 28 private static NewUserRegistration_Engine instance = null; 29 public static final String ENGINE_NAME = "newuserregistration"; 30 private EngineToolBox toolBox; 31 32 33 private static final org.apache.log4j.Logger logger = 34 org.apache.log4j.Logger.getLogger (NewUserRegistration_Engine.class); 35 36 39 private NewUserRegistration_Engine () { 40 logger.debug ("***** Starting " + NewUserRegistration_Engine.class.getName () + 41 " engine *****"); 42 toolBox = EngineToolBox.getInstance (); 43 } 44 45 50 public static synchronized NewUserRegistration_Engine getInstance () { 51 if (instance == null) { 52 instance = new NewUserRegistration_Engine (); 53 } 54 return instance; 55 } 56 57 64 public boolean authoriseRender (ParamBean jParams) { 65 return jParams.getUser().getUsername().equals(JahiaUserManagerService.GUEST_USERNAME); 66 } 67 68 78 public String renderLink (ParamBean jParams, Object theObj) 79 throws JahiaException { 80 String rightParams = (String ) theObj; 81 String params = EMPTY_STRING; 82 params += "?mode=display&screen=edit"; 83 return jParams.composeEngineUrl (ENGINE_NAME, params); 85 } 86 87 95 public boolean needsJahiaData (ParamBean jParams) { 96 return true; 97 } 98 99 109 public void handleActions (ParamBean jParams, JahiaData jData) 110 throws JahiaException, 111 JahiaSessionExpirationException { 112 HashMap engineMap = initEngineMap (jParams); 114 115 processLastScreen (jParams, engineMap); 116 processCurrentScreen (jParams, engineMap); 117 118 toolBox.displayScreen (jParams, engineMap); 120 121 } 122 123 128 public final String getName () { 129 return ENGINE_NAME; 130 } 131 132 140 public void processLastScreen (ParamBean jParams, HashMap engineMap) 141 throws JahiaException { 142 String theScreen = (String ) 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 String userName = jParams.getRequest().getParameter("newUser_username"); 156 String password1 = jParams.getRequest().getParameter("newUser_password1"); 157 String password2 = jParams.getRequest().getParameter("newUser_password2"); 158 String [] groupList = jParams.getRequest().getParameterValues("newUser_groupList"); 159 Properties userProperties = new Properties (); 160 Enumeration paramNames = jParams.getRequest().getParameterNames(); 161 while (paramNames.hasMoreElements()) { 162 String curParamName = (String ) paramNames.nextElement(); 163 String [] curParamValues = jParams.getRequest().getParameterValues(curParamName); 164 if (curParamName.startsWith("newUserProp_")) { 165 String propName = curParamName.substring("newUserProp_".length()); 167 StringBuffer propValue = new StringBuffer (); 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 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 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 if (groupList != null) { 230 for (int i = 0; i < groupList.length; i++) { 231 String 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 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 engineMap.put("screen", "edit"); 286 resultMessages.saveMessages(jParams.getRequest()); 287 } 288 } 289 } 290 291 299 public void processCurrentScreen (ParamBean jParams, HashMap engineMap) 300 throws JahiaException { 301 String theScreen = (String ) engineMap.get ("screen"); 302 303 305 Map 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 groupNameSet = groupMap.keySet(); 310 engineMap.put("groupList", groupNameSet); 311 312 String targetJSP = EDIT_JSP; 313 if ("save".equals(theScreen)) { 314 targetJSP = SUCCESS_JSP; 315 } 316 317 String 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 338 private HashMap initEngineMap (ParamBean jParams) 339 throws JahiaException, 340 JahiaSessionExpirationException { 341 String theScreen = jParams.getRequest ().getParameter ("screen"); 342 343 HttpSession theSession = jParams.getSession (); 346 347 HashMap engineMap = (HashMap ) theSession.getAttribute ( 348 "jahia_session_engineMap"); 349 350 if (engineMap == null) { 351 theScreen = "edit"; 352 engineMap = new HashMap (); 354 } 355 engineMap.put (RENDER_TYPE_PARAM, new Integer (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 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 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 |