1 41 42 43 package org.jahia.engines.logout; 44 45 import java.util.ArrayList ; 46 import java.util.Locale ; 47 import java.util.Vector ; 48 49 import org.jahia.data.JahiaData; 50 import org.jahia.data.events.JahiaEvent; 51 import org.jahia.engines.JahiaEngine; 52 import org.jahia.engines.login.Login_Engine; 53 import org.jahia.engines.core.Core_Engine; 54 import org.jahia.exceptions.JahiaException; 55 import org.jahia.params.ParamBean; 56 import org.jahia.registries.EnginesRegistry; 57 import org.jahia.registries.ServicesRegistry; 58 import org.jahia.services.pages.ContentPage; 59 import org.jahia.services.pages.JahiaPage; 60 import org.jahia.services.pages.JahiaPageBaseService; 61 import org.jahia.services.sites.JahiaSite; 62 import org.jahia.services.usermanager.JahiaGroup; 63 import org.jahia.services.usermanager.JahiaGroupManagerService; 64 import org.jahia.services.usermanager.JahiaUser; 65 import org.jahia.services.cache.HtmlCache; 66 import org.jahia.services.cache.CacheFactory; 67 import java.util.Properties ; 68 import org.jahia.settings.SettingsBean; 69 import org.jahia.bin.Jahia; 70 import javax.servlet.http.Cookie ; 71 72 73 80 public class Logout_Engine implements JahiaEngine { 81 82 83 private static Logout_Engine instance = null; 84 85 86 public static final String ENGINE_NAME = "logout"; 87 88 89 private static final org.apache.log4j.Logger logger = 90 org.apache.log4j.Logger.getLogger (Logout_Engine.class); 91 92 93 96 private Logout_Engine () { 97 logger.debug ("***** Starting " + Logout_Engine.class.getName () + " engine *****"); 98 } 99 100 101 106 public static synchronized Logout_Engine getInstance () { 107 108 if (instance == null) { 109 instance = new Logout_Engine (); 110 } 111 return instance; 112 } 113 114 115 122 public boolean authoriseRender (ParamBean jParams) { 123 return true; } 125 126 127 137 public String renderLink (ParamBean jParams, Object theObj) 138 throws JahiaException { 139 145 Properties extraProps = new Properties (); 146 if (theObj != null) { 147 Integer pageID = (Integer ) theObj; 148 extraProps.setProperty("pid", pageID.toString()); 149 } 150 151 String theUrl = jParams.composeEngineUrl (ENGINE_NAME, extraProps, "?engine_params=logout"); 152 153 return jParams.getResponse ().encodeURL (theUrl); 154 } 155 156 157 165 public boolean needsJahiaData (ParamBean jParams) { 166 return false; } 169 170 171 177 public void handleActions (ParamBean jParams, JahiaData jData) 178 throws JahiaException { 179 String engineParams = jParams.getRequest ().getParameter ("engine_params"); 180 181 jParams.setOperationMode (ParamBean.NORMAL); 183 184 JahiaPage logoutPage = null; 185 186 if ((engineParams != null) && (engineParams.equals ("logout"))) { 190 logger.debug ("User [" + jParams.getUser ().getName () + "] logged out."); 191 192 JahiaEvent theEvent = new JahiaEvent (this, jParams, jParams.getUser ()); 194 ServicesRegistry.getInstance ().getJahiaEventService ().fireLogout (theEvent); 195 196 logoutPage = getLogoutPage (jData); 197 198 JahiaUser curUser = jParams.getUser(); 201 SettingsBean settingsBean = Jahia.getSettings(); 202 String cookieAuthKey = curUser.getProperty(settingsBean.getCookieAuthUserPropertyName()); 203 Cookie authCookie = new Cookie (settingsBean.getCookieAuthCookieName(), cookieAuthKey); 204 authCookie.setPath(jParams.getRequest().getContextPath()); 205 authCookie.setMaxAge(0); jParams.getRealResponse().addCookie(authCookie); 207 curUser.removeProperty(settingsBean.getCookieAuthUserPropertyName()); 208 209 if (ParamBean.EDIT.equals (jParams.getOperationMode ())) { 211 HtmlCache htmlCache = CacheFactory.getHtmlCache (); 213 if (htmlCache != null) { 214 htmlCache.invalidateUserEntries (jParams.getUser ().getUsername ()); 216 217 } else { 218 logger.warn ("Could not get the HTML cache instance!!"); 219 } 220 } 221 222 ServicesRegistry.getInstance ().getJahiaApplicationsDispatchingService () 223 .flushAllSessionsCaches (jParams.getSession ()); 224 jParams.purgeSession (); 227 jParams.setUserGuest (jParams.getSiteID ()); 229 } 230 231 if (logoutPage == null) { 232 throw new JahiaException ("403 Forbidden - Page:" + jParams.getPageID (), 234 "No read access for page " + jParams.getPageID (), 235 JahiaException.SECURITY_ERROR, 236 JahiaException.ERROR_SEVERITY); 237 } else { 238 239 if (!logoutPage.hasEntry (ContentPage.ACTIVE_PAGE_INFOS, 240 jParams.getEntryLoadRequest () 241 .getFirstLocale (true).toString ())) { 242 ArrayList siteLanguages = 243 jParams.getSite ().getLanguageSettingsAsLocales (true); 244 boolean skip = false; 245 for (int i = 0; i < siteLanguages.size (); i++) { 246 Locale locale = (Locale ) siteLanguages.get (i); 247 if (!skip && logoutPage.hasEntry (ContentPage.ACTIVE_PAGE_INFOS, 248 locale.toString ())) { 249 jParams.changeLanguage (locale); 250 skip = true; 251 } 252 } 253 } 254 255 try { 257 if (logoutPage.hasActiveEntries ()) { 258 JahiaPageBaseService pageService = JahiaPageBaseService.getInstance (); 259 logoutPage = pageService.lookupPage (logoutPage.getID (), jParams); 260 } 261 } catch (Throwable t) { 262 t.printStackTrace (); 263 } 264 265 if (logoutPage == null) { 266 throw new JahiaException ("404 Page not found ", 267 "Logout page not found ", 268 JahiaException.SECURITY_ERROR, 269 JahiaException.ERROR_SEVERITY); 270 } 271 272 ContentPage contentPage = ServicesRegistry.getInstance () 274 .getJahiaPageService ().lookupContentPage (logoutPage.getID (), 275 jParams.getEntryLoadRequest (), true); 276 jParams.changePage (contentPage); 277 278 } 279 if (logoutPage.hasActiveEntries () 280 && logoutPage.checkGuestAccess (jParams.getSiteID ())) { 281 JahiaData jData2 = new JahiaData (jParams, true); 282 ((JahiaEngine) EnginesRegistry.getInstance ().getEngine (Core_Engine.ENGINE_NAME)).handleActions ( 283 jParams, jData2); 284 285 } else { 286 jParams.setUserGuest (jParams.getSiteID ()); 287 JahiaData jData2 = new JahiaData (jParams, false); 288 ((JahiaEngine) EnginesRegistry.getInstance ().getEngine (Login_Engine.ENGINE_NAME)).handleActions ( 289 jParams, jData2); 290 } 291 } 292 293 298 public final String getName () { 299 return ENGINE_NAME; 300 } 301 302 303 310 private JahiaPage getLogoutPage (JahiaData jData) { 311 312 JahiaUser user = jData.params ().getUser (); 313 JahiaPage page = jData.params ().getPage (); 314 JahiaSite site = jData.params ().getSite (); 315 316 if (page == null || user == null || site == null) 317 return null; 318 319 if (page.hasActiveEntries () && page.checkGuestAccess (site.getID ())) 320 return page; 322 323 try { 325 326 JahiaPageBaseService pageService = JahiaPageBaseService.getInstance (); 327 328 if (user.getHomepageID () >= 0) { 330 331 try { 332 page = pageService.lookupPage (user.getHomepageID (), 333 jData.params ().getEntryLoadRequest (), false); 334 if ((page != null) && page.hasActiveEntries () 335 && page.checkGuestAccess (site.getID ())) 336 return page; } catch (Throwable t) { 338 t.printStackTrace (); 339 } 340 } 341 342 JahiaGroupManagerService grpServ = 344 ServicesRegistry.getInstance ().getJahiaGroupManagerService (); 345 346 Vector v = grpServ.getUserMembership (user); 347 int size = v.size (); 348 String grpKey; 349 JahiaGroup grp; 350 for (int i = 0; i < size; i++) { 351 grpKey = (String ) v.get (i); 352 grp = grpServ.lookupGroup (grpKey); 353 if (grp != null 354 && grp.getSiteID () == site.getID () 355 && grp.getHomepageID () >= 0) { 356 357 try { 358 page = pageService.lookupPage (grp.getHomepageID (), 359 jData.params ().getEntryLoadRequest (), false); 360 if ((page != null) && page.hasActiveEntries () 361 && page.checkGuestAccess (site.getID ())) 362 return page; 365 } catch (Throwable t) { 366 t.printStackTrace (); 367 } 368 } 369 } 370 page = pageService.lookupPage (site.getHomePageID (), 372 jData.params ().getEntryLoadRequest (), false); 373 if ((page != null) && page.hasActiveEntries () 374 && page.checkGuestAccess (site.getID ())) { 375 return page; 376 } else if ((page != null) && !page.hasActiveEntries ()) { 377 return page; 378 } 379 } catch (Throwable t) { 380 t.printStackTrace (); 381 } 382 return null; } 384 } 385 | Popular Tags |