SecurityParameterProvider.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 2006 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.
*
* Created Apr 17, 2006
*
* @author mbatchel
*/
package com.pentaho.security;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import org.acegisecurity.Authentication;
import org.acegisecurity.GrantedAuthority;
import org.pentaho.core.session.IPentahoSession;
import org.pentaho.core.solution.IParameterProvider;
import org.pentaho.core.system.PentahoSystem;
import java.util.List;
public class SecurityParameterProvider implements IParameterProvider {
public static final List SecurityNames = new ArrayList(4);
public static final List SecurityTypes = new ArrayList(4);
private static final int PRINCIPAL_NAME = 0;
private static final int PRINCIPAL_ROLES = 1;
private static final int PRINCIPAL_AUTHENTICATED = 2;
private static final int PRINCIPAL_IS_ADMINISTRATOR = 3;
private static final int SYSTEM_ROLE_NAMES = 4;
private static final int SYSTEM_USER_NAMES = 5;
public static final String SCOPE_SECURITY = "security"; //$NON-NLS-1$
private String listSeparator = ",";//$NON-NLS-1$
private IPentahoSession session;
static {
SecurityNames.add("principalName"); //$NON-NLS-1$
SecurityNames.add("principalRoles");//$NON-NLS-1$
SecurityNames.add("principalAuthenticated");//$NON-NLS-1$
SecurityNames.add("principalAdministrator");//$NON-NLS-1$
SecurityNames.add("systemRoleNames");//$NON-NLS-1$
SecurityNames.add("systemUserNames");//$NON-NLS-1$
SecurityTypes.add("string"); //$NON-NLS-1$
SecurityTypes.add("string-list");//$NON-NLS-1$
SecurityTypes.add("string"); //$NON-NLS-1$
SecurityTypes.add("string"); //$NON-NLS-1$
SecurityTypes.add("string-list");//$NON-NLS-1$
SecurityTypes.add("string-list");//$NON-NLS-1$
}
public SecurityParameterProvider(IPentahoSession session) {
super();
this.session = session;
}
public void setListSeparator(String value) {
this.listSeparator = value;
}
public String getListSeparator() {
return this.listSeparator;
}
public String getStringParameter(String name, String defaultValue) {
Object obj = getParameter(name);
if (obj != null) {
if (obj instanceof List) {
return listToString((List) obj);
} else if (obj instanceof String[]) {
return arrayToString((String[]) obj);
} else if (obj instanceof GrantedAuthority[]) {
return arrayToString((GrantedAuthority[]) obj);
} else {
return obj.toString();
}
}
return defaultValue;
}
public String listToString(List aList) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < aList.size(); i++) {
if (aList.get(i) != null) {
Object listObj = aList.get(i);
if (listObj instanceof GrantedAuthority) {
sb.append(i > 0 ? this.listSeparator : "").append(((GrantedAuthority) listObj).getAuthority());//$NON-NLS-1$
} else {
sb.append(i > 0 ? this.listSeparator : "").append(listObj.toString());//$NON-NLS-1$
}
}
}
return sb.toString();
}
public String arrayToString(String[] anArray) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < anArray.length; i++) {
if (anArray[i] != null) {
sb.append(i > 0 ? this.listSeparator : "").append(anArray[i]);//$NON-NLS-1$
}
}
return sb.toString();
}
public String arrayToString(GrantedAuthority[] anArray) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < anArray.length; i++) {
if (anArray[i] != null) {
sb.append(i > 0 ? this.listSeparator : "").append(anArray[i].getAuthority());//$NON-NLS-1$
}
}
return sb.toString();
}
public long getLongParameter(String name, long defaultValue) {
// No integer parameters supported
return defaultValue;
}
public Date getDateParameter(String name, Date defaultValue) {
// No Date parameters supported
return defaultValue;
}
public Object getDecimalParameter(String name, Object defaultValue) {
// No decimal parameters supported
return defaultValue;
}
public Iterator getParameterNames() {
return SecurityNames.iterator();
}
public String getParameterType(String name) {
int idx = SecurityNames.indexOf(name);
if (idx >= 0) {
return (String) SecurityTypes.get(idx);
}
return null;
}
public Object getParameter(String name) {
if (name.startsWith("principal")) { //$NON-NLS-1$
if (name.equals(SecurityNames.get(PRINCIPAL_NAME))) {
return getPrincipalName();
} else if (name.equals(SecurityNames.get(PRINCIPAL_ROLES))) {
return getPrincipalRoles();
} else if (name.equals(SecurityNames.get(PRINCIPAL_AUTHENTICATED))) {
return getPrincipalAuthenticated();
} else if (name.equals(SecurityNames.get(PRINCIPAL_IS_ADMINISTRATOR))) {
return getPrincipalIsAdministrator();
}
} else {
if (name.equals(SecurityNames.get(SYSTEM_ROLE_NAMES))) {
return getSystemRoleNames();
} else if (name.equals(SecurityNames.get(SYSTEM_USER_NAMES))) {
return getSystemUserNames();
}
}
return null;
}
private Authentication getAuthentication() {
if (session != null) {
return SecurityUtils.getAuthentication(session, true); // Should the "true" be a setting???
}
return null;
}
protected String getPrincipalName() {
Authentication auth = getAuthentication();
if (auth != null) {
return auth.getName();
}
return null;
}
protected String getPrincipalAuthenticated() {
Authentication auth = getAuthentication();
if (auth != null) {
return auth.isAuthenticated() ? "true" : "false"; //$NON-NLS-1$ //$NON-NLS-2$
}
return "false"; //$NON-NLS-1$
}
protected String getPrincipalIsAdministrator() {
return SecurityUtils.isPentahoAdministrator(this.session) ? "true" : "false"; //$NON-NLS-1$ //$NON-NLS-2$
}
protected Object getPrincipalRoles() {
Authentication auth = getAuthentication();
if (auth != null) {
GrantedAuthority[] auths = auth.getAuthorities();
if (auths != null) {
List rtn = new ArrayList(auths.length);
for (int i = 0; i < auths.length; i++) {
rtn.add(auths[i].getAuthority());
}
return rtn;
} else {
return new ArrayList();
}
}
return null;
}
protected Object getSystemRoleNames() {
UserDetailsRoleListService service = PentahoSystem.getUserDetailsRoleListService();
if (service != null) {
return service.getAllRoles();
}
return null;
}
protected Object getSystemUserNames() {
UserDetailsRoleListService service = PentahoSystem.getUserDetailsRoleListService();
if (service != null) {
return service.getAllUsers();
}
return null;
}
}
The table below shows all metrics for SecurityParameterProvider.java.




