1 13 package info.magnolia.cms.security; 14 15 import info.magnolia.cms.beans.config.ContentRepository; 16 import info.magnolia.cms.core.Content; 17 import info.magnolia.cms.core.HierarchyManager; 18 import info.magnolia.cms.core.search.QueryManager; 19 import info.magnolia.cms.core.search.SearchFactory; 20 import info.magnolia.cms.util.SimpleUrlPattern; 21 import info.magnolia.cms.util.UrlPattern; 22 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import javax.jcr.LoginException; 29 import javax.jcr.PathNotFoundException; 30 import javax.jcr.RepositoryException; 31 import javax.jcr.Session; 32 import javax.jcr.SimpleCredentials; 33 import javax.servlet.http.HttpServletRequest ; 34 35 import org.apache.commons.lang.StringUtils; 36 import org.apache.log4j.Logger; 37 38 39 43 public final class SessionAccessControl { 44 45 private static Logger log = Logger.getLogger(SessionAccessControl.class); 46 47 private static final String ATTRIBUTE_REPOSITORY_SESSION_PREFIX = "mgnlRepositorySession_"; 49 private static final String ATTRIBUTE_HM_PREFIX = "mgnlHMgr_"; 51 private static final String ATTRIBUTE_AM_PREFIX = "mgnlAccessMgr_"; 53 private static final String ATTRIBUTE_QM_PREFIX = "mgnlQueryMgr_"; 55 private static final String DEFAULT_REPOSITORY = ContentRepository.WEBSITE; 56 57 private static final String DEFAULT_WORKSPACE = ContentRepository.DEFAULT_WORKSPACE; 58 59 62 private SessionAccessControl() { 63 } 65 66 70 protected static Session getSession(HttpServletRequest request) throws LoginException, RepositoryException { 71 return getSession(request, DEFAULT_REPOSITORY); 72 } 73 74 79 protected static Session getSession(HttpServletRequest request, String repositoryID) throws LoginException, 80 RepositoryException { 81 return getSession(request, repositoryID, DEFAULT_WORKSPACE); 82 } 83 84 89 protected static Session getSession(HttpServletRequest request, String repositoryID, String workspaceID) 90 throws LoginException, RepositoryException { 91 return getRepositorySession(request, repositoryID, workspaceID); 92 } 93 94 100 public static HierarchyManager getHierarchyManager(HttpServletRequest request) { 101 return getHierarchyManager(request, DEFAULT_REPOSITORY); 102 } 103 104 110 public static HierarchyManager getHierarchyManager(HttpServletRequest request, String repositoryID) { 111 return getHierarchyManager(request, repositoryID, DEFAULT_WORKSPACE); 112 } 113 114 120 public static HierarchyManager getHierarchyManager(HttpServletRequest request, String repositoryID, 121 String workspaceID) { 122 HierarchyManager hm = (HierarchyManager) request.getSession().getAttribute( 123 ATTRIBUTE_HM_PREFIX + repositoryID + "_" + workspaceID); if (hm == null) { 125 createHierarchyManager(request, repositoryID, workspaceID); 126 return (HierarchyManager) request.getSession().getAttribute( 127 ATTRIBUTE_HM_PREFIX + repositoryID + "_" + workspaceID); } 129 return hm; 130 } 131 132 136 public static AccessManager getAccessManager(HttpServletRequest request) { 137 return getAccessManager(request, DEFAULT_REPOSITORY); 138 } 139 140 145 public static AccessManager getAccessManager(HttpServletRequest request, String repositoryID) { 146 return getAccessManager(request, repositoryID, DEFAULT_WORKSPACE); 147 } 148 149 155 public static AccessManager getAccessManager(HttpServletRequest request, String repositoryID, String workspaceID) { 156 157 AccessManager accessManager = (AccessManager) request.getSession().getAttribute( 158 ATTRIBUTE_AM_PREFIX + repositoryID + "_" + workspaceID); 160 if (accessManager == null) { 161 getHierarchyManager(request, repositoryID, workspaceID); 163 accessManager = (AccessManager) request.getSession().getAttribute( 165 ATTRIBUTE_AM_PREFIX + repositoryID + "_" + workspaceID); } 167 168 return accessManager; 169 } 170 171 175 public static QueryManager getQueryManager(HttpServletRequest request) throws RepositoryException { 176 return getQueryManager(request, DEFAULT_REPOSITORY); 177 } 178 179 184 public static QueryManager getQueryManager(HttpServletRequest request, String repositoryID) 185 throws RepositoryException { 186 return getQueryManager(request, repositoryID, DEFAULT_WORKSPACE); 187 } 188 189 195 public static QueryManager getQueryManager(HttpServletRequest request, String repositoryID, String workspaceID) 196 throws RepositoryException { 197 QueryManager queryManager = (QueryManager) request.getSession().getAttribute( 198 ATTRIBUTE_QM_PREFIX + repositoryID + "_" + workspaceID); if (queryManager == null) { 200 javax.jcr.query.QueryManager qm = getSession(request, repositoryID, workspaceID) 201 .getWorkspace() 202 .getQueryManager(); 203 queryManager = SearchFactory.getAccessControllableQueryManager(qm, getAccessManager( 204 request, 205 repositoryID, 206 workspaceID)); 207 request.getSession().setAttribute(ATTRIBUTE_QM_PREFIX + repositoryID + "_" + workspaceID, queryManager); } 209 return queryManager; 210 } 211 212 private static Session getRepositorySession(HttpServletRequest request, String repositoryID, String workspaceID) 213 throws LoginException, RepositoryException { 214 Object ticket = request.getSession().getAttribute( 215 ATTRIBUTE_REPOSITORY_SESSION_PREFIX + repositoryID + "_" + workspaceID); if (ticket == null) { 217 createRepositorySession(request, repositoryID, workspaceID); 218 return (Session) request.getSession().getAttribute( 219 ATTRIBUTE_REPOSITORY_SESSION_PREFIX + repositoryID + "_" + workspaceID); } 221 return (Session) ticket; 222 } 223 224 228 public static void createSession(HttpServletRequest request) throws LoginException, RepositoryException { 229 createRepositorySession(request, DEFAULT_REPOSITORY); 230 } 231 232 236 private static void createRepositorySession(HttpServletRequest request, String repositoryID) throws LoginException, 237 RepositoryException { 238 createRepositorySession(request, repositoryID, DEFAULT_WORKSPACE); 239 } 240 241 245 private static void createRepositorySession(HttpServletRequest request, String repositoryID, String workspaceID) 246 throws LoginException, RepositoryException { 247 SimpleCredentials sc = new SimpleCredentials(ContentRepository.SYSTEM_USER, ContentRepository.SYSTEM_PSWD); 248 Session session = ContentRepository.getRepository(repositoryID).login(sc, workspaceID); 249 request.getSession().setAttribute(ATTRIBUTE_REPOSITORY_SESSION_PREFIX + repositoryID + "_" + workspaceID, session); 251 Content userNode = getUserNode(request); 252 253 if (userNode != null) { 254 List acl = new ArrayList (); 255 updateRolesACL(userNode, acl, repositoryID); 256 AccessManagerImpl accessManager = new AccessManagerImpl(); 257 accessManager.setPermissionList(acl); 258 request.getSession().setAttribute(ATTRIBUTE_AM_PREFIX + repositoryID + "_" + workspaceID, accessManager); } 260 } 261 262 private static void createHierarchyManager(HttpServletRequest request, String repositoryID, String workspaceID) { 263 HierarchyManager hm = new HierarchyManager(Authenticator.getUserId(request)); 264 try { 265 hm.init(getSession(request, repositoryID, workspaceID).getRootNode()); 266 hm.setAccessManager((AccessManager) request.getSession().getAttribute( 267 ATTRIBUTE_AM_PREFIX + repositoryID + "_" + workspaceID)); request.getSession().setAttribute(ATTRIBUTE_HM_PREFIX + repositoryID + "_" + workspaceID, hm); } 270 catch (RepositoryException re) { 271 log.error(re.getMessage(), re); 272 } 273 } 274 275 279 public static Content getUserNode(HttpServletRequest request) { 280 Content userPage = Authenticator.getUserPage(request); 281 try { 282 if (userPage == null) { 283 String userid = Authenticator.getUserId(request); 284 if (StringUtils.isNotBlank(userid)) { 285 userPage = ContentRepository.getHierarchyManager(ContentRepository.USERS).getContent(userid); 286 } 287 } 288 } 289 catch (Exception e) { 290 log.error(e.getMessage(), e); 291 } 292 return userPage; 293 } 294 295 299 public static boolean isSecuredSession(HttpServletRequest request) { 300 return Authenticator.isAuthenticated(request); 301 } 302 303 308 private static void updateACL(Content roleNode, List userACL, String repositoryID) { 309 try { 310 Content acl = null; 312 try { 313 acl = roleNode.getContent("acl_" + repositoryID); } 315 catch (PathNotFoundException e) { 316 log.warn("No acl defined for role " + roleNode.getHandle() + " on repository \"" + repositoryID + "\""); return; 318 } 319 320 Collection aclCollection = acl.getChildren(); 321 if (aclCollection == null) { 322 return; 323 } 324 Iterator children = aclCollection.iterator(); 325 while (children.hasNext()) { 326 Content map = (Content) children.next(); 327 String path = map.getNodeData("path").getString(); 329 UrlPattern p = new SimpleUrlPattern(path); 330 Permission permission = new PermissionImpl(); 331 permission.setPattern(p); 332 permission.setPermissions(map.getNodeData("permissions").getLong()); userACL.add(permission); 334 } 335 } 336 catch (RepositoryException re) { 337 log.error(re.getMessage(), re); 338 } 339 } 340 341 346 private static void updateRolesACL(Content userNode, List groupACL, String repositoryID) { 347 348 if (userNode == null) { 349 log.error("Called updateRolesACL with a null userNode"); return; 351 } 352 353 try { 354 HierarchyManager rolesHierarchy = ContentRepository.getHierarchyManager(ContentRepository.USER_ROLES); 355 357 Content acl = null; 358 try { 359 acl = userNode.getContent("roles"); } 361 catch (PathNotFoundException e) { 362 log.warn("No roles defined for user " + userNode.getHandle()); return; 364 } 365 366 Collection aclCollection = acl.getChildren(); 367 if (aclCollection == null) { 368 return; 369 } 370 Iterator children = aclCollection.iterator(); 371 372 while (children.hasNext()) { 373 Content map = (Content) children.next(); 374 String groupPath = map.getNodeData("path").getString(); if (StringUtils.isNotEmpty(groupPath)) { 376 Content roleNode = rolesHierarchy.getContent(groupPath); 377 updateACL(roleNode, groupACL, repositoryID); 378 } 379 } 380 } 381 catch (RepositoryException e) { 382 log.error("Failed to update roles ACL"); log.error(e.getMessage(), e); 384 } 385 } 386 387 391 public static void invalidateUser(HttpServletRequest request) { 392 request.getSession().invalidate(); 393 } 394 395 400 public static void logout(HttpServletRequest request, String repositoryID) { 401 try { 402 getSession(request, repositoryID).logout(); 403 } 404 catch (RepositoryException re) { 405 log.error(re.getMessage(), re); 406 } 407 } 408 } 409 | Popular Tags |