1 16 17 package org.apache.jetspeed.services.security.registry; 18 19 import java.util.Iterator ; 21 22 import javax.servlet.ServletConfig ; 23 24 import org.apache.jetspeed.om.SecurityReference; 25 import org.apache.jetspeed.om.profile.Entry; 26 import org.apache.jetspeed.om.registry.RegistryEntry; 27 import org.apache.jetspeed.om.registry.SecurityEntry; 28 import org.apache.jetspeed.om.security.GroupRole; 29 import org.apache.jetspeed.om.security.JetspeedUser; 30 import org.apache.jetspeed.portal.Portlet; 31 import org.apache.jetspeed.portal.PortletController; 32 import org.apache.jetspeed.portal.PortletSet; 33 import org.apache.jetspeed.services.Registry; 34 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService; 35 import org.apache.jetspeed.services.logging.JetspeedLogger; 36 import org.apache.jetspeed.services.security.JetspeedRoleManagement; 37 import org.apache.jetspeed.services.security.PortalAccessController; 38 import org.apache.jetspeed.services.security.PortalResource; 39 import org.apache.turbine.services.InitializationException; 40 import org.apache.turbine.services.TurbineBaseService; 41 42 48 public class RegistryAccessController extends TurbineBaseService implements PortalAccessController 49 { 50 53 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(RegistryAccessController.class.getName()); 54 55 66 public boolean checkPermission(JetspeedUser user, Portlet portlet, String action) 67 { 68 return checkPermission(user, portlet, action, null); 69 } 70 71 83 public boolean checkPermission(JetspeedUser user, Portlet portlet, String action, String owner) 84 { 85 SecurityReference securityRef = portlet.getPortletConfig().getSecurityRef(); 86 if (securityRef != null) 87 { 88 return checkPermission( user, securityRef, action, owner); 89 } 90 91 String portletName = portlet.getName(); 92 RegistryEntry registryEntry = null; 93 if (!(portlet instanceof PortletSet)) 95 { 96 registryEntry = (RegistryEntry) Registry.getEntry(Registry.PORTLET, portletName); 97 } 98 if (registryEntry==null) { 100 PortletSet ps = portlet.getPortletConfig().getPortletSet(); 101 if (ps != null) { 102 PortletController pc = ps.getController(); 103 if (pc != null) { 104 portletName = pc.getConfig().getName(); 105 registryEntry = (RegistryEntry)Registry.getEntry(Registry.PORTLET_CONTROLLER, portletName); 106 } 107 } 108 } 109 if (registryEntry==null) { 110 return true; } 112 113 return checkPermission(user, registryEntry, action, owner); 114 } 115 116 127 public boolean checkPermission(JetspeedUser user, Entry entry, String action) 128 { 129 return checkPermission( user, entry, action, null); 130 } 131 132 144 public boolean checkPermission(JetspeedUser user, Entry entry, String action, String owner) 145 { 146 SecurityReference securityRef = entry.getSecurityRef(); 147 if (securityRef == null) 148 { 149 return checkPermission( user, Registry.getEntry( Registry.PORTLET, entry.getParent()), action, owner); 150 } 151 return checkPermission( user, securityRef, action, owner); 152 } 153 154 155 166 public boolean checkPermission(JetspeedUser user, PortalResource resource, String action) 167 { 168 switch (resource.getResourceType()) 169 { 170 case PortalResource.TYPE_ENTRY: 171 return checkPermission(user, resource.getEntry(), action, resource.getOwner()); 172 case PortalResource.TYPE_PORTLET: 173 return checkPermission(user, resource.getPortlet(), action, resource.getOwner()); 174 case PortalResource.TYPE_REGISTRY: 175 return checkPermission(user, resource.getRegistryEntry(), action, resource.getOwner()); 176 case PortalResource.TYPE_REGISTRY_PARAMETER: 177 return checkPermission(user, resource.getRegistryParameter(), action, resource.getOwner()); 178 } 179 180 logger.error( "In " + this.getClass().getName() + ".checkPermission(user, resource, action) - Unkown resource = " + resource.getResourceType()); 182 return false; 183 } 184 185 193 private boolean checkPermission(JetspeedUser user, RegistryEntry regEntry, String action, String owner) 194 { 195 SecurityReference securityRef = regEntry.getSecurityRef(); 196 if (securityRef == null) 197 return true; return checkPermission( user, securityRef, action, owner); 199 } 200 201 208 private boolean checkPermission(JetspeedUser user, SecurityReference securityRef, String action, String owner) 209 { 210 SecurityEntry securityEntry = (SecurityEntry) Registry.getEntry( Registry.SECURITY, securityRef.getParent()); 211 if (securityEntry == null) 212 { 213 logger.warn("Security id " + securityRef.getParent() + " does not exist. This was requested by the user " + user.getUserName()); 214 return false; 215 } 216 217 if (securityEntry.allowsUser(user.getUserName(), action, owner)) 218 { 219 return true; 220 } 221 222 try 223 { 224 for( Iterator roles = JetspeedRoleManagement.getRoles(user.getUserName()); roles.hasNext();) 225 { 226 GroupRole grouprole = (GroupRole) roles.next(); 227 String groupname = grouprole.getGroup().getName(); 228 String rolename = grouprole.getRole().getName(); 229 if (securityEntry.allowsGroupRole(groupname, rolename, action)) 230 return true; 231 } 232 233 247 } 248 catch (Exception e) 249 { 250 logger.error("Exception", e); 251 return false; 252 } 253 return false; 254 } 255 256 259 260 267 public synchronized void init(ServletConfig conf) 268 throws InitializationException 269 { 270 if (getInit()) return; 271 272 super.init(conf); 273 274 setInit(true); 275 } 276 } 277 | Popular Tags |