1 38 39 package org.jahia.engines.mysettings; 40 41 42 import org.jahia.data.JahiaData; 43 import org.jahia.engines.EngineMessage; 44 import org.jahia.engines.EngineMessages; 45 import org.jahia.engines.EngineToolBox; 46 import org.jahia.engines.JahiaEngine; 47 import org.jahia.engines.core.Core_Engine; 48 import org.jahia.exceptions.JahiaException; 49 import org.jahia.exceptions.JahiaSessionExpirationException; 50 import org.jahia.params.ParamBean; 51 import org.jahia.registries.ServicesRegistry; 52 import org.jahia.services.usermanager.JahiaUser; 53 import org.jahia.services.usermanager.JahiaUserManagerService; 54 import org.jahia.services.usermanager.UserProperties; 55 import org.jahia.services.usermanager.UserProperty; 56 57 import java.util.Enumeration ; 58 import java.util.HashMap ; 59 import java.util.Iterator ; 60 61 62 67 public class MySettingsEngine implements JahiaEngine { 68 69 public static final String REQUEST_KEY_PREFIX = "mysettings-user-"; 70 public static final String REQUEST_PASSWORD_KEY = REQUEST_KEY_PREFIX + "password"; 71 public static final String REQUEST_PASSWORD_CONFIRMATION_KEY = 72 REQUEST_KEY_PREFIX + "passwordConfirmation"; 73 74 public static final String SEPARATOR = "#"; 75 public static final String USER_PROPERTY_PREFIX = REQUEST_KEY_PREFIX + "property" + SEPARATOR; 76 public static final String EDIT_TOKEN = "edit"; 77 public static final String SAVE_TOKEN = "save"; 78 79 82 public static final String ENGINE_NAME = "mysettings"; 83 84 private static final String EDIT_JSP = "mysettings.jsp"; 85 private static final String SUCCESS_JSP = "mysettingschanged.jsp"; 86 87 90 private static final org.apache.log4j.Logger logger = 91 org.apache.log4j.Logger.getLogger (MySettingsEngine.class); 92 93 96 private static MySettingsEngine instance; 97 private EngineToolBox toolbox; 98 99 100 103 public MySettingsEngine () { 104 logger.debug ("***** Starting " + MySettingsEngine.class.getName () + " engine *****"); 105 toolbox = EngineToolBox.getInstance (); 106 } 107 108 109 114 public static synchronized MySettingsEngine getInstance () { 115 if (instance == null) 116 instance = new MySettingsEngine (); 117 return instance; 118 } 119 120 121 public boolean authoriseRender (ParamBean jParams) { 122 return true; 123 } 124 125 126 public String renderLink (ParamBean jParams, Object theObj) throws JahiaException { 127 String theUrl = jParams.composeEngineUrl (ENGINE_NAME, EMPTY_STRING); 128 if (theObj != null) 129 theUrl += theObj; 130 return jParams.getResponse ().encodeURL (theUrl); 131 } 132 133 134 public boolean needsJahiaData (ParamBean jParams) { 135 return true; 136 } 137 138 139 public void handleActions (ParamBean jParams, JahiaData jData) 140 throws JahiaException { 141 142 HashMap engineMap = new HashMap (); 144 initEngineMap (jParams, engineMap); 145 146 processScreen (jParams, engineMap); 147 148 toolbox.displayScreen (jParams, engineMap); 150 151 } 152 153 154 public final String getName () { 155 return ENGINE_NAME; 156 } 157 158 159 164 private void initEngineMap (ParamBean jParams, HashMap engineMap) 165 throws JahiaException { 166 String theScreen = jParams.getRequest ().getParameter ("screen"); 168 if (theScreen == null) { 169 theScreen = EDIT_TOKEN; 170 } 171 172 engineMap.put ("screen", theScreen); 173 if (!theScreen.equals (SAVE_TOKEN)) { 174 engineMap.put ("jspSource", EDIT_JSP); 175 engineMap.put (ENGINE_URL_PARAM, jParams.composeEngineUrl (ENGINE_NAME, EMPTY_STRING)); 176 engineMap.put (ENGINE_NAME_PARAM, ENGINE_NAME); 177 178 } else { 179 engineMap.put (ENGINE_NAME_PARAM, Core_Engine.ENGINE_NAME); 180 engineMap.put (ENGINE_URL_PARAM, jParams.composePageUrl (jParams.getPageID ())); 181 engineMap.put ("jspSource", "close"); 182 } 183 engineMap.put (RENDER_TYPE_PARAM, new Integer (JahiaEngine.RENDERTYPE_FORWARD)); 184 engineMap.put ("jahiaBuild", new Integer (jParams.settings ().getBuildNumber ())); 185 engineMap.put ("javascriptUrl", jParams.settings ().getJsHttpPath ()); 186 187 188 if (logger.isDebugEnabled ()) 189 logger.debug ("fetch user properties"); 190 191 JahiaUser user = jParams.getUser (); 193 UserProperties userProps = user.getUserProperties (); 194 195 Iterator propNameIter = userProps.propertyNameIterator(); 197 while (propNameIter.hasNext()) { 198 String key = (String ) propNameIter.next(); 199 200 UserProperty value = userProps.getUserProperty(key); 202 203 engineMap.put (USER_PROPERTY_PREFIX + key, value); 205 logger.debug ("Adding property ["+ key +"] = ["+ value +"]"); 206 } 207 208 if (logger.isDebugEnabled ()) 209 logger.debug ("done with properties"); 210 211 212 jParams.getRequest ().setAttribute ("engineTitle", "MySettings"); 213 jParams.getRequest().setAttribute( "engineMap", engineMap ); 214 } 215 216 217 222 public void processScreen (ParamBean jParams, HashMap engineMap) 223 throws JahiaException, 224 JahiaSessionExpirationException { 225 226 if (logger.isDebugEnabled ()) 227 logger.debug ("started"); 228 229 String theScreen = jParams.getRequest ().getParameter ("screen"); 232 if (theScreen == null) { 233 theScreen = EDIT_TOKEN; 234 } 235 236 JahiaUser user = jParams.getUser (); 237 if (user == null) 238 return; 239 240 boolean ok = true; 243 244 if (theScreen.equals (SAVE_TOKEN)) { 246 ok = processSave (jParams, user); 247 } 248 249 if (!ok) { 250 } 252 253 if (logger.isDebugEnabled ()) 254 logger.debug ("end"); 255 256 String targetJSP = EDIT_JSP; 257 if ("save".equals(theScreen) && ok) { 258 targetJSP = SUCCESS_JSP; 259 } 260 261 String jspSiteMapFileName = jParams.getPage ().getPageTemplate ().getSourcePath (); 262 jspSiteMapFileName = jspSiteMapFileName.substring (0, 263 jspSiteMapFileName.lastIndexOf ("/") + 1) + 264 targetJSP; 265 engineMap.put (ENGINE_OUTPUT_FILE_PARAM, jspSiteMapFileName); 266 } 267 268 private boolean processSave (ParamBean jParams, JahiaUser user) { 269 if (logger.isDebugEnabled ()) 270 logger.debug ("save button clicked"); 271 272 EngineMessages resultMessages = new EngineMessages(); 273 ServicesRegistry registry = ServicesRegistry.getInstance (); 274 if (registry != null) { 275 JahiaUserManagerService service = registry.getJahiaUserManagerService (); 276 if (service != null) { 277 278 String password = jParams.getRequest ().getParameter (REQUEST_PASSWORD_KEY); 280 if ((password != null) && (!"".equals(password))) { 281 if (logger.isDebugEnabled ()) 282 logger.debug ("password changed, check for password confirmation"); 283 284 String passwordConfirmation = jParams.getRequest ().getParameter (REQUEST_PASSWORD_CONFIRMATION_KEY); 286 if (password.equals (passwordConfirmation)) { 287 if (logger.isDebugEnabled ()) 288 logger.debug ("password and password confirmation match!"); 289 user.setPassword (password); 290 registry.getJahiaSiteUserManagerService().refreshUser(jParams.getSiteID(),user.getUsername()); 291 } else { 292 if (logger.isDebugEnabled ()) 293 logger.debug ("password and password confirmation do not match!"); 294 EngineMessage errorMessage = new EngineMessage( 295 "org.jahia.engines.mysettings.passwordsDontMatch"); 296 resultMessages.add("mySettings", errorMessage); 297 resultMessages.saveMessages(jParams.getRequest()); 298 return false; 299 } 300 if (password.length() < 6) { 301 EngineMessage errorMessage = new EngineMessage("org.jahia.engines.mysettings.passwordTooShort"); 302 resultMessages.add("newUserRegistration", errorMessage); 303 resultMessages.saveMessages(jParams.getRequest()); 304 return false; 305 } 306 } 307 308 Enumeration names = jParams.getRequest ().getParameterNames (); 311 if (names != null) { 312 while (names.hasMoreElements ()) { 313 String name = (String ) names.nextElement (); 314 if (name != null && name.startsWith (USER_PROPERTY_PREFIX)) { 315 String newValue = jParams.getRequest ().getParameter (name); 316 int index = name.indexOf (SEPARATOR); 317 String key = name.substring (index + 1); 318 UserProperty currentProp = user.getUserProperty (key); 319 if (newValue == null) { 320 continue; 321 } 322 if (currentProp == null) { 323 user.setProperty (key, newValue); 324 } else if (!currentProp.getValue().equals (newValue)) { 325 if (!currentProp.isReadOnly()) { 327 user.setProperty(key, newValue); 328 } 329 } 330 } 331 } 332 } 333 334 } else { 335 return false; 336 } 337 } else { 338 return false; 339 } 340 341 if (logger.isDebugEnabled ()) 342 logger.debug ("save process ended"); 343 return true; 344 } 345 346 347 } 348 | Popular Tags |