org.picketlink.idm.internal.util.PermissionUtil Maven / Gradle / Ivy
/*
* JBoss, Home of Professional Open Source.
* Copyright 2012, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.picketlink.idm.internal.util;
import org.picketlink.idm.permission.Permission;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import static org.picketlink.common.util.StringUtil.isNullOrEmpty;
/**
* Utility class for common permission operations.
*
* @author Pedro Igor
*/
public class PermissionUtil {
/**
* Adds a new operation
to operationsCSV
, where the latter is a CSV representing the current
* operationsCSV.
*
* @param operationsCSV A CSV of operationsCSV
* @param operation The operation to add
*
* @return The CSV string updated with the given operation.
*/
public static String addOperation(String operationsCSV, String operation) {
Set ops = asOperationList(operationsCSV);
for (String newOperation : asOperationList(operation)) {
if (!ops.contains(newOperation)) {
ops.add(newOperation);
}
}
return asOperationCSV(ops);
}
/**
* Removes a new operation
to operationsCSV
, where the latter is a CSV representing the current
* operationsCSV.
*
* @param operationsCSV A CSV of operationsCSV
* @param operation The operation to remove
*
* @return The CSV string updated without the given operation.
*/
public static String removeOperation(String operationsCSV, String operation) {
Set ops = asOperationList(operationsCSV);
for (String opToRemove : asOperationList(operation)) {
ops.remove(opToRemove);
}
return asOperationCSV(ops);
}
/**
* Returns a {@link java.util.Set} with all operationsCSV from operationsCSV
.
*
* @param operationsCSV The CSV of operationsCSV.
*
* @return A set with all operationsCSV.
*/
public static Set asOperationList(String operationsCSV) {
Set operations = new HashSet();
if (!isNullOrEmpty(operationsCSV)) {
for (String operation : operationsCSV.split(",")) {
operations.add(operation.trim());
}
}
return operations;
}
/**
* Check if the given {@link org.picketlink.idm.permission.Permission} has the given attributes.
*
* @param permission The permission to check
* @param resourceClass
* @param identifier
* @param operation
*
* @return True if the permission has all thi given attributes. Otherwise, false.
*/
public static boolean hasAttributes(Permission permission, Class> resourceClass, Serializable identifier, String operation) {
if (!permission.getResourceClass().equals(resourceClass)) {
return false;
}
if (!permission.getResourceIdentifier().toString().equals(identifier.toString()) && !identifier.equals(resourceClass.getName())) {
return false;
}
if (hasOperation(permission, operation)) {
return true;
}
return false;
}
/**
* Checks if the fiven given operation
is granted by the given {@link org.picketlink.idm.permission.Permission}.
*
* @param permission
* @param operation
* @return
*/
public static boolean hasOperation(Permission permission, String operation) {
String operationToCheck = operation;
if (operationToCheck == null) {
operationToCheck = "*";
}
for (String op : asOperationList(permission.getOperation())) {
for (String opCheck : asOperationList(operationToCheck)) {
if ("*".equals(opCheck.trim()) || op.trim().equals(opCheck.trim())) {
return true;
}
}
}
return false;
}
/**
* Returns a CSV string containing all operations from the give {@link java.util.Set}.
*
* @param ops A set with strings where each one represents an operation.
*
* @return
*/
private static String asOperationCSV(Set ops) {
StringBuilder sb = new StringBuilder();
for (String op : ops) {
if (sb.length() > 0) {
sb.append(",");
}
sb.append(op);
}
return sb.toString();
}
}