SecurityUtils.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
com.pentaho.security |
![]() |
![]() |
Pentaho |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* Copyright 2007 Pentaho Corporation. All rights reserved.
* This software was developed by Pentaho Corporation and is provided under the terms
* of the Mozilla Public License, Version 1.1, or any later version. You may not use
* this file except in compliance with the license. If you need a copy of the license,
* please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
* BI Platform. The Initial Developer is Pentaho Corporation.
*
* Software distributed under the Mozilla Public License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
* the license for the specific language governing your rights and limitations.
*/
package com.pentaho.security;
import java.security.Principal;
import java.util.List;
import org.acegisecurity.Authentication;
import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.GrantedAuthorityImpl;
import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.pentaho.core.repository.ISolutionRepository;
import org.pentaho.core.session.IPentahoSession;
import org.pentaho.core.system.PentahoSystem;
import com.pentaho.repository.dbbased.solution.RepositoryFile;
import com.pentaho.security.acls.IAclHolder;
import com.pentaho.security.acls.PentahoAclEntry;
import com.pentaho.security.acls.voter.IAclVoter;
/**
* A utility class with several static methods that are used to
* either bind the <tt>Authentication</tt> to the <tt>IPentahoSession</tt>, retrieve
* the <tt>Authentication</tt> from the <tt>IPentahoSession</tt>, and other various helper
* functions.
* @author mbatchel
*
*/
public class SecurityUtils {
private static final Log logger = LogFactory.getLog(SecurityUtils.class);
public static final String SESSION_PRINCIPAL = "SECURITY_PRINCIPAL"; //$NON-NLS-1$
public static String DefaultAnonymousRole = PentahoSystem.getSystemSetting(
"anonymous-authentication/anonymous-role", "Anonymous"); //$NON-NLS-1$ //$NON-NLS-2$
public static String DefaultAnonymousUser = PentahoSystem.getSystemSetting(
"anonymous-authentication/anonymous-user", "anonymous"); //$NON-NLS-1$ //$NON-NLS-2$
/**
* Looks in the provided session to get the ACEGI Authentication object out.
* Optionally returns an "anonymous" Authentication if desired.
* @param session Users' IPentahoSession object
* @param allowAnonymous If true, will return an anonymous Authentication object.
* @return the Authentication object from the session
*/
public static Authentication getAuthentication(IPentahoSession session, boolean allowAnonymous) {
Principal principal = (Principal) session.getAttribute(SESSION_PRINCIPAL);
if (logger.isDebugEnabled()) {
logger.debug("principal from IPentahoSession: " + principal); //$NON-NLS-1$
if (null != principal) {
logger.debug("principal class: " + principal.getClass().getName()); //$NON-NLS-1$
}
}
if (principal instanceof Authentication) {
if (logger.isDebugEnabled()) {
logger.debug("principal is an instance of Authentication"); //$NON-NLS-1$
}
return (Authentication) principal;
} else if (principal != null) {
if (logger.isDebugEnabled()) {
logger.debug("principal is not an instance of Authentication"); //$NON-NLS-1$
logger.debug("attempting role fetch with username"); //$NON-NLS-1$
}
// OK - Not ACEGI somehow.
// However, since the principal interface doesn't specify the
// roles a user is in, we need to dispatch a call to the
// UserRoleListProvider to get that information from there.
UserDetailsRoleListService roleListService = PentahoSystem.getUserDetailsRoleListService();
List roles = roleListService.getRolesForUser(principal.getName());
if (logger.isDebugEnabled()) {
logger.debug("rolesForUser from roleListService:" + roles); //$NON-NLS-1$
}
if (!roles.isEmpty()) {
GrantedAuthority[] grantedAuthorities = new GrantedAuthority[roles.size()];
for (int i = 0; i < roles.size(); i++) {
grantedAuthorities[i] = new GrantedAuthorityImpl((String) roles.get(i));
}
Authentication auth = new UsernamePasswordAuthenticationToken(principal.getName(), null, grantedAuthorities);
return auth;
}
}
if (logger.isDebugEnabled()) {
logger.debug("either principal is null or user has no roles"); //$NON-NLS-1$
}
if (allowAnonymous) {
if (logger.isDebugEnabled()) {
logger.debug("there is no principal in IPentahoSession"); //$NON-NLS-1$
logger.debug("creating token with username anonymous and role Anonymous"); //$NON-NLS-1$
}
// Hmmm - at this point, we're being asked for an authentication on
// an un-authenticated user. For now, we'll default to returning
// an authentication that has the user as anonymous.
Authentication auth = new UsernamePasswordAuthenticationToken(DefaultAnonymousUser, null,
new GrantedAuthorityImpl[] { new GrantedAuthorityImpl(DefaultAnonymousRole) });
return auth;
} else {
if (logger.isDebugEnabled()) {
logger.debug("there is no principal in IPentahoSession"); //$NON-NLS-1$
logger.debug("and allowAnonymous is false"); //$NON-NLS-1$
}
// If we're here - we require a properly authenticated user and
// there's nothing
// else we can do aside from returning null.
return null;
}
}
/**
* Gets the java.security.principal object from the IPentahoSession object
* @param session The users' session
* @return The bound Principal
*/
public static Principal getPrincipal(IPentahoSession session) {
Principal principal = (Principal) session.getAttribute(SESSION_PRINCIPAL);
return principal;
}
/**
* Sets the java.security.principal object into the IPentahoSession object.
* @param principal The principal from the servlet context
* @param session The users' IPentahoSession object
*/
public static void setPrincipal(Principal principal, IPentahoSession session) {
session.setAttribute(SESSION_PRINCIPAL, principal);
}
/**
* Utility method that communicates with the installed ACLVoter to determine
* administrator status
* @param session The users IPentahoSession object
* @return true if the user is considered a Pentaho administrator
*/
public static boolean isPentahoAdministrator(IPentahoSession session) {
IAclVoter voter = PentahoSystem.getAclVoter(session);
return voter.isPentahoAdministrator(session);
}
/**
* Utility method that communicates with the installed ACLVoter to determine
* whether a particular role is granted to the specified user.
* @param session The users' IPentahoSession
* @param role The role to look for
* @return true if the user is granted the specified role.
*/
public static boolean isGranted(IPentahoSession session, GrantedAuthority role) {
IAclVoter voter = PentahoSystem.getAclVoter(session);
return voter.isGranted(session, role);
}
/**
* @param aFile
* @return a boolean that indicates if this file can have ACLS placed on it.
*/
public static boolean canHaveACLS(RepositoryFile aFile) {
if (aFile.isDirectory()) { // All Directories can have ACLS
return true;
}
// Otherwise anything in the PentahoSystem extension list.
return PentahoSystem.getACLFileExtensionList().contains(aFile.getExtension());
}
public static boolean hasAccess(IAclHolder aHolder, int actionOperation, IPentahoSession session) {
IAclVoter voter = PentahoSystem.getAclVoter(session);
int aclMask = -1;
switch (actionOperation) {
case (IAclHolder.ACCESS_TYPE_READ): {
aclMask = PentahoAclEntry.PERM_EXECUTE;
break;
}
case IAclHolder.ACCESS_TYPE_WRITE:
case IAclHolder.ACCESS_TYPE_UPDATE: {
aclMask = PentahoAclEntry.PERM_UPDATE;
break;
}
case IAclHolder.ACCESS_TYPE_DELETE: {
aclMask = PentahoAclEntry.PERM_DELETE;
break;
}
case IAclHolder.ACCESS_TYPE_ADMIN: {
aclMask = PentahoAclEntry.PERM_ADMINISTRATION;
break;
}
default: {
aclMask = PentahoAclEntry.PERM_EXECUTE;
break;
}
}
return voter.hasAccess(session, aHolder, aclMask);
}
/**
* Utility method for access negotiation. For performance, not all files will
* be checked against the supplied voter.
* @param aFile
* @param actionOperation
* @param session
* @return
*/
public static boolean hasAccess(RepositoryFile aFile, int actionOperation, IPentahoSession session) {
if (!aFile.isDirectory()) {
List extensionList = PentahoSystem.getACLFileExtensionList();
String fName = aFile.getFileName();
int posn = fName.lastIndexOf('.');
if (posn >= 0) {
if (extensionList.indexOf(fName.substring(posn)) < 0) {
// Non-acl'd file. Return true.
return true;
}
} else {
// Untyped file. Allow access.
return true;
}
}
IAclVoter voter = PentahoSystem.getAclVoter(session);
int aclMask = -1;
switch (actionOperation) {
case ISolutionRepository.ACTION_EXECUTE: {
aclMask = PentahoAclEntry.PERM_EXECUTE;
break;
}
case ISolutionRepository.ACTION_ADMIN: {
// aclMask = PentahoAclEntry.ADMINISTRATION;
// break;
return isPentahoAdministrator(session);
}
case ISolutionRepository.ACTION_SUBSCRIBE: {
aclMask = PentahoAclEntry.PERM_SUBSCRIBE;
break;
}
case ISolutionRepository.ACTION_CREATE: {
aclMask = PentahoAclEntry.PERM_CREATE;
break;
}
case ISolutionRepository.ACTION_UPDATE: {
aclMask = PentahoAclEntry.PERM_UPDATE;
break;
}
case ISolutionRepository.ACTION_DELETE: {
aclMask = PentahoAclEntry.PERM_DELETE;
break;
}
default: {
aclMask = PentahoAclEntry.PERM_EXECUTE;
break;
}
}
return voter.hasAccess(session, aFile, aclMask);
}
}
The table below shows all metrics for SecurityUtils.java.




