return toAssociations;
}
protected JetspeedPrincipal synchronizeEntity(Entity entity, Set<JetspeedPrincipalAssociationReference> toAssociations) throws SecurityException
{
JetspeedPrincipal principal = getJetspeedPrincipal(entity.getType(), entity.getId());
JetspeedPrincipalManager principalManager = principalManagerProvider.getManager(principalManagerProvider.getPrincipalType(entity.getType()));
boolean syncAll = false;
if (principal == null)
{
// principal does not exist yet, create a new one using the principal manager
principal = principalManager.newPrincipal(entity.getId(), true);
principalManager.addPrincipal(principal, toAssociations);
syncAll = true;
}
else if (!principal.isMapped())
{
logger.debug("Found "+principal.getType().getName()+" principal: "+principal.getName()+" is not mapped therefore not synchronized!");
return null;
}
else
{
// sync relations
for (final SecurityEntityRelationType relationType : securityEntityManager.getSupportedEntityRelationTypes(entity.getType()))
{
if (relationType.getFromEntityType().equals(entity.getType()))
{
List<? extends JetspeedPrincipal> associatedFrom = principalManager.getAssociatedFrom(principal.getName(), principal.getType(), relationType.getRelationType());
for (JetspeedPrincipal p : associatedFrom)
{
if (toAssociations.isEmpty() ||
!toAssociations.remove(new JetspeedPrincipalAssociationReference(JetspeedPrincipalAssociationReference.Type.TO, p, relationType.getRelationType())))
{
principalManager.removeAssociation(principal, p, relationType.getRelationType());
}
}
}
}
for (JetspeedPrincipalAssociationReference ref : toAssociations)
{
principalManager.addAssociation(principal, ref.ref, ref.associationName);
}
}
boolean updated = false;
SecurityAttributes principalAttrs = principal.getSecurityAttributes();
for (AttributeDef attrDef : entity.getAttributeDefinitions())
{
if (attrDef.isMapped() && !attrDef.isMultiValue())
{
Attribute attr = entity.getAttribute(attrDef.getName());
if (attr == null)
{
if (!syncAll)
{
// if principal has attr: remove it
SecurityAttribute principalAttr = principalAttrs.getAttribute(attrDef.getMappedName());
if (principalAttr != null)
{
if (logger.isDebugEnabled())
{
logger.debug("Removing attribute "+principalAttr.getName()+" for principal "+principal.getName()+".");
}
principalAttrs.removeAttribute(principalAttr.getName());
updated = true;
}
}
}
else if (syncAll)
{
SecurityAttribute principalAttr = principalAttrs.getAttribute(attrDef.getMappedName(), true);
if (logger.isDebugEnabled())
{
logger.debug("Adding attribute "+principalAttr.getName()+" for principal "+principal.getName()+". Value: "+attr.getValue());
}
principalAttr.setStringValue(attr.getValue());
updated = true;
}
else
{
SecurityAttribute principalAttr = principalAttrs.getAttribute(attrDef.getMappedName(), true);
if (!StringUtils.equals(principalAttr.getStringValue(), attr.getValue()))
{
if (logger.isDebugEnabled())
{
logger.debug("Updating attribute "+principalAttr.getName()+" for principal "+principal.getName()+". Old value: "+(principalAttr.getStringValue())+" new value: "+attr.getValue());
}
principalAttr.setStringValue(attr.getValue());
updated = true;
}
}
}
}
if (updated)
{
if (logger.isDebugEnabled())
{
logger.debug("Storing attribute changes for principal "+principal.getName());
}
principalManager.updatePrincipal(principal);
}
if (logger.isDebugEnabled())
{