ldaprolemanagement.java
来自「jetspeed源代码」· Java 代码 · 共 576 行 · 第 1/2 页
JAVA
576 行
/*
* Copyright 2000-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jetspeed.services.security.ldap;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.servlet.ServletConfig;
import org.apache.jetspeed.om.profile.Profile;
import org.apache.jetspeed.om.profile.ProfileException;
import org.apache.jetspeed.om.security.Role;
import org.apache.jetspeed.om.security.UserNamePrincipal;
import org.apache.jetspeed.om.security.ldap.LDAPRole;
import org.apache.jetspeed.om.security.ldap.LDAPUser;
import org.apache.jetspeed.services.JetspeedLDAP;
import org.apache.jetspeed.services.JetspeedSecurity;
import org.apache.jetspeed.services.Profiler;
import org.apache.jetspeed.services.PsmlManager;
import org.apache.jetspeed.services.ldap.LDAPURL;
import org.apache.jetspeed.services.rundata.JetspeedRunData;
import org.apache.jetspeed.services.rundata.JetspeedRunDataService;
import org.apache.jetspeed.services.security.CachedAcl;
import org.apache.jetspeed.services.security.JetspeedSecurityCache;
import org.apache.jetspeed.services.security.JetspeedSecurityException;
import org.apache.jetspeed.services.security.JetspeedSecurityService;
import org.apache.jetspeed.services.security.RoleException;
import org.apache.jetspeed.services.security.RoleManagement;
import org.apache.jetspeed.services.security.UnknownUserException;
import org.apache.turbine.services.InitializationException;
import org.apache.turbine.services.TurbineBaseService;
import org.apache.turbine.services.TurbineServices;
import org.apache.turbine.services.resources.ResourceService;
import org.apache.turbine.services.rundata.RunDataService;
/**
*
* @author <a href="mailto:ender@kilicoglu.nom.tr">Ender KILICOGLU</a>
* @author <a href="mailto:sami.leino@netorek.fi">Sami Leino</a>
*
* @version $Id: LDAPRoleManagement.java,v 1.9 2004/02/23 03:52:33 jford Exp $
*
*/
public class LDAPRoleManagement extends TurbineBaseService
implements RoleManagement
{
// Constants
private final static String CASCADE_DELETE = "programmatic.cascade.delete";
private final static String CACHING_ENABLE = "caching.enable";
private final static boolean DEFAULT_CASCADE_DELETE = true;
private final static boolean DEFAULT_CACHING_ENABLE = true;
private final static String[] ATTRS = { "ou", "uid", "rolename", "rolepermissions" };
// Instance variables
private JetspeedRunDataService runDataService = null;
private boolean cascadeDelete = false;
private boolean cachingEnable = false;
///////////////////////////////////////////////////////////////////////////
// Role Management Interfaces
///////////////////////////////////////////////////////////////////////////
/**
* Retrieves all <code>Role</code>s for a given username principal.
*
* The security service may optionally check the current user context
* to determine if the requestor has permission to perform this action.
*
* @param username a user principal identity to be retrieved.
* @return Iterator over all roles associated to the user principal.
* @exception RoleException when the security provider has a general failure.
* @exception InsufficientPrivilegeException when the requestor is denied due to insufficient privilege
*/
public Iterator getRoles(String username)
throws JetspeedSecurityException
{
StringTokenizer st;
LDAPUser user;
try
{
if (cachingEnable)
{
Iterator result = JetspeedSecurityCache.getRoles(username);
if (null != result)
{
return result;
}
}
user = (LDAPUser)JetspeedSecurity.getUser(new UserNamePrincipal(username));
}
catch(JetspeedSecurityException e)
{
throw new RoleException("Failed to Retrieve User: ", e);
}
Vector roles= new Vector();
try
{
for (Enumeration enum = user.getGroupRoles().elements(); enum.hasMoreElements();)
{
st = new StringTokenizer((String)enum.nextElement(), ",");
st.nextToken();
roles.add(new LDAPRole(st.nextToken(), false));
}
}
catch(Exception e)
{
throw new RoleException("Failed to retrieve groups ", e);
}
return roles.iterator();
}
/**
* Retrieves all <code>Role</code>s.
*
* The security service may optionally check the current user context
* to determine if the requestor has permission to perform this action.
*
* @return Iterator over all roles.
* @exception RoleException when the security provider has a general failure.
* @exception InsufficientPrivilegeException when the requestor is denied due to insufficient privilege
*/
public Iterator getRoles()
throws JetspeedSecurityException
{
Vector roles = new Vector();
Vector roleurls;
try
{
roleurls = JetspeedLDAP.search(JetspeedLDAP.buildURL("ou=roles"),"(objectclass=jetspeedrole)", ATTRS, true);
if (roleurls.size() > 0)
{
for (Enumeration enum = roleurls.elements(); enum.hasMoreElements() ;)
{
roles.add(new LDAPRole((LDAPURL) (((Vector)enum.nextElement()).firstElement())));
}
}
else
{
throw new UnknownUserException("No role ");
}
}
catch(Exception e)
{
throw new RoleException("Failed to retrieve roles ", e);
}
return roles.iterator();
}
/**
* Adds a <code>Role</code> into permanent storage.
*
* The security service may optionally check the current user context
* to determine if the requestor has permission to perform this action.
*
* @exception RoleException when the security provider has a general failure.
* @exception InsufficientPrivilegeException when the requestor is denied due to insufficient privilege
*/
public void addRole(Role role)
throws JetspeedSecurityException
{
LDAPRole ldapRole = null;
if(roleExists(role.getName()))
{
throw new RoleException("The role '" +
role.getName() + "' already exists");
}
try
{
ldapRole = new LDAPRole(role.getName(), true);
ldapRole.update(true);
}
catch(Exception e)
{
throw new RoleException("Failed to create role '" +
role.getName() + "'", e);
}
if (cachingEnable)
{
JetspeedSecurityCache.addRole(ldapRole);
}
try
{
addDefaultRolePSML(ldapRole);
}
catch (Exception e)
{
try
{
removeRole(ldapRole.getName());
}
catch (Exception e2)
{
}
throw new RoleException("failed to add default PSML for Role resource", e);
}
}
protected void addDefaultRolePSML(Role role)
throws RoleException
{
try
{
JetspeedRunDataService runDataService =
(JetspeedRunDataService)TurbineServices.getInstance()
.getService(RunDataService.SERVICE_NAME);
JetspeedRunData rundata = runDataService.getCurrentRunData();
Profile profile = Profiler.createProfile();
profile.setRole(role);
profile.setMediaType("html");
Profiler.createProfile(rundata, profile);
}
catch (ProfileException e)
{
try
{
removeRole(role.getName());
}
catch(Exception e2)
{
e.printStackTrace();
}
throw new RoleException("Failed to create Role PSML", e);
}
}
/**
* Saves a <code>Role</code> into permanent storage.
*
* The security service can throw a <code>NotUniqueEntityException</code> when the public
* credentials fail to meet the security provider-specific unique constraints.
* The security service may optionally check the current user context
* to determine if the requestor has permission to perform this action.
*
* @exception RoleException when the security provider has a general failure.
* @exception InsufficientPrivilegeException when the requestor is denied due to insufficient privilege
*/
public void saveRole(Role role)
throws JetspeedSecurityException
{
if(!roleExists(role.getName()))
{
throw new RoleException("The role '" +
role.getName() + "' doesn't exists");
}
try
{
}
catch(Exception e)
{
throw new RoleException("Failed to create role '" +
role.getName() + "'", e);
}
}
/**
* Removes a <code>Role</code> from the permanent store.
*
* The security service may optionally check the current user context
* to determine if the requestor has permission to perform this action.
*
* @param roleName the principal identity of the role to be retrieved.
* @exception RoleException when the security provider has a general failure.
* @exception InsufficientPrivilegeException when the requestor is denied due to insufficient privilege
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?