pl.edu.icm.unity.engine.bulkops.EntityMVELContextBuilder Maven / Gradle / Ivy
/*
* Copyright (c) 2019 Bixbit - Krzysztof Benedyczak. All rights reserved.
* See LICENCE.txt file for licensing information.
*/
package pl.edu.icm.unity.engine.bulkops;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.google.common.collect.Sets;
import pl.edu.icm.unity.engine.api.bulk.EntityInGroupData;
import pl.edu.icm.unity.engine.api.bulkops.EntityMVELContextKey;
import pl.edu.icm.unity.types.authn.CredentialInfo;
import pl.edu.icm.unity.types.authn.CredentialPublicInformation;
import pl.edu.icm.unity.types.basic.AttributeExt;
import pl.edu.icm.unity.types.basic.Identity;
/**
* Helper responsible for build or print entity related MVEL context
*
* @author P.Piernik
*/
public class EntityMVELContextBuilder
{
private static final Set SENSITIVE = Sets.newHashSet("hash", "cred", "pass");
public static String ctx2ReadableString(Object context, String pfx)
{
if (!(context instanceof Map))
return context.toString();
Map, ?> map = (Map, ?>) context;
StringBuilder ret = new StringBuilder(10240);
map.forEach((k, v) -> {
String key = k.toString();
ret.append(pfx).append(k).append(": ");
if (seemsSensitive(key))
ret.append("--MASKED--").append("\n");
else
ret.append(ctx2ReadableString(v, pfx + " ")).append("\n");
});
return ret.toString();
}
private static boolean seemsSensitive(String key)
{
for (String checked : SENSITIVE)
if (key.contains(checked))
return true;
return false;
}
public static Map getContext(EntityInGroupData membershipInfo)
{
return getContext(membershipInfo.entity.getIdentities(), membershipInfo.entity.getState().toString(),
membershipInfo.entity.getCredentialInfo(), membershipInfo.groups,
membershipInfo.groupAttributesByName.values());
}
public static Map getContext(List identities, String entityStatus,
CredentialInfo credentialInfo, Set groups, Collection attributes)
{
Map ctx = new HashMap<>();
Map> idsByType = new HashMap<>();
Map> idsByTypeObj = new HashMap<>();
for (Identity identity : identities)
{
List vals = idsByType.get(identity.getTypeId());
if (vals == null)
{
vals = new ArrayList<>();
idsByType.put(identity.getTypeId(), vals);
}
vals.add(identity.getValue());
List