1 16 17 package org.apache.jetspeed.services.security.turbine; 18 19 import java.util.Iterator ; 21 22 import javax.servlet.ServletConfig ; 23 24 import org.apache.jetspeed.om.profile.Entry; 25 import org.apache.jetspeed.om.registry.RegistryEntry; 26 import org.apache.jetspeed.om.registry.Security; 27 import org.apache.jetspeed.om.security.GroupRole; 28 import org.apache.jetspeed.om.security.JetspeedUser; 29 import org.apache.jetspeed.om.security.Role; 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.JetspeedSecurity; 34 import org.apache.jetspeed.services.Registry; 35 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService; 36 import org.apache.jetspeed.services.logging.JetspeedLogger; 37 import org.apache.jetspeed.services.resources.JetspeedResources; 38 import org.apache.jetspeed.services.security.PortalAccessController; 39 import org.apache.jetspeed.services.security.PortalResource; 40 import org.apache.turbine.services.InitializationException; 41 import org.apache.turbine.services.TurbineBaseService; 42 43 49 public class TurbineAccessController extends TurbineBaseService 50 implements PortalAccessController 51 { 52 55 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(TurbineAccessController.class.getName()); 56 57 private final static String CONFIG_DEFAULT_PERMISSION_LOGGEDIN = "services.JetspeedSecurity.permission.default.loggedin"; 58 private final static String CONFIG_DEFAULT_PERMISSION_ANONYMOUS = "services.JetspeedSecurity.permission.default.anonymous"; 59 60 71 public boolean checkPermission(JetspeedUser user, Portlet portlet, String action) 72 { 73 return checkPermission(user, portlet, action, null); 74 } 75 76 88 public boolean checkPermission(JetspeedUser user, Portlet portlet, String action, String owner) 89 { 90 String portletName = portlet.getName(); 91 RegistryEntry regEntry = (RegistryEntry)Registry.getEntry(Registry.PORTLET, portletName); 92 if (regEntry==null) 94 { 95 PortletSet ps = portlet.getPortletConfig().getPortletSet(); 96 if (ps != null) 97 { 98 PortletController pc = ps.getController(); 99 if (pc != null) 100 { 101 portletName = pc.getConfig().getName(); 102 regEntry = (RegistryEntry)Registry.getEntry(Registry.PORTLET_CONTROLLER, portletName); 103 } 104 } 105 } 106 if (regEntry==null) 107 { 108 return checkDefaultPermission(user, action); 109 } 110 return checkPermission(user, regEntry, action); 111 } 112 113 124 public boolean checkPermission(JetspeedUser user, Entry entry, String action) 125 { 126 return checkPermission(user, entry, action, null); 127 } 128 129 141 public boolean checkPermission(JetspeedUser user, Entry entry, String action, String owner) 142 { 143 String portletName = entry.getParent(); 144 RegistryEntry regEntry = (RegistryEntry)Registry.getEntry(Registry.PORTLET, portletName); 145 if (regEntry==null) 146 { 147 return checkDefaultPermission(user, action); 148 } 149 return checkPermission(user, regEntry, action); 150 } 151 152 153 164 public boolean checkPermission(JetspeedUser user, PortalResource resource, String action) 165 { 166 switch (resource.getResourceType()) 167 { 168 case PortalResource.TYPE_ENTRY: 169 return checkPermission(user, resource.getEntry(), action); 170 case PortalResource.TYPE_REGISTRY: 171 return checkPermission(user, resource.getRegistryEntry(), action); 172 case PortalResource.TYPE_REGISTRY_PARAMETER: 173 return checkPermission(user, resource.getRegistryParameter(), action); 174 case PortalResource.TYPE_PORTLET: 175 return checkPermission(user, resource.getPortlet(), action); 176 case PortalResource.TYPE_ENTRY_PARAMETER: 177 return checkPermission(user, (RegistryEntry) resource.getEntryParameter(), action); 178 } 179 return false; 180 } 181 182 191 private boolean checkPermission(JetspeedUser user, RegistryEntry regEntry, String action) 192 { 193 Security security = regEntry.getSecurity(); 194 if (null == security) 195 return checkDefaultPermission( user, action); 196 String securityRole = security.getRole(); 197 if (null == securityRole) 198 return checkDefaultPermission( user, action); 199 200 201 try 203 { 204 205 if (false == JetspeedSecurity.hasRole(user.getUserName(), securityRole)) 206 { 207 return false; 208 } 209 210 } catch (Exception e) 211 { 212 logger.error("Exception", e); 213 return false; 214 } 215 216 return checkPermission(user, action); 217 } 218 219 227 236 private boolean checkPermission(JetspeedUser user, String action) 237 { 238 if (action == null) 239 { 240 return true; 241 } 242 243 try 245 { 246 Iterator roles = JetspeedSecurity.getRoles(user.getUserName()); 247 while (roles.hasNext()) 248 { 249 GroupRole gr = (GroupRole) roles.next(); 250 Role role = gr.getRole(); 251 return JetspeedSecurity.hasPermission(role.getName(), action); 252 } 253 254 } catch (Exception e) 255 { 256 logger.error("Exception", e); 257 return false; 258 } 259 260 return true; 261 } 262 263 private boolean checkDefaultPermission(JetspeedUser user, String action) 264 { 265 String defaultPermissions[] = null; 266 try 267 { 268 if ( (user == null) || !user.hasLoggedIn() ) 269 { 270 defaultPermissions = JetspeedResources.getStringArray(CONFIG_DEFAULT_PERMISSION_ANONYMOUS); 271 } else 272 { 273 defaultPermissions = JetspeedResources.getStringArray(CONFIG_DEFAULT_PERMISSION_LOGGEDIN); 274 } 275 } 276 catch (Exception e) 277 { 278 logger.error( "Error checking permissions for " + user + " on " + action, e); 279 } 280 for (int i = 0; i < defaultPermissions.length; i++) 281 { 282 if (defaultPermissions[i].equals("*")) 283 return true; 284 if (defaultPermissions[i].equals(action)) 285 return true; 286 } 287 return false; 288 } 289 290 293 294 301 public synchronized void init(ServletConfig conf) 302 throws InitializationException 303 { 304 if (getInit()) return; 305 306 super.init(conf); 307 308 setInit(true); 309 } 310 311 } 312 | Popular Tags |