1 18 package org.apache.roller.ui.core; 19 20 import java.io.Serializable ; 21 import java.security.Principal ; 22 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.http.HttpSession ; 25 import javax.servlet.http.HttpSessionActivationListener ; 26 import javax.servlet.http.HttpSessionEvent ; 27 import javax.servlet.http.HttpSessionListener ; 28 29 import org.apache.commons.collections.ArrayStack; 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.apache.roller.RollerException; 33 import org.apache.roller.config.RollerConfig; 34 import org.apache.roller.model.RollerFactory; 35 import org.apache.roller.model.UserManager; 36 import org.apache.roller.pojos.PermissionsData; 37 import org.apache.roller.pojos.UserData; 38 import org.apache.roller.pojos.WebsiteData; 39 import org.apache.roller.ui.core.security.AutoProvision; 40 41 42 47 public class RollerSession 48 implements HttpSessionListener , HttpSessionActivationListener , Serializable 49 { 50 static final long serialVersionUID = 5890132909166913727L; 51 52 private UserData authenticatedUser = null; 53 54 private static Log mLogger = 55 LogFactory.getFactory().getInstance(RollerSession.class); 56 57 public static final String ROLLER_SESSION = "org.apache.roller.rollersession"; 58 public static final String ERROR_MESSAGE = "rollererror_message"; 59 public static final String STATUS_MESSAGE = "rollerstatus_message"; 60 61 65 public static RollerSession getRollerSession(HttpServletRequest request) 66 { 67 RollerSession rollerSession = null; 68 HttpSession session = request.getSession(false); 69 if (session != null) 70 { 71 rollerSession = (RollerSession)session.getAttribute(ROLLER_SESSION); 72 if (rollerSession == null) 73 { 74 rollerSession = new RollerSession(); 77 session.setAttribute(ROLLER_SESSION, rollerSession); 78 } 79 Principal principal = request.getUserPrincipal(); 80 if (rollerSession.getAuthenticatedUser() == null && principal != null) 81 { 82 try 83 { 84 UserManager umgr = RollerFactory.getRoller().getUserManager(); 85 UserData user = umgr.getUserByUserName(principal.getName()); 86 87 if(user == null && RollerConfig.getBooleanProperty("users.sso.autoProvision.enabled")) { 90 AutoProvision provisioner = RollerContext.getAutoProvision(); 92 if(provisioner != null) 93 { 94 boolean userProvisioned = provisioner.execute(); 95 if(userProvisioned) 96 { 97 user = umgr.getUserByUserName(principal.getName()); 99 } 100 } 101 } 102 if(user != null && user.getEnabled().booleanValue()) 104 { 105 rollerSession.setAuthenticatedUser(user); 106 } 107 } 108 catch (RollerException e) 109 { 110 mLogger.error("ERROR: getting user object",e); 111 } 112 } 113 } 114 return rollerSession; 115 } 116 117 119 120 public void sessionCreated(HttpSessionEvent se) 121 { 122 RollerSession rollerSession = new RollerSession(); 123 se.getSession().setAttribute(ROLLER_SESSION, rollerSession); 124 RollerContext rctx = RollerContext.getRollerContext(); 125 rctx.sessionCreated(se); 126 } 127 128 public void sessionDestroyed(HttpSessionEvent se) 129 { 130 RollerContext rctx = RollerContext.getRollerContext(); 131 rctx.sessionDestroyed(se); 132 clearSession(se); 133 } 134 135 136 public void sessionDidActivate(HttpSessionEvent se) 137 { 138 } 139 140 145 public void sessionWillPassivate(HttpSessionEvent se) 146 { 147 clearSession(se); 148 } 149 150 152 155 public UserData getAuthenticatedUser() 156 { 157 return authenticatedUser; 158 } 159 160 163 public void setAuthenticatedUser(UserData authenticatedUser) 164 { 165 this.authenticatedUser = authenticatedUser; 166 } 167 168 171 public boolean isGlobalAdminUser() throws RollerException 172 { 173 UserData user = getAuthenticatedUser(); 174 if (user != null && user.hasRole("admin") 175 && user.getEnabled().booleanValue()) return true; 176 return false; 177 } 178 179 182 public boolean isUserAuthorized(WebsiteData website) 183 throws RollerException 184 { 185 UserData user = getAuthenticatedUser(); 186 if (user != null && user.getEnabled().booleanValue()) 187 return hasPermissions(website, PermissionsData.LIMITED); 188 return false; 189 } 190 191 194 public boolean isUserAuthorizedToAuthor(WebsiteData website) 195 throws RollerException 196 { 197 UserData user = getAuthenticatedUser(); 198 if (user != null && user.getEnabled().booleanValue()) 199 return hasPermissions(website, PermissionsData.AUTHOR); 200 return false; 201 } 202 203 206 public boolean isUserAuthorizedToAdmin(WebsiteData website) 207 throws RollerException 208 { 209 UserData user = getAuthenticatedUser(); 210 if (user != null && user.getEnabled().booleanValue()) 211 return hasPermissions(website, PermissionsData.ADMIN); 212 return false; 213 } 214 215 private boolean hasPermissions(WebsiteData website, short mask) 216 { 217 UserData user = getAuthenticatedUser(); 218 if (website != null && user != null) 219 { 220 return website.hasUserPermissions(user, mask); 221 } 222 return false; 223 } 224 225 227 private void clearSession(HttpSessionEvent se) 228 { 229 HttpSession session = se.getSession(); 230 try 231 { 232 session.removeAttribute(ROLLER_SESSION); 233 } 234 catch (Throwable e) 235 { 236 if (mLogger.isDebugEnabled()) 237 { 238 mLogger.debug("EXCEPTION PURGING session attributes",e); 240 } 241 } 242 } 243 } 244 245 | Popular Tags |