1/*2 * Copyright 2000-2004 The Apache Software Foundation.3 * 4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */1617packageorg.apache.jetspeed.services.security.turbine;
1819// Java imports20import java.util.Iterator;
2122import javax.servlet.ServletConfig;
2324import org.apache.jetspeed.om.profile.Entry;
25import org.apache.jetspeed.om.registry.RegistryEntry;
26import org.apache.jetspeed.om.registry.Security;
27import org.apache.jetspeed.om.security.GroupRole;
28import org.apache.jetspeed.om.security.JetspeedUser;
29import org.apache.jetspeed.om.security.Role;
30import org.apache.jetspeed.portal.Portlet;
31import org.apache.jetspeed.portal.PortletController;
32import org.apache.jetspeed.portal.PortletSet;
33import org.apache.jetspeed.services.JetspeedSecurity;
34import org.apache.jetspeed.services.Registry;
35import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
36import org.apache.jetspeed.services.logging.JetspeedLogger;
37import org.apache.jetspeed.services.resources.JetspeedResources;
38import org.apache.jetspeed.services.security.PortalAccessController;
39import org.apache.jetspeed.services.security.PortalResource;
40import org.apache.turbine.services.InitializationException;
41import org.apache.turbine.services.TurbineBaseService;
4243/***44 * TurbineAccessController45 *46 * @author <a href="paulsp@apache.org">Paul Spencer</a>47 * @version $Id: TurbineAccessController.java,v 1.8 2004/02/23 03:54:49 jford Exp $48 */49publicclassTurbineAccessControllerextends TurbineBaseService
50 implements PortalAccessController51 {
52/***53 * Static initialization of the logger for this class54 */55privatestaticfinalJetspeedLogger logger = JetspeedLogFactoryService.getLogger(TurbineAccessController.class.getName());
5657privatefinalstatic String CONFIG_DEFAULT_PERMISSION_LOGGEDIN = "services.JetspeedSecurity.permission.default.loggedin";
58privatefinalstatic String CONFIG_DEFAULT_PERMISSION_ANONYMOUS = "services.JetspeedSecurity.permission.default.anonymous";
5960/***61 * Given a <code>JetspeedUser</code>, authorize that user to perform the secured action on62 * the given <code>Portlet</code> resource. If the user does not have63 * sufficient privilege to perform the action on the resource, the check returns false,64 * otherwise when sufficient privilege is present, checkPermission returns true.65 *66 * @param user the user to be checked.67 * @param portlet the portlet resource.68 * @param action the secured action to be performed on the resource by the user.69 * @return boolean true if the user has sufficient privilege.70 */71publicboolean checkPermission(JetspeedUser user, Portlet portlet, String action)
72 {
73return checkPermission(user, portlet, action, null);
74 }
7576/***77 * Given a <code>JetspeedUser</code>, authorize that user to perform the secured action on78 * the given <code>Portlet</code> resource. If the user does not have79 * sufficient privilege to perform the action on the resource, the check returns false,80 * otherwise when sufficient privilege is present, checkPermission returns true.81 *82 * @param user the user to be checked.83 * @param portlet the portlet resource.84 * @param action the secured action to be performed on the resource by the user.85 * @param owner of the entry, i.e. the username86 * @return boolean true if the user has sufficient privilege.87 */88publicboolean checkPermission(JetspeedUser user, Portlet portlet, String action, String owner)
89 {
90 String portletName = portlet.getName();
91RegistryEntry regEntry = (RegistryEntry)Registry.getEntry(Registry.PORTLET, portletName);
92//portlet is not a portlet - probably a controller or control93if (regEntry==null)
94 {
95PortletSet ps = portlet.getPortletConfig().getPortletSet();
96if (ps != null)
97 {
98PortletController pc = ps.getController();
99if (pc != null)
100 {
101 portletName = pc.getConfig().getName();
102 regEntry = (RegistryEntry)Registry.getEntry(Registry.PORTLET_CONTROLLER, portletName);
103 }
104 }
105 }
106if (regEntry==null)
107 {
108return checkDefaultPermission(user, action);
109 }
110return checkPermission(user, regEntry, action);
111 }
112113/***114 * Given a <code>JetspeedUser</code>, authorize that user to perform the secured action on115 * the given Portlet Instance (<code>Entry</code>) resource. If the user does not have116 * sufficient privilege to perform the action on the resource, the check returns false,117 * otherwise when sufficient privilege is present, checkPermission returns true.118 *119 * @param user the user to be checked.120 * @param entry the portlet instance resource.121 * @param action the secured action to be performed on the resource by the user.122 * @return boolean true if the user has sufficient privilege.123 */124publicboolean checkPermission(JetspeedUser user, Entry entry, String action)
125 {
126return checkPermission(user, entry, action, null);
127 }
128129/***130 * Given a <code>JetspeedUser</code>, authorize that user to perform the secured action on131 * the given Portlet Instance (<code>Entry</code>) resource. If the user does not have132 * sufficient privilege to perform the action on the resource, the check returns false,133 * otherwise when sufficient privilege is present, checkPermission returns true.134 *135 * @param user the user to be checked.136 * @param entry the portlet instance resource.137 * @param action the secured action to be performed on the resource by the user.138 * @param owner of the entry, i.e. the username139 * @return boolean true if the user has sufficient privilege.140 */141publicboolean checkPermission(JetspeedUser user, Entry entry, String action, String owner)
142 {
143 String portletName = entry.getParent();
144RegistryEntry regEntry = (RegistryEntry)Registry.getEntry(Registry.PORTLET, portletName);
145if (regEntry==null)
146 {
147return checkDefaultPermission(user, action);
148 }
149return checkPermission(user, regEntry, action);
150 }
151152153/***154 * Given a <code>JetspeedUser</code>, authorize that user to perform the secured action on155 * the given resource. If the user does not have156 * sufficient privilege to perform the action on the resource, the check returns false,157 * otherwise when sufficient privilege is present, checkPermission returns true.158 *159 * @param user the user to be checked.160 * @param resource requesting an action161 * @param action the secured action to be performed on the resource by the user.162 * @return boolean true if the user has sufficient privilege.163 */164publicboolean checkPermission(JetspeedUser user, PortalResource resource, String action)
165 {
166switch (resource.getResourceType())
167 {
168case PortalResource.TYPE_ENTRY:
169return checkPermission(user, resource.getEntry(), action);
170case PortalResource.TYPE_REGISTRY:
171return checkPermission(user, resource.getRegistryEntry(), action);
172case PortalResource.TYPE_REGISTRY_PARAMETER:
173return checkPermission(user, resource.getRegistryParameter(), action);
174case PortalResource.TYPE_PORTLET:
175return checkPermission(user, resource.getPortlet(), action);
176case PortalResource.TYPE_ENTRY_PARAMETER:
177return checkPermission(user, (RegistryEntry) resource.getEntryParameter(), action);
178 }
179return false;
180 }
181182/***183 * Checks if the user has access to a given portlet for the given action184 *185 * @param user the requesting user.186 * @param regEntry the registry entry from the registry.187 * @param action the jetspeed-action (view, edit, customize, delete...) for which permission is being checked.188 * @exception Sends a RegistryException if the manager can't add189 * the provided entry190 */191privateboolean checkPermission(JetspeedUser user, RegistryEntry regEntry, String action)
192 {
193Security security = regEntry.getSecurity();
194if (null == security)
195return checkDefaultPermission( user, action);
196 String securityRole = security.getRole();
197if (null == securityRole)
198return checkDefaultPermission( user, action);
199200201// determine if Portlet has specified role202try203 {
204205if (false == JetspeedSecurity.hasRole(user.getUserName(), securityRole))
206 {
207return false;
208 }
209210 } catch (Exception e)
211 {
212 logger.error("Exception", e);
213return false;
214 }
215216return checkPermission(user, action);
217 }
218219/***220 * Checks if the currently logged on user has access for the given action221 *222 * @param user the requesting user.223 * @param action the jetspeed-action (view, edit, customize, delete...) for which permission is being checked.224 * @exception Sends a RegistryException if the manager can't add225 * the provided entry226 */227/***228 * given the rundata, checks if the currently logged on user has access for the given action229 *230 * @param rundata the request rundata.231 * @param permission the jetspeed-action (view, edit, customize, delete...) for which permission is being checked.232 * @param entry the registry entry from the registry.233 * @exception Sends a RegistryException if the manager can't add234 * the provided entry235 */236privateboolean checkPermission(JetspeedUser user, String action)
237 {
238if (action == null)
239 {
240returntrue;
241 }
242243// determine if user has specified role244try245 {
246 Iterator roles = JetspeedSecurity.getRoles(user.getUserName());
247while (roles.hasNext())
248 {
249GroupRole gr = (GroupRole) roles.next();
250Role role = gr.getRole();
251return JetspeedSecurity.hasPermission(role.getName(), action);
252 }
253254 } catch (Exception e)
255 {
256 logger.error("Exception", e);
257return false;
258 }
259260returntrue;
261 }
262263privateboolean checkDefaultPermission(JetspeedUser user, String action)
264 {
265 String defaultPermissions[] = null;
266try267 {
268if ( (user == null) || !user.hasLoggedIn() )
269 {
270 defaultPermissions = JetspeedResources.getStringArray(CONFIG_DEFAULT_PERMISSION_ANONYMOUS);
271 } else272 {
273 defaultPermissions = JetspeedResources.getStringArray(CONFIG_DEFAULT_PERMISSION_LOGGEDIN);
274 }
275 }
276catch (Exception e)
277 {
278 logger.error( "Error checking permissions for " + user + " on " + action, e);
279 }
280for (int i = 0; i < defaultPermissions.length; i++)
281 {
282if (defaultPermissions[i].equals("*"))
283returntrue;
284if (defaultPermissions[i].equals(action))
285returntrue;
286 }
287return false;
288 }
289290/*291 * Turbine Services Interface292 */293294/***295 * This is the early initialization method called by the296 * Turbine <code>Service</code> framework297 * @param conf The <code>ServletConfig</code>298 * @exception throws a <code>InitializationException</code> if the service299 * fails to initialize300 */301publicsynchronizedvoid init(ServletConfig conf)
302 throws InitializationException
303 {
304if (getInit()) return;
305306super.init(conf);
307308 setInit(true);
309 }
310311 }